Twitter je mezi uživateli stále více oblíbený a to reflektují i tvůrci WordPress šablon a proto se v každé lepší šabloně zobrazují poslední příspěvky ze zvoleného twitter účtu. Dnes si ukážeme, jak pro naše potřeby vytvořit widget, který můžeme umístit do sidebaru a zobrazíme zvolený počet tweetů. Popis widgetu poněkud skrátím, pokud si chcete přečíst, jak posutpovat krok za krokem při vytváření widgetu, odkazuji na článek http://musilda.cz/prihlasovaci-formular-jako-widget-do-sidebaru/.
Vytvoříme si soubor tripodion-twitter-widget.php a do něj vložíme hlavičku:
/*
-----------------
Plugin Name: Tripodion Twitter Widget
Plugin URI:
Description: A widget that displays messages from twitter.com
Version:
Author:
Author URI:
----------------
*/ |
Zaregistrujeme si widget a vytvoříme třídu.
add_action( 'widgets_init', 'Tripodion_twitter_load_widget' ); // Register widget function Tripodion_twitter_load_widget() { register_widget( 'Tripodion_Twitter_Widget' ); } // Widget class class Tripodion_Twitter_Widget extends WP_Widget { } |
Přidáme konstruktor:
function Tripodion_Twitter_Widget() { /* Widget settings. */ $widget_ops = array( 'classname' => 'tripodion_twitter_widget' , 'description' => __( 'Twitter Widget' , 'simple' ) ); /* Widget control settings. */ $control_ops = array( 'width' => 200, 'height' => 350, 'id_base' => 'tripodion_twitter_widget' ); /* Create the widget. */ $this->WP_Widget('tripodion_twitter_widget', __( 'Tripodion : Twitter Widget' , 'simple' ) , $widget_ops, $control_ops ); } |
Přidáme zobrazení widgetu:
function widget( $args, $instance ) { extract( $args ); $title = apply_filters('widget_title', $instance['title'] ); $user_name = $instance['user_name']; $count_message = $instance['count_message']; echo $before_widget; if ( $title ) echo $before_title . $title . $after_title; ?> <script type="text/javascript"> jQuery.noConflict()(function($){ $(document).ready(function() { $(".tweet").tweet({ count: <?php echo $instance['count_message']; ?>, username: "<?php echo $instance['user_name']; ?>", loading_text: "loading twitter...", avatar_size: 32 }); }); }); </script> <div class="tweet"></div> <?php echo $after_widget; } |
Update widgetu:
function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['user_name'] = stripslashes( $new_instance['user_name']); $instance['count_message'] = stripslashes( $new_instance['count_message']); return $instance; } |
Formulář widgetu:
function form( $instance ) { $defaults = array( 'title' => __( 'From Twitter' , 'simple' ), 'user_name' => 'VladaMusilek', 'count_message' => '3', ); $instance = wp_parse_args( (array) $instance, $defaults ); ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"> <?php _e( 'Title:', 'simple' ); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'user_name' ); ?>"> <?php _e( 'User Name:' , 'simple'); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id( 'user_name' ); ?>" name="<?php echo $this->get_field_name( 'user_name' ); ?>" value="<?php echo stripslashes(htmlspecialchars(( $instance['user_name'] ), ENT_QUOTES)); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'count_message' ); ?>"> <?php _e( 'The Number of Displayed Messages:' , 'simple' ); ?> </label> <input class="widefat" id="<?php echo $this->get_field_id( 'count_message' ); ?>" name="<?php echo $this->get_field_name( 'count_message' ); ?>" value="<?php echo stripslashes(htmlspecialchars(( $instance['count_message'] ), ENT_QUOTES)); ?>" /> </p> <?php } |
A to je vše. Ať slouží.