0

I'm a newbie in WordPress and PHP. I created a small widget to embed video's from YouTube or other video hosting. Everything works fine.

I know I have messy code, therefore I would appreciate it if someone is able to tidy up my code.

This is my code:

<?php
/**
 * Plugin Name: Widget Name
 * Author: Author Name
 * Author URI: Site URL
 */

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 );
    }

    /**
     * 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' ] );
            }
        }

        /* Before widget (defined by themes). */
        if( isset( $before_widget ) ) {
        echo $before_widget;
        }

        /* Display the widget title if one was input (before and after defined by themes). */
        if( isset( $title ) ) { 
        echo $before_title . $title . $after_title; 
        }

        /* Display the widget video */
        if( isset( $template ) ) { 
        echo '<div class="tm_today_video"> '.$template.' </div>';
        }

        /* After widget (defined by themes). */
        echo $after_widget;

    }


    /**
     * 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="112" src="http://www.youtube.com/embed/Xrt1V_XhvSk?showinfo=0" 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%;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'template' ); ?>"><?php _e('Embed Code:', 'hybrid'); ?></label>
            <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" );' ) );
?>

Thank you.

3
  • You should hire a developer if you want someone else to do your work...this forum is to get answers to your questions. Jun 10, 2013 at 15:31
  • Dear Brian Fegter, I create this question because i need the answers. I just want to learn how to make a widget. I'm a newbie in WordPress. I think wordpress.stackexchange.com is the right forum for it. I can't find others forum like wordpress.stackexchange.com.
    – v123shine
    Jun 10, 2013 at 20:37
  • You should probably reformat your 'question' so it becomes an actual question. To 'tidy' up code isn't a question. A more relevant approach to this question would be to address an area you aren't familiar with and ask in specificity. Jun 11, 2013 at 2:27

1 Answer 1

0

Your Class Name:

  1. I'd prefix the class name to make collisions less likely-- something like Your_Initials_Today_Video, or namespace it, but be aware that PHP namespacing requires a higher versions of PHP than WordPress requires. That could cause issues.

Your Constructor:

  1. You can use __construct instead of Today_video for your constructor. WordPress no longer supports PHP 4, so you shouldn't have to either.

In your widget function:

  1. $instance should always be set. You don't need to check for it explicitly, and even if not set isset( $instance[ 'title' ] ) would still operate correctly and without error. That "wrapper" conditional is unnecessary.

In your form function:

  1. Embedding that markup in the template argument is going to be prone to markup error. I would add a set of individual parameters-- height, width, src, whatever you need-- and generate the iframe dynamically.

While I might do a few things differently, I wouldn't call the code "messy" and I didn't spot any major issues, nor does it trigger any warning or notices.

4
  • Thank you so much s_ha_dum for your advice. If you create the same plugin, may I see the result? Why? Because I want to compare my work with your work, so I can more easily understand how to create a good widget. Forgive me, if I should bother you, but I really appreciate if you want to help me. **sorry about my english language.
    – v123shine
    Jun 10, 2013 at 20:31
  • Dear s_ha_dum, If you create the same plugin, may I see the result?
    – v123shine
    Jun 11, 2013 at 20:07
  • @v123shine : if I have time, I will rewrite it my way but no promises and the changes will not be significant. You did not do a bad job.
    – s_ha_dum
    Jun 11, 2013 at 20:15
  • Thank you so much s_ha_dum. I'm so sorry if i bother you. I hope you can set aside your time to make this small widget :)
    – v123shine
    Jun 11, 2013 at 22:01

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.