come-creare-widget-personalizzato-per-wordpress

Come creare un widget per wordpress, #wordpress #widget #comecreare #howmake

Indice

Come creare un widget per WordPress

Per evitare che terze parti (i nostri clienti) possano compromettere le funzionalità del nostro tema per wordpress, vedremo come creare un widget per wordpress

Esempio di widget base

<?php
class myWidget extends WP_Widget {

    function myWidget() {
    }
    function widget( $args, $instance ) {
    }
    function update( $new_instance, $old_instance ) {
    }
    function form( $instance ) {
    }
}
function my_register_widgets() {
    register_widget( 'myWidget&#039; );
}
add_action( 'widgets_init', 'my_register_widgets' );
?>

Widget Completo

Il codice sottostante va inserito nel file functions.php del nostro tema.Il widget che andremo a creare è un semplice widget con un titolo e stampa un semplice Hello World,  sarà poi visibile dall’apposito pannello di WordPress e trascinabile in qualsivoglia sidebar!

<?php
class myWidget extends WP_Widget {
    function myWidget() {
        parent::__construct( false, 'Il mio Widget&' );
    }

    function widget( $args, $instance ) {
        extract($args);
        echo $before_widget;
        echo $before_title.$instance['title'].$after_title;

        //DA QUI INIZIA IL WIDGET VERO E PROPRIO
        echo &quoquot;Ciao WordPress&quot;;
        //FINE WIDGET

        echo $after_widget;
    }
    function update( $new_instance, $old_instance ) {
        return $new_instance;
    }
    function form( $instance ) {
        $title = esc_attr($instance['title']); <?php
        $title = esc_attr($instance['title']); ?>
        Titolo: <?php echo $title; ?>
<?php     } } function my_register_widgets() {     register_widget( 'myWidget' ); }
add_action( 'widgets_init', 'my_register_widgets' ); ?>