Zen-Dreams

Zen-Dreams.com

Write your own plugin for Wordpress [Chapter 4]

  • Français

As a reminder, this article is the fourth of a series called Write your own plugin for Wordpress

    1. Introduction to plugins
    2. Plugin’s skeletton
    3. Activation, Hooks & Filters
    4. Widgets Definition

      1. Widgets definition

      A widget is, in fact, a plugin that will allow users to display a block on their sidebar. That being said, there are only one action to use in order to create a Widget.

      This action is called widgets_init - therefore you can call it like this : add_action(’widgets_init’, function_name);

      Once you have done this, you will have to register two other functions inside this function_name function only if sidebars are available with your wordpress install.

      Here is some code that can do the trick :

      function function_name() {
      	if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
      		return;
      	register_sidebar_widget(string $name,callback $function);
      	// register_sidebar_widget takes the name of the widget and the function that will be used to display it.
      	register_widget_control(string $name, callback $function2);
      	// register_widget_control takes the name of the widget and the function that will be used to to change options (in the design panel).
      }

      That’s pretty all you need to know for Widgets. All the rest is non widget specific (options, actions and filters, api calls)

      2. Sample Widget

      Here below you will find a sample widget in written in fully object oriented php - not there is no option tab. This widget will only display a Simple Hello World with widget title customisable.

      /*
      Plugin Name: ZenDreams_Sample_Widget
      Plugin URI: http://www.zen-dreams.com/
      Description: Sample Widget
      Version: 1.0
      Author: Anthony PETITBOIS
      Author URI: http://www.zen-dreams.com/
      */
      class myWidget {
      
      	function myWidget() {
      		add_action('widgets_init', array(& $this, 'init_widget'));
      	}
      
      	function init_widget() {
      		if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
      			return;
      		register_sidebar_widget(array('myWidget','widgets'),array(& $this, 'widget'));
      		register_widget_control(array('myWidget', 'widgets'), array(& $this, 'widget_options'));
      	}
      
      	function widget($args) {
      		global $wpdb;
      
      		$WidgetTitle=get_option('mywidget_options');
      		extract($args);
      
      		echo $before_widget.$before_title.$WidgetTitle.$after_title;
      		echo 'Hello World !';
      		echo $after_widget;
      	}
      
      	function widget_options() {
      		if ($_POST['mywidget_options']) {
      			$option=$_POST['mywidget_options'];
      			update_option('mywidget_options',$option);
      		}
      		$option=get_option('mywidget_options');
      		echo '<label for="mywidget_options">Title : <input id="mywidget_options" name="mywidget_options" type="text" value="'.$option.'" /></label>';
      	}
      }
      $myWidgetVariable= new myWidget ();
      By Anthony

      7 Comments

      1. ebouilleur
        Posted 16 September 2008 at 18:49 | Permalink

        Bonjour,

        Avez vous tester votre code?
        J’en doute fortement… => il bug

        :)

      2. Posted 16 September 2008 at 20:17 | Permalink

        Effectivement, si tu te contente de faire un copier/coller du texte, cela ne risque pas de fonctionner. En même temps, il suffisait de transformer le &amp ; en & tout court…

      3. ebouilleur
        Posted 16 September 2008 at 20:20 | Permalink

        Je n’ai pas testé ca, car je me disais que c’était peut etre normal, après avoir fait plusieurs test j’ai laissé tombé

      4. Kitiz
        Posted 17 September 2008 at 9:35 | Permalink

        Bonjour,
        Merci pour ces tutos mais certains points restent flou, est-ce que tu pourrais nous en dire un peu plus stp sur les différentes variables globales qui existent ?

        Et je voulais savoir également si on était obligé de passer par des classes pour le code soit propre ?

        je dois créer un plugin et pour cela, j’ai déjà conçut toutes les pages de façon à ce que je travaille d’abord le code avant de l’intégrer à WP.
        Est-ce que tu as des conseils à me donner par quoi commencer ?

      5. Posted 17 September 2008 at 17:15 | Permalink

        Kitiz,

        Non, tu n’es pas obligé de passer par des classes pour que le code soit propre, un code en fonction peut-être propre. Personellement j’avoue préferer le code en classes.

        Les variables globales, j’y viendrais dans mon prochain tutoriel justement.

        Des conseils pour commencer ? Regarde quelques plugins simples et leur fonctionnement, et surtout, lire, lire et relire la doc pas très bien faite du Codex

      6. Kitiz
        Posted 18 September 2008 at 9:02 | Permalink

        Ok, merci beaucoup.
        Euh, je ne connais pas trop le fonctionnement des classes. Est-ce que tu pourrais nous faire un tuto dessus ? :$

        à quand le prochain tuto parce que j’avoue être assez impatiente étant donné qu’ils sont en français et compréhensible à 100%.

        Encore Merci :-)

      7. Titomus
        Posted 22 September 2008 at 21:18 | Permalink

        Merci Anthony pour ces tutos sur les plugins !

        Moi qui cherchait un nouveau truc pour faire du viral ;)

        Je suis servi, je m’y mets demain :)

        Merci encore

        Jérémy

      Post a Comment

      Your email is never shared. Required fields are marked *

      *
      *