Estructura básica de WordPress Plugin
WordPress Plugin puede consistir de un solo archivo PHP sin embargo, para la consistencia del mismo es recomendable crear por lo menos un único archivo PHP dentro del él. La carpeta y el archivo deben tener el mismo nombre con la excepción del archivo con extensión. Vamos a llamar a nuestro plugin «Mi primer plugin».
Folder: mi-primer-plugin
Archivo: mi-primer-plugin.php
Conceptos básicos de un WordPress Plugin
No hay duda interminable que se podría entrar todas las funciones en un solo archivo, pero voy a cubrir solo las necesidades básicas para poder empezar.
Encabezado
Aca es donde iniciamos el complemento dentro de WordPress.
1 2 3 4 5 6 7 8 9 10 | /* Plugin Name: Nombre del plugin Plugin URI: http://friki.pe/ Description: Descripcion del plugin Author: FeCoRI Author URI: http://friki.pe/ Version: 1.0 Text Domain: text-domain License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ |
Armando nuestro primer WordPress Plugin
Hay muchas maneras de crear un WordPress Plugin, recomiendo la orientada a objetos es mas rápida y mas entendible (te ayuda a ser mas ordenado).
Crearemos nuestra clase llamada «miPrimerPlugin».
1 2 3 | class miPrimerPlugin { } |
Agregamos el constructor.
1 2 3 4 5 6 7 8 | class miPrimerPlugin { public function __construct() { } } |
Agregamos el iniciador del WordPress Plugin.
1 2 3 4 5 6 7 8 | class miPrimerPlugin { public function __construct() { } } |
Como ahora estamos haciendo un ejemplo, vamos a comenzar con un widget asi que extenderemos nuestra clase a WP_Widget.
1 2 3 4 5 6 7 8 | class miPrimerPlugin extends WP_Widget { public function __construct() { } } |
Iniciamos con el plugin
Agregaremos las funciones «form», «update», «widget» a nuestra clase.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class miPrimerPlugin extends WP_Widget { public function __construct() { } //Funcion del formulario que ira dentro del widget function form($instance) { } //Funcion para actualizar nuestro widget function update($new_instance, $old_instance) { } //Funcion para mostrar el widget function widget($args, $instance) { } } |
Comenzaremos agregando 3 campos: Titulo, Text, Textarea, dentro de nuestra función «form».
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | //Funcion del formulario que ira dentro del widget function form($instance) { if ($instance) { $title = esc_attr($instance['title']); $text = esc_attr($instance['text']); $textarea = esc_textarea($instance['textarea']); } else { $title = ''; $text = ''; $textarea = ''; } ?> <p> <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Titulo Widget', 'wp_widget_plugin'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>"/> </p> <p> <label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Text:', 'wp_widget_plugin'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" type="text" value="<?php echo $text; ?>"/> </p> <p> <label for="<?php echo $this->get_field_id('textarea'); ?>"><?php _e('Textarea:', 'wp_widget_plugin'); ?></label> <textarea class="widefat" id="<?php echo $this->get_field_id('textarea'); ?>" name="<?php echo $this->get_field_name('textarea'); ?>"><?php echo $textarea; ?></textarea> </p> <?php } |
Ahora en nuestra función «update» le decimos que actualice los datos del widget.
1 2 3 4 5 6 7 8 9 10 | //Funcion para actualizar nuestro widget function update($new_instance, $old_instance) { $instance = $old_instance; // Fields $instance['title'] = strip_tags($new_instance['title']); $instance['text'] = strip_tags($new_instance['text']); $instance['textarea'] = strip_tags($new_instance['textarea']); return $instance; } |
En nuestra ultima función «widget», mostramos todo lo que ingresamos en «form».
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | //Funcion para mostrar el widget function widget($args, $instance) { extract($args); //Obtenemos los 3 campos del widget $title = apply_filters('widget_title', $instance['title']); $text = $instance['text']; $textarea = $instance['textarea']; echo $before_widget; // Mostramos el widget echo '<div class="widget-text wp_widget_plugin_box">'; // Check if title is set if ($title) { echo $before_title . $title . $after_title; } // Revisamos si se ingresaron datos en el campo de Text if ($text) { echo '<p class="wp_widget_plugin_text">' . $text . '</p>'; } // Revisamos si se ingresaron datos en el campo de Textarea if ($textarea) { echo '<p class="wp_widget_plugin_textarea">' . $textarea . '</p>'; } echo '</div>'; echo $after_widget; } |
Resultado final
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | /* Plugin Name: Nombre del plugin Plugin URI: http://friki.pe/ Description: Descripcion del plugin Author: FeCoRI Author URI: http://friki.pe/ Version: 1.0 Text Domain: text-domain License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ class miPrimerPlugin extends WP_Widget { //Iniciamos nuestro plugin public function __construct() { parent::WP_Widget(false, $name = __('Mi Widget', 'wp_widget_plugin')); } //Funcion del formulario que ira dentro del widget function form($instance) { if ($instance) { $title = esc_attr($instance['title']); $text = esc_attr($instance['text']); $textarea = esc_textarea($instance['textarea']); } else { $title = ''; $text = ''; $textarea = ''; } ?> <p> <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Titulo Widget', 'wp_widget_plugin'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>"/> </p> <p> <label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Text:', 'wp_widget_plugin'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" type="text" value="<?php echo $text; ?>"/> </p> <p> <label for="<?php echo $this->get_field_id('textarea'); ?>"><?php _e('Textarea:', 'wp_widget_plugin'); ?></label> <textarea class="widefat" id="<?php echo $this->get_field_id('textarea'); ?>" name="<?php echo $this->get_field_name('textarea'); ?>"><?php echo $textarea; ?></textarea> </p> <?php } //Funcion para actualizar nuestro widget function update($new_instance, $old_instance) { $instance = $old_instance; // Fields $instance['title'] = strip_tags($new_instance['title']); $instance['text'] = strip_tags($new_instance['text']); $instance['textarea'] = strip_tags($new_instance['textarea']); return $instance; } //Funcion para mostrar el widget function widget($args, $instance) { extract($args); //Obtenemos los 3 campos del widget $title = apply_filters('widget_title', $instance['title']); $text = $instance['text']; $textarea = $instance['textarea']; echo $before_widget; // Mostramos el widget echo '<div class="widget-text wp_widget_plugin_box">'; // Check if title is set if ($title) { echo $before_title . $title . $after_title; } // Revisamos si se ingresaron datos en el campo de Text if ($text) { echo '<p class="wp_widget_plugin_text">' . $text . '</p>'; } // Revisamos si se ingresaron datos en el campo de Textarea if ($textarea) { echo '<p class="wp_widget_plugin_textarea">' . $textarea . '</p>'; } echo '</div>'; echo $after_widget; } } add_action('widgets_init', create_function('', 'return register_widget("miPrimerPlugin");')); |
Espero que les haya sido de utilidad y ¡OJO! que hay mas metodos a utilizar con WordPress les recomendaria revisar el Codex (ohh gran codex!!!)