Home » Wordpress » How to create a widget that uses parameters on a global and per post basis?

How to create a widget that uses parameters on a global and per post basis?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have created a widget and so far it only displays on the home page. However I want it to appear on the post page, and only if the user has inputted a parameter.

class My_Widget extends WP_Widget {
    function __construct() {
        $widget_ops = array ('description' => __ ( 'Lorem ipsum' ) );
        parent::__construct ( 'exampl', __ ( 'Example' ), $widget_ops );
    }
    function widget($args, $instance) {
        extract ( $args );
        $title = apply_filters ( 'widget_title', empty ( $instance ['title'] ) ? ' ' : $instance ['title'] );
        # Before the widget
        echo $before_widget;

        # The title
        if ($title)
            echo $before_title . $title . $after_title;

        # Make the Hello World Example widget
        echo isset($instance['foo'])?$instance['foo']:'foo';
        echo isset($instance['bar'])?$instance['bar']:'bar';

        # After the widget
        echo $after_widget;

    }
    //The global widget options
    function form($instance){
        /* Set up some default widget settings. */
        $defaults = array( 'foo' => 'oof', 'bar' => 'rab');
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'foo' ); ?>"><!--shortened... -->
        </p>
        <?php 
    }
}

function my_init() {
    register_widget ( 'My_Widget' );
}
add_action ( "widgets_init", "my_init" );

Now the widget appears in the home page on the right, the default options are treated correctly.

It does not appear on the post page however. (“Hello world! Welcome to WordPress. This is your first post…”)
A) How can I make it appear only on the post page
B) How can I control the parameters on a per post level

Example: The global setting “color” should be ‘green’, controlled on the widgets page. however in the post page I want a textarea “poemoftheday”, if it’s not empty it should show up on the sidebar using the global “color” ‘green’, but depending on the post, the “poemoftheday” will be different or not present

My Problems All the WordPress terms and version have made it really hard to find out the correct current solution. I’ve been working on this for 7 hours now and I’m really stuck. Not asking people to code for me, but I need to know the right terms and methods I should be using. Pointers to tutorials/documentations that solve fit my requirements greatly appreciated (there are sooo many tutorials, but they all behave different from the behaviour I’m looking for)

Visually
This is where the global configuration is, great
^This is where the global configuration is, great


This is where I'd like to have the per post configuration
^This is where I’d like to have the per post configuration


This is where it's appearing right now, no good
^This is where it’s appearing right now – displaying good, position no good


This is where I'd like it to appear
^This is where I’d like it to appear

Answers:

So here’s how I solved it after 5 days.

To write store per_post vars I had to use the function

  • In my class constructor i added the hook add_action ( 'add_my_meta_box', array (__CLASS__, 'add_my_meta_box') );
  • create the callback function add_my_meta_box($page) in my class
  • In that callback amongst other things add_meta_box ( 'My Widget', 'My box', array (__CLASS__, 'render_meta_box' ), $page);

All values are read and stored with global $post; and the wordpress functions get_post_meta ($post->ID, 'my_var',true ) and update_post_meta ($post->ID, 'my_var', $value )

To make sure that my widget only appears on a single post page I used the check
if(is_single()) before echoing the widget