0

**I'm sorry about my english.

I'm newbie in wordpress. I try to create a small widget for embed video from youtube or others video hosting. Everything is going well in backend (Wordpress adminsitrator).

This is the print screen >> http://i.imgur.com/jfedV4R.gif

When i publish the widget in my sidebar, the widget does not appear anything. But work fine in my backend.

This is my widget code:

<?php

 /**
 * Today Video Widget Class
 *
 */

class Today_video extends WP_Widget {

    /**
     * Widget setup.
     */
    function Today_video    () {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'tm_widget_today_video', 'description' => __('Display today video on your site', 'today_video') );

        /* Widget control settings. */
        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'today_video' );

        /* Create the widget. */
        $this->WP_Widget( 'today_video' , __( 'Today Video' , 'today_video' ), $widget_ops, $control_ops );
    }

    /**
     * The frontend function
     */
    function widget( $args, $instance ) {
        extract( $args );

        /* Our variables from the widget settings. */
        if(isset($instance))
        {
            if( isset( $instance[ 'title' ] ) ) {
                $title =                apply_filters( 'widget_title', $instance[ 'title' ] );
            }

            if( isset( $instance[ 'template' ] ) ) {
                $template =             htmlspecialchars_decode( $instance[ 'template' ] );
            }
        }
    }
    /**
     * Backend widget settings
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        /* Strip tags for title and name to remove HTML (important for text inputs). */
        $instance['title'] =            strip_tags( $new_instance['title'] );

        // htmlspecialchars to save html markup in database, at frontend we use htmlspecialchars_decode
        $instance['template'] =         htmlspecialchars($new_instance['template']);

        return $instance;
    }

    /**
     * Displays the widget settings controls on the widget panel.
     *
     * Backend widget options form
     */
    function form( $instance ) {
        $defaults = array( 
            'title' => __(  'Today Video', 'today_video'), 
                            'template' => __('<iframe width="200" height="150" src="http://www.youtube.com/embed/tv49TbW5rEg" frameborder="0" allowfullscreen></iframe>', 'today_video'),
                        );

        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:96%;float:right;" />
        </p>

        <p>
            <textarea id="<?php echo $this->get_field_id( 'template' ); ?>" name="<?php echo $this->get_field_name( 'template' ); ?>"  style="width:100%;height:100px;"><?php echo $instance['template']; ?></textarea>
        </p>

    <?php
    }
} //Add function to widgets_init that'll load today_video
add_action( 'widgets_init', create_function( '', 'register_widget( "Today_video" );' ) );
?>

Can someone help me, Please!

1 Answer 1

0

Your widget method does not echo anything. If you want content to show up the widget needs to echo content.

function widget( $args, $instance ) {
    extract( $args );

    /* Our variables from the widget settings. */
    if(isset($instance))
    {
        if( isset( $instance[ 'title' ] ) ) {
            $title =                apply_filters( 'widget_title', $instance[ 'title' ] );
            // try this
            echo $title;
        }

        if( isset( $instance[ 'template' ] ) ) {
            $template =             htmlspecialchars_decode( $instance[ 'template' ] );
            // try this
            echo $template;
        }
    }
    
    
}

That is probably not the format you want but it should display your content. Be aware that widgets support several parameters such as $before_title and after_title. While you don't have to use those, it is generally a good idea.

Reference

http://codex.wordpress.org/Widgets_API

1
  • Solved. Thank you so much s_ha_dum for your help :)
    – v123shine
    Jun 9, 2013 at 14:06

Your Answer

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

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