in my widget, shortcode $variable parameter not working
-
I wrapped a plugin with a widget. i want the widget to tell the plugin what to do by sending the widget $title to the plugin shortcode parameter.
echo do_shortcode('[rotating-posts category_name="blog"]');
works
echo do_shortcode('[rotating-posts category_name="$title"]');
does not.
i tried a few different syntaxes,
_name=$title – nothing happens
_name=’$title’ – crashI will post the code in the next comment.
-
<?php /* Plugin Name: Rotating Post Widgetizer Description: Assists configuration and placement of rotating post plugin. */ /* Start Adding Functions Below this Line */ // Creating the widget class rpost_widget extends WP_Widget { function __construct() { parent::__construct( // Base ID of your widget 'rpost_widget', // Widget name will appear in UI __('Rotating Post', 'rpost_widget_domain'), // Widget description array( 'description' => __( 'Wrapper for rotating posts plugin.', 'rpost_widget_domain' ), ) ); } // Creating widget front-end // This is where the action happens public function widget( $args, $instance ) { $title = apply_filters( 'widget_title', $instance['title'] ); // before and after widget arguments are defined by themes echo $args['before_widget']; //if ( ! empty( $title ) ) // dont show the title here, let the shortcode bring the title in //echo $args['before_title'] . $title . $args['after_title']; // This is where you run the code and display the output echo do_shortcode('[rotating-posts category_name="blog"]'); echo $args['after_widget']; } // Widget Backend public function form( $instance ) { if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'New title', 'rpost_widget_domain' ); } // Widget admin form ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> </p> <?php } // Updating widget replacing old instances with new public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; return $instance; } } // Class rpost_widget ends here // Register and load the widget function rpost_load_widget() { register_widget( 'rpost_widget' ); } add_action( 'widgets_init', 'rpost_load_widget' ); /* Stop Adding Functions Below this Line */ ?>
PHP traets strings in two differnt ways depending on the quotes that you use for them. It’s a little strange, but when you understand the difference you’ll find it easy.
When you use double-quotes, any variables inside it are parsed to the value of the variable. eg:
$val = "Hello World" echo "$val"
This outputs
Hello World
.When you use single-quotes, any variable sinside are not parsed, it’s taken as a literal string exactly as you’ve typed it in:
$val = "Hello World"; echo '$val';
This outputs
$val
.In your case, you’d need to set up your call something like this:
echo do_shortcode('[rotating-posts category_name="' . $title . '"]');
As a foot note, I prefer doing it like this os that I can easily and clearly see where the variables are in the text. That lets you see where things are and there’s no problems with missing variables inside the string when you’re looking at it quickly.
Thank you for such a detailed answer. You went way above and beyond what i expected, and have solved my issue completely. I am in your debt.
I somewhat understand the difference between ‘ and “, but i never would have put it together with .’s to make “‘ . $var . ‘”
- The topic ‘in my widget, shortcode $variable parameter not working’ is closed to new replies.