1

I am running into a problem where I have no idea where to add in a description so it will show up on the widget dashboard. It just keeps displaying the title.

Something else I would love some help with is to add functionality to display the 5 most recent post titles (all categories) and I also do not know how to do that. I will post the code I have now below. Any input will greatly help my web design education.

    <?php
/*
Plugin Name: Kevins Textbox
Plugin URI: 
Description: A text widget created by Kevin Ullyott for practice in creating widgets
Author: Kevin Ullyott
Version: 1.0
Author URI: http://modmacro.com/
*/


class kevintext extends WP_Widget {

    public function __construct() {
        parent::WP_Widget(false, $name='Kevins Textbox');
        array('description' => __('A text widget created by Kevin Ullyott for practice in creating widgets') );
    }


    public function widget( $args, $instance ) {


        extract( $args );

        $headline = $instance['headline'];      
        $text = $instance['text'];      



        echo $before_widget;

        echo $before_title;

        echo "<p class=\"headline\">$headline</p>";

        echo $after_title;

        echo "<p class=\"text\">$text</p>";

        echo $after_widget;


    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['headline'] = ( $new_instance['headline'] );
        $instance['text'] = ( $new_instance['text'] );
        return $instance;
    }


    public function form( $instance ) {

        $headline = $instance[ 'headline' ];
        $text = $instance[ 'text' ];

        ?>
        <p>
        <label for="<?php echo $this->get_field_id( 'headline' ); ?>"><?php _e( 'Headline:' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id( 'headline' ); ?>" name="<?php echo $this->get_field_name( 'headline' ); ?>" type="text" value="<?php echo esc_attr( $headline ); ?>" />
        </p>

        <p>
        <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Text:' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" type="text" value="<?php echo esc_attr( $text ); ?>" />
        </p>


        <?php 
    }
}

add_action( 'widgets_init', create_function('', 'return register_widget("kevintext");') );


?>

2 Answers 2

1

You are not passing your description correctly. Really, you aren't passing it at all.

public function __construct() {
    parent::WP_Widget(false, $name='Kevins Textbox');
    array('description' => __('A text widget created by Kevin Ullyott for practice in creating widgets') );
}

That 'description' line is creating an array but you aren't doing anything with it. It is just floating alone in space. You need to pass that to WP_Widget. Follow the example in the Codex:

/**
 * Register widget with WordPress.
 */
public function __construct() {
    parent::__construct(
        'foo_widget', // Base ID
        'Foo_Widget', // Name
        array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args
    );
}

See how ID, name, and the array all get passed to the parent constructor? Compare that to your code, where the array is outside the closing bracket of WP_Widget.

Calling WP_Widget is about the same as calling __construct-- the first passes everything to the second. WP_Widget is a pre-PHP5 style constructor as noted in the source.

Also, no need to assign the name to a variable here, $name='Kevins Textbox'. You can't 'name' variables when you pass them like you can in Python.

So, what you need is this:

public function __construct() {
    parent::WP_Widget(
    // or parent::__construct(
        false, 
        'Kevins Textbox',
        array(
            'description' => __('A text widget created by Kevin Ullyott for practice in creating widgets') 
        )
    );
    ;
}

You need WP_Query or get_posts to get your posts.

$fiveposts = get_posts(array('numberposts' => 5);
var_dump($fiveposts); // should show you what you have to work with.

Hope that helps.

1

You can set description of a widget like this:

Create a __construct function.

Then create variable for widgets options like Description, class, id of the widget.

    function __construct() {
        $widget_ops = array(
                'description' => __('Your Widget description goes here'),
                'classname' => 'your_widget_class',
                'id' => 'your_widget_id'
            );
        parent::__construct('your_widget_id_backend', 'Your Widget Title', $widget_ops);
    }

Call parent class (WP_Widget) construct function.

parent::_construct passes 3 parameters. First parameter passes the html widget id, which should display on backend under Widget section. Second parameter passes the widget title and Third parameter passes the array of options you set earlier with variable $widget_ops.

To get your most recent posts. You can try this.

$loop = new WP_Query('showposts=5&order=DESC');

while($loop->have_posts()) : $loop->the_post();
    the_title();
endwhile;

The above code will output 5 recent posts title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.