Costruire un widget

  mie note, plugin wordpress, wordpress
Titoli in questa pagina:
Un semplice widget - Un widget per visualizzare i feed

programmare wordpress

Un semplice widget

Per creare un widget si deve estendere la casse di wordpress WP_Widget utilizzare l’hook widgets_init e registrarlo con register_widget

<?php

class My_Widget extends WP_Widget {

	function __construct() {

		parent::__construct(
			'my-text',  // Base ID
			'My Text'   // Name
		);

		add_action( 'widgets_init', function() {
			register_widget( 'My_Widget' );
		});

	}

	public $args = array(
		'before_title'  => '<h4 class="widgettitle">',
		'after_title'   => '</h4>',
		'before_widget' => '<div class="widget-wrap">',
		'after_widget'  => '</div></div>'
	);

	public function widget( $args, $instance ) {

		echo $args['before_widget'];

		if ( ! empty( $instance['title'] ) ) {
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
		}

		echo '<div class="textwidget">';

		echo esc_html__( $instance['text'], 'text_domain' );

		echo '</div>';

		echo $args['after_widget'];

	}

	public function form( $instance ) {

		$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'text_domain' );
		$text = ! empty( $instance['text'] ) ? $instance['text'] : esc_html__( '', 'text_domain' );
		?>
		<p>
		<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php echo esc_html__( 'Title:', 'text_domain' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'Text' ) ); ?>"><?php echo esc_html__( 'Text:', 'text_domain' ); ?></label>
			<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" type="text" cols="30" rows="10"><?php echo esc_attr( $text ); ?></textarea>
		</p>
		<?php

	}

	public function update( $new_instance, $old_instance ) {

		$instance = array();

		$instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
		$instance['text'] = ( !empty( $new_instance['text'] ) ) ? $new_instance['text'] : '';

		return $instance;
	}

}
$my_widget = new My_Widget();
?>

Un widget per visualizzare i feed

<?php

// usa l'azione widgets_init hook per eseguire la funzione personalizzata

add_action( 'widgets_init', 'boj_awe_register_widgets' );

//registra il nostro widget

function boj_awe_register_widgets() {

register_widget( 'boj_awe_widget');

}

//boj_widget_my_info class

class boj_awe_widget extends WP_Widget {

//process the new widget

    function __construct () {

        $widget_ops = array(

        'classname' => 'boj_awe_widget_class',

        'description' => 'Display an RSS feed with options.'

        );

        //$this->WP_Widget( 'boj_awe_widget', 'Advanced RSS Widget', $widget_ops );
        parent::__construct('boj_awe_widget', 'Advanced RSS Widget', $widget_ops );
    }
//build the widget settings form

function form($instance) {

    $defaults = array(

        'title' => 'RSS Feed',

        'rss_feed' => 'http://strangework.com/feed',

        'rss_items' => '2'

    );

    $instance = wp_parse_args( (array) $instance, $defaults );

    $title = $instance['title'];

    $rss_feed = $instance['rss_feed'];

    $rss_items = $instance['rss_items'];

    $rss_date = $instance['rss_date'];

    $rss_summary = $instance['rss_summary'];

?>
    <p>Title: <input class="widefat" name=" <?php echo $this->get_field_name( 'title' ); ?>" 
        type="text" value="<?php echo esc_attr( $title ); ?>" /></p>

    <p>RSS Feed: <input class="widefat" name="<?php echo $this->get_field_name( 'rss_feed' ); ?>" 
        type="text" value="<?php echo esc_attr( $rss_feed ); ?>" /></p>

    <p>Items to Display:

        <select name="<?php echo $this->get_field_name('rss_items' ); ?>">

        <option value="1" <?php selected( $rss_items, 1 ); ?>>1</option>

        <option value="2" <?php selected( $rss_items, 2 ); ?>>2</option>

        <option value="3" <?php selected( $rss_items, 3 ); ?>>3</option>

        <option value="4" <?php selected( $rss_items, 4 ); ?>>4</option>

        <option value="5" <?php selected( $rss_items, 5 ); ?>>5</option>
    </select>

    </p>

    <p>Show Date?: <input name=" <?php echo $this->get_field_name( 'rss_date' ); ?>"
        type="checkbox" <?php checked( $rss_date, 'on' ); ?> /></p>

    <p>Show Summary?: <input name=" <?php echo $this->get_field_name( 'rss_summary' ); ?>"
        type="checkbox" <?php checked( $rss_summary, 'on' ); ?> /></p>

    <?php

}

//save the widget settings

function update($new_instance, $old_instance) {

    $instance = $old_instance;

    $instance['title'] = strip_tags( $new_instance['title'] );
    $instance['rss_feed'] = strip_tags( $new_instance['rss_feed'] ); 
    $instance['rss_items'] = strip_tags( $new_instance['rss_items'] );

    $instance['rss_date'] = strip_tags( $new_instance['rss_date'] ); 

    $instance['rss_summary'] = strip_tags( $new_instance['rss_summary'] );

    return $instance;

}

//display the widget

function widget($args, $instance) {

    extract($args);
    
    echo $before_widget;

    //load the widget settings

    $title = apply_filters( 'widget_title', $instance['title'] );

    $rss_feed = empty( $instance['rss_feed'] ) ? '' : $instance['rss_feed'];

    $rss_items = empty( $instance['rss_items'] ) ? 2 : $instance['rss_items'];

    $rss_date = empty( $instance['rss_date'] ) ? 0 : 1;

    $rss_summary = empty( $instance['rss_summary'] ) ? 0 : 1;

    if ( !empty( $title ) ) { 
        echo $before_title . $title . $after_title; 
    };

    if ( $rss_feed ) {

    //display the RSS feed

        wp_widget_rss_output( array(

            'url' => $rss_feed,

            'title' => $title,

            'items' => $rss_items,

            'show_summary' => $rss_summary,

            'show_author' => 0,
            'show_date' => $rss_date

            ) );

    }

    echo $after_widget;

}
}
?>

I Feed di un sito wordpress, si possono visualizzare con nomesito.com/feed

Per inserire un widget utilizzare la bacheca di wordpress Aspetto-> Widget e nella barra di ricerca ricercare il nome dato al plugin all’interno del construttore.
Per inserire informazioni del widget nella dashboard di wordpress, utilizzare il seguente codice

function boj_dashboard (  ) {
    wp_add_dashboard_widget( 'dashboard_custom_feed', 'Mio dashboard personalizzato', 'boj_dashboard_example_display'); 
}

function boj_dashboard_example_display(){ 
    echo '<p>Contatta support@example.com per segnalare bug.</p>';

} 

Se si vuole far personalizzare il widget salvando delle opzioni add_option utilizzare un quarto parametro al wp_add_dashboard_widget

SCRIVI UN COMMENTO