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
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 |
<?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
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
<?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
1 2 3 4 5 6 7 8 |
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