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.