Come Creare un Plugin WordPress
1.489 visiteCreare un Plugin WordPress
Introduzione
Un plugin wordpress è un una estenzione di wordpress che aggiunge funzionalità al CMS.
Esistono migliaia di plugin di tutti i tipi che permettono di fare praticamente qualsiasi cosa e di trasformare WordPress da un semplice CMS in un e-commerce oppure da un Blog ad un vero e proprio portale.
Un plugin può modificare un comportamento di WordPress sia lato front-end che back-end e, soprattutto, quando si tratta di modificarne la parte di visualizzazione, generalmente esso si traduce nella creazione di un Widget che verrà inserito dall’utente nei Placeholder disponibili nella sezione Widget del pannello di amministrazione.
1. Creare un Plugin WordPress: Directory
Per prima cosa andiamo nella directory plugins dentro alla directory wp-content di WordPress:
1 |
/wp-content/plugins/ |
e creiamo la directory del nostro plugin che abbia un nome unico.
Nel nostro esempio creeremo un widget che contiene un campo di test e stampi il testo “Benenuto in geeknews.it” sulla parte front-end del placeholder in verrà inserito.
Il nome del plugin sarà: “print_geeknews_it_text”. Quindi il path completo del nostro plugin sarà:
1 |
/wp-content/plugins/print_geeknews_it_text/ |
2.Creare un Plugin WordPress: Il File contenente la classe
Ora creiamo nella directory del nostro plugin il file print_geeknews_it_text.php e inseriamo una intestazione di base necessaria per far capire a WordPress che è un plugin. Questa intestazione conterrà alcune informazioni importanti del plugin come il nome la versione e l’autore:
1 2 3 4 5 6 7 8 9 |
/* Plugin Name: Print GeekNews.it Text Plugin URI: https://www.geeknews.it Description: Stampa a video il testo GeekNews.it Version: 0.1 Author: https://www.geeknews.it/ Author URI: https://www.geeknews.it/ License: GPL2 */ |
Ecco come si presenterà nella lista dei plugin:
Dopodichè creiamo la classe PrintGeekNewItText che dovrà estendere WP_Widget di WordPress.
In questo modo erediteremo alcuni metodi di cui faremo l’ “Override” e sono:
- il metodo widget usato per inizializzare il widget.
- il metodo form usato per creare la form di amministrazione che conterrà i parametri di configurazione.
- il metodo update usato durante l’aggiornamento dei parametri di configurazione.
Quindi la classe diventa:
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 |
class PrintGeekNewItText extends WP_Widget { //Costruttore function __construct() { // Impostazioni del widget $widget_ops = array('classname' => 'PrintGeekNewItText', 'description' => __( 'Stampa a video il testo GeekNews.it')); // Impostazioni del componente backend del Widget $control_ops = array( 'width' => 500, 'height' => 350, 'id_base' => 'print-geeknews-it-text' ); // Creazione del Widget parent::WP_Widget('print-geeknews-it-text', 'Print Geeknew.it Text', $widget_ops, $control_ops ); } //Distruttore public function __destruct() { } //Metodo Override widget chiamato quando il widget è attivo function widget($args, $instance) { if (isset($instance['text'])&&!empty($instance['text'])) echo $instance['text']; } //Metodo Override update chiamato quando aggiorni i paramtetri del widget function update( $new_instance, $old_instance ) { //Aggiorno il parametro text unico parametro del widget (in questo caso) $instance['text'] = strip_tags( stripslashes($new_instance['text']) ); return $new_instance; } function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'text' => '' ) ); $text = $instance['text']; if (!isset($text)&&empty($text)) $text='Benvenuti su GeekNews.it'; ?> <input type="text" class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" value="<?php echo $text; ?>"/> <?php } } |
Come si può notare il nostro parametro di configurazione è “text” e il valore è contenuto nella variabile $instance[‘text’].
Adesso attiviamo il plugin inserendo il seguente codice PHP:
1 2 3 4 5 6 7 |
function PrintGeekNewItTextInit() { if ( !is_blog_installed() ) return; register_widget('PrintGeekNewItText'); } add_action('widgets_init', 'PrintGeekNewItTextInit', 2); |
L’ “add_action” è una funzione di wordpress usata per sfruttare gli Hooks di cui parleremo nel dettaglio in un altro articolo.
Il codice completo è il seguente:
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 |
<?php /* Plugin Name: Print GeekNews.it Text Plugin URI: https://www.geeknews.it Description: Stampa a video il testo GeekNews.it Version: 0.1 Author: https://www.geeknews.it/ Author URI: https://www.geeknews.it/ License: GPL2 */ class PrintGeekNewItText extends WP_Widget { //Costruttore function __construct() { // Impostazioni del widget $widget_ops = array('classname' => 'PrintGeekNewItText', 'description' => __( 'Stampa a video il testo GeekNews.it')); // Impostazioni del componente backend del Widget $control_ops = array( 'width' => 500, 'height' => 350, 'id_base' => 'print-geeknews-it-text' ); // Creazione del Widget parent::WP_Widget('print-geeknews-it-text', 'Print Geeknew.it Text', $widget_ops, $control_ops ); } //Distruttore public function __destruct() { } //Metodo Override widget chiamato quando il widget è attivo function widget($args, $instance) { if (isset($instance['text'])&&!empty($instance['text'])) echo $instance['text']; } //Metodo Override update chiamato quando aggiorni i paramtetri del widget function update( $new_instance, $old_instance ) { //Aggiorno il parametro text unico parametro del widget (in questo caso) $instance['text'] = strip_tags( stripslashes($new_instance['text']) ); return $new_instance; } function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'text' => '' ) ); $text = $instance['text']; if (!isset($text)&&empty($text)) $text='Benvenuti su GeekNews.it'; ?> <input type="text" class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" value="<?php echo $text; ?>"/> <?php } } function PrintGeekNewItTextInit() { if ( !is_blog_installed() ) return; register_widget('PrintGeekNewItText'); } add_action('widgets_init', 'PrintGeekNewItTextInit', 2); ?> |
Dopo aver attivato il plugin entriamo nella sezione widget e vedremo il nostro plugin:
E inserito in un placeholder avremo: