How to enter a Shortcode into a Widget via backend in WordPress

I’m trying to complete a wordpress widget that will allow me to enter different shortcodes via my admin panel in wordpress into a widget. There are a multitude of shortcodes that a plugin uses and I to use them in my side par widget. The widget does execute the shortcodes if they are hard coded . The question is, how do i code to widget to accept the short code yia admin => appearance => widgets panel and the execute it?

MJ, what do you mean when you say the widget executes when it is hard coded?

Does your widget load it’s info from a database or do you mean you place one shortcode into the widget code itself?

There are many ways to get a widget to alter itself. But, most of them are handled by the widget to load the commands from somewhere, usually a database. Therefore, your back-end admin panel would need to store the shortcode(s) into a database table. Then, the widget needs to read that table and see what it is supposed to be handling. Or, you could make multiple widgets, but, that is not the usual way to do it.

Not sure if that helps, but, I think we need further info so we can help.

When I say hard coded i mean written into the widget itself without the ability to change it, see lines 31-35, this is also examples of the type of shortcode I run. But I need to be able to execute additional shortcodes that the admin can add to customize the widget in the side bar, see lines 145-146, witch did not work. Below is what I have so far:

[php]function wpspprofwid_load_widgets() {
register_widget( ‘WPS_PRO_Profile_Widget’ );
}
add_action( ‘widgets_init’, ‘wpspprofwid_load_widgets’ );

/**

  • Profile Widget class.
    */
    class WPS_PRO_Profile_Widget extends WP_Widget {

    public function __construct() {
    $widget_ops = array( ‘description’ => __(‘Add a custom Profile to your sidebar.’) );
    parent::__construct( ‘nav_menu1’, __(‘WPS PRO Profile’), $widget_ops );

    }

    //How to display Widget on Screen
    public function widget($args, $instance) {

     // Set Title
     $title = apply_filters( 'widget_title', $instance['title'] );
     
     // Display before widget code
     echo $before_widget;
     
     //Display Title
     if ( ! empty( $title ) )
     	echo $args['before_title'] . $title . $args['after_title'];
     	
     // Do Shortcodes
     
     echo do_shortcode('[wps-avatar size="200"]');
     echo "<h3>My Friends</h3>";
     echo do_shortcode('[wps-friends size="35"]');
      
      //Set Menu title
     $mtitle = apply_filters( 'Menu Title', $instance['mtitle'] );
     
     //Display Title
     if ( ! empty( $mtitle ) )
     	echo $args['before_mtitle'] . "<h3>$mtitle</h3>" . $args['after_mtitle'];
     
     // Get menu
     $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
    
     if ( !$nav_menu )
     	return;
    
     wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );
     
      //Set Shortcode 1 title
     $sc1title = apply_filters( 'Shortcode 1 Title', $instance['sc1title'] );
     
     //Display Shortcode 1 title
     if ( ! empty( $sc1title ) )
     	echo $args['before_sc1title'] . "<h3>$sc1title</h3>" . $args['after_sc1title'];
     	
     //Do Shortcode 1
     //echo do_shortcode('sc1');
     //echo do_shortcode($this->shortcodes[$instance['sc1']]);
     echo do_shortcode('[sc1]');
    

    echo “
    ”;

     //Set Shortcode 2 title
     $sc2title = apply_filters( 'Shortcode 2 Title', $instance['sc2title'] );
     
     //Display Shortcode 2 title
     if ( ! empty( $sc2title ) )
     	echo $args['before_sc1title'] . "<h3>$sc2title</h3>" . $args['after_sc1title'];
     	
     //Do Shortcode 1
     //echo do_shortcode('sc2');
     echo do_shortcode($this->shortcodes[$instance['sc2']]);	
     	
     echo $args['after_widget'];
    

    }

    // Update the widget settings.
    public function update( $new_instance, $old_instance ) {
    $instance[‘title’] = strip_tags( stripslashes($new_instance[‘title’]) );
    $instance[‘mtitle’] = strip_tags( stripslashes($new_instance[‘mtitle’]) );
    $instance[‘nav_menu’] = (int) $new_instance[‘nav_menu’];
    $instance[‘sc1title’] = strip_tags( stripslashes($new_instance[‘sc1title’]) );
    $instance[‘sc1’] = strip_tags( stripslashes($new_instance[‘sc1’]) );
    $instance[‘sc2title’] = strip_tags( stripslashes($new_instance[‘sc2title’]) );
    $instance[‘sc2’] = strip_tags( stripslashes($new_instance[‘sc2’]) );
    return $instance;

    }

    // Displays the form on the widget page
    public function form( $instance ) {
    $title = isset( $instance[‘title’] ) ? $instance[‘title’] : ‘’;
    $nav_menu = isset( $instance[‘nav_menu’] ) ? $instance[‘nav_menu’] : ‘’;
    $mtitle = isset( $instance[‘mtitle’] ) ? $instance[‘mtitle’] : ‘’;
    $sc1title = isset( $instance[‘sc1title’] ) ? $instance[‘sc1title’] : ‘’;
    $sc1 = isset( $instance[‘sc1’] ) ? $instance[‘sc1’] : ‘’;
    $sc2title = isset( $instance[‘sc2title’] ) ? $instance[‘sc2title’] : ‘’;
    $sc2 = isset( $instance[‘sc2’] ) ? $instance[‘sc2’] : ‘’;
    $nav_menu1_placeholder = ( 0 > $_nav_menu1_placeholder ) ? intval($_nav_menu1_placeholder) - 1 : -1;

     // Get menus
     $menus = wp_get_nav_menus( array( 'orderby' => 'name' ) );
    
     // If no menus exists, direct the user to go and create some.
     if ( !$menus ) {
     	echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
     	return;
     }
     ?>
     <p>
     	<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wpspprofwid' ); ?></label>
     	<input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />	
     </p>
     <p>
     	<label for="<?php echo $this->get_field_id( 'mtitle' ); ?>"><?php _e( 'Menu Title:' ); ?></label>
     	<input type="text" class="widefat" id="<?php echo $this->get_field_id('mtitle'); ?>" name="<?php echo $this->get_field_name('mtitle'); ?>" value="<?php echo $mtitle; ?>" />
     </p>
     <p>
     	<label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
     	<select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
     <?php
     	foreach ( $menus as $menu ) {
     		echo '<option value="' . $menu->term_id . '"'
     			. selected( $nav_menu, $menu->term_id, false )
     			. '>'. $menu->name . '</option>';
     	}
     ?>
     	</select>
     </p>
     <p>
     	<label for="<?php echo $this->get_field_id( 'sc1title' ); ?>"><?php _e( 'Shortcode 1 Title:' ); ?></label>
     	<input type="text" class="widefat" id="<?php echo $this->get_field_id('sc1title'); ?>" name="<?php echo $this->get_field_name('sc1title'); ?>" value="<?php echo $sc1title; ?>" />
     </p>
     <p>
     	<label for="<?php echo $this->get_field_id( 'sc1' ); ?>"><?php _e( 'Shortcode 1:' ); ?></label>
     	<input type="text" class="widefat" id="<?php echo $this->get_field_id('sc1'); ?>" name="<?php echo $this->get_field_name('sc1'); ?>" value="<?php echo $sc1; ?>" />
     </p>
     <p>
     	<label for="<?php echo $this->get_field_id( 'sc1title' ); ?>"><?php _e( 'Shortcode 2 Title:' ); ?></label>
     	<input type="text" class="widefat" id="<?php echo $this->get_field_id('sc2title'); ?>" name="<?php echo $this->get_field_name('sc2title'); ?>" value="<?php echo $sc2title; ?>" />
     </p>
     <p>
     	<label for="<?php echo $this->get_field_id( 'sc2' ); ?>"><?php _e( 'Shortcode 2:' ); ?></label>
     	<input type="text" class="widefat" id="<?php echo $this->get_field_id('sc2'); ?>" name="<?php echo $this->get_field_name('sc2'); ?>" value="<?php echo $sc2; ?>" />
     </p>
     <?php
    

    }
    }[/php]

So, the lines 31-35:

// Do Shortcodes

echo do_shortcode(’[wps-avatar size=“200”]’);
echo “

My Friends

”;
echo do_shortcode(’[wps-friends size=“35”]’);

Are hardcoded. So, to replace it with PHP code, just load the correct “shortcode” data from your database into a variable and instead of 'wps-avatar size=“200” ', you would put the variable there instead.
Now how to load that variable is your decision. My guess is that you would want to use a stored value inside your database somewhere. This would be pulled out using a query. Then, the data would be stored into a variable, let’s call it $shortcode1 and $shortcode2… So, then, change your shortcode to be something like:

// Do Shortcodes

echo do_shortcode(’[$shortcode1]’);
echo “

My Friends

”;
echo do_shortcode(’[$shortcode2]’);

If the shortcode that you need to put there is different each time, then, that will do it, but, if you are just talking about the “size” numbers, then just store the numbers not the full code.

Not really sure if that is what you are looking for. But, hope it helps some… Good luck!

Thanks for the code. Yes the shortcodes are all different and the plugin author keeps adding new once every day and the plugin is being developed right now. So I need a way for the admin to enter the short code without stripping any thing off it. the current input code i’m using does the stripping that I don’t want. And as every site is different I need the ability to allow the admin do use the code he/she wants not a choice from an array or database. Currently there are 30 plus shortcodes with a multitude of variations for each.

Well, so, I would create a table in the ADMIN pages to keep the shortcodes as you call them.
You would have the 30+ in a table and the variations in a sub-table or in a second field.

The ADMIN could then, select the first drop-down that would select the main 3o+ codes.
This could cause the second drop-down to show the variations. Once the ADMIN selects
the code plus the variation, it can be saved in the site’s tables.

Then, you would just have to pull the saved value out in the live site and place it where it
belongs in your code you showed earlier.

Hope that makes sense… Good luck!

What I would like to do is use a statment similar to:
[php]<?php _e( 'Shortcode 1 Title:' ); ?>
[/php]
That allows me to enter a title or text. So I tries:

[php]<?php _e('Shortcode 1:'); ?>
[/php]

it seams to update the database with the shortcode as when I refreash the widget the shortcode is still in the field but all it displays is [$sc1]

Okay, so you have two parts.

The first is the ADMIN part. This is where you place the drop-down for the ADMIN to select the shortcode he/she wants to show. Once this is selected it should be saved into the database in the ADMIN’s table for that display or that page.

The second part is the display of the selected short code. This is where it must do a query to the ADMIN’s table and find out which shortcode is needed to be displayed. Then, it has to load the shortcode value and save it in the variable which looks like you call “$sc1”. So, somewhere, before you echo $sc1, you have to load it from the database. You did not show that part of code.

Don’t need to show it, but, you must make sure that $sc1 is “loaded” with the correct value. Does $this->get_field_id(’$sc1’) get the contents of that field? I don’t think so. I think $sc1 is the name of the
variable that holds the value of the field name. So, to debug this, you would have to add a line between #1 and #2 that you posted and do this line: die("***".$sc1."***"); This will print out the current value of $sc1. It show the value inside of stars in case a lot of other stuff is on the screen. Should be at the bottom of the screen. So, it will show you what $sc1 actually holds and you can see if it is what you want
it to be or not.
Then, of course, you can fix it as needed. I do think you are getting close to the solution…

Good luck!

No I don’t think that its saving the shortcode because all it does is show ******

currently this is the code i’m using to save sc1:

[php]$instance[‘sc1’] = $new_instance[‘sc1’] ;[/php]

Well, not sure what that “instance” routine does. So, you are using a WordPress system.
You have a WordPress Template that you are using. You have created a Widget that is used
in your template. The back-end of the template’s Widget is loaded by the ADMIN using the
WordPress control panel. Correct up to here?

Now, in the ADMIN’s control panel, you store a value into a database table for future use.
This is the “data” that will be pulled out in the site from the database and shown inside the
Widget. Correct?

So, in the “die” debug line you did, it clearly is showing it is NOT getting the data from the database
table where you saved the value in the ADMIN panel. So, first, in the ADMIN pages, did you actually
get it to save the value into the database? Did you go live to the database and look at the data in
it to see if the value actually got stored in it? Do these and make sure the data is there. If not, you
have to fix the ADMIN code where it is supposed to be saving the value. If it is there, then, you are
pulling it back out incorrectly and have to fix the site’s part.

Not sure if that helps or not…

Its not posting to database, so that is the first issue.

Well, not saving to the database would be an issue with the back-end section.
(The ADMIN or WP section, not the displaying section.)

So, first, you must study the template you are using to see what the docs say about adding new fields
to the database and how to save data to it. Every template seems to be different. So, inside your
Widget panel in the WP admin section, how are you adding in the shortcodes to the Widget you are
calling this addin?

Also, what code is used inside the admin-widget-panel to add in the shortcode options? Is that where
you are using the " $instance[‘sc1’] = $new_instance[‘sc1’] ; " line? Since we do not have that template
and do not have access to all the code, back-end admin and front-end site, it is very hard to sort this out
without seeing more of the code. Do you have the full docs on the template you are using?

What is the name of the template you are using and where did you get it? Or did you create it all from
scratch with just Wordpress?

Sponsor our Newsletter | Privacy Policy | Terms of Service