2

Trying to create my first plugin. A simple one. It's doing mainly what I want to do except the widget itself in Appearance-->Widgets doesn't display my fields. Wondering if anyone could point me in the right direction to make my fields appear?

function form($instance) {
    $title = esc_attr($instance['title']); ?>
    <p><label for="<?php echo $this->get_field_id('title'); ?>">
    <?php _e('Title:'); ?>     
    <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" 
    name="<?php echo $this->get_field_name('title'); ?>" type="text" 
    value="<?php echo $title; ?>" /></label></p>
<?php } }
  • Are you getting errors? You seem to have an extra } – shanebp Apr 4 '15 at 21:27
  • No I'm not. Right at the end? Is that where this extra character is? – Adam Bell Apr 5 '15 at 1:22
  • I tried to delete the extra } but all that did was make my code invalid. Probably because I had this at the end: add_action('widgets_init', create_function('', 'return register_widget("thisWidget");')); – Adam Bell Apr 5 '15 at 2:43
  • Never ever use create_function(), it is a huge security risk and should be avoided ar all times. Use proper closures. – Pieter Goosen Apr 5 '15 at 4:38
2

With your code given in OP, it is hard to exactly tell you where you are going wrong. One note on what I can, you should never use create_function(), it is a security thread that can be exploited by hackers. You should avoid using that completely. Rather make use of proper closures that was introduced in PHP 5.3.

To help you, here is a basic skeleton which you can use to build a widget with. Just one note, this requires PHP 5.4 due the the use of the new short array syntax ([])

class Custom_Services extends WP_Widget 
{

    public function __construct() 
    {
        parent::__construct(
            'widget_custom_service', 
            _x( 'Custom Service Widget', 'Custom Service Widget' ), 
            [ 'description' => __( 'Displays information from a custom service.' ) ] 
        );
        $this->alt_option_name = 'widget_custom_service';

        add_action( 'save_post', [$this, 'flush_widget_cache'] );
        add_action( 'deleted_post', [$this, 'flush_widget_cache'] );
        add_action( 'switch_theme', [$this, 'flush_widget_cache'] );
    }

    public function widget( $args, $instance ) 
    {
        $cache = [];
        if ( ! $this->is_preview() ) {
            $cache = wp_cache_get( 'widget_services', 'widget' );
        }

        if ( ! is_array( $cache ) ) {
            $cache = [];
        }

        if ( ! isset( $args['widget_id'] ) ) {
            $args['widget_id'] = $this->id;
        }

        if ( isset( $cache[ $args['widget_id'] ] ) ) {
            echo $cache[ $args['widget_id'] ];
            return;
        }

        ob_start();

        $title          = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Category Posts' );
        /** This filter is documented in wp-includes/default-widgets.php */
        $title          = apply_filters( 'widget_title', $title, $instance, $this->id_base );

        // ADD YOUR CUSTOM PHP CODE HERE FOR EXECUTION TO DISPLAY ON FRONT END

        echo $args['after_widget']; 

        if ( ! $this->is_preview() ) {
            $cache[ $args['widget_id'] ] = ob_get_flush();
            wp_cache_set( 'widget_services', $cache, 'widget' );
        } else {
            ob_end_flush();
        }
    }

    public function update( $new_instance, $old_instance ) 
    {
        $instance                   = $old_instance;
        $instance['title']          = strip_tags( $new_instance['title'] );
        $this->flush_widget_cache();

        $alloptions = wp_cache_get( 'alloptions', 'options' );
        if ( isset($alloptions['widget_custom_service']) )
            delete_option('widget_custom_service');

        return $instance;
    }

    public function flush_widget_cache() 
    {
        wp_cache_delete('widget_services', 'widget');
    }

    public function form( $instance ) 
    {

        $title      = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        ?>

        <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 $title; ?>" />
        </p>

    <?php
    }

}

add_action( 'widgets_init', function () 
{
    register_widget( 'Custom_Services' );
});
| improve this answer | |
  • Thanks! Took some trial and error but I got it! – Adam Bell Apr 6 '15 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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