WordPress自定义小工具(Widget )开发实例

这是一段有详细注释的WordPress自定义小工具(Widget )开发实例代码,可用于制作主题时集成自定义小工具,将代码添加到主题functions.php中,具体效果如图:

  1. <?php
  2. add_action( \’widgets_init\’, \’example_load_widgets\’ );
  3.  example_load_widgets() {
  4.     register_widget( \’Example_Widget\’ );
  5. }
  6.  Example_Widget  WP_Widget {
  7.     
  8.      Example_Widget() {
  9.         
  10.          = ( \’classname\’ => \’example\’, \’description\’ => __(\’An example widget that displays a person\\\’s name  sex.\’, \’example\’) );
  11.         
  12.          = ( \’width\’ => 300, \’height\’ => 350, \’id_base\’ => \’example-widget\’ );
  13.         
  14.         ->WP_Widget( \’example-widget\’, __(\’Example Widget\’, \’example\’), ,  );
  15.     }
  16.     
  17.      widget( ,  ) {
  18.         extract(  );
  19.         
  20.          = apply_filters(\’widget_title\’, [\’title\’] );
  21.          = [\’name\’];
  22.          = [\’sex\’];
  23.          = isset( [\’show_sex\’] ) ? [\’show_sex\’] : false;
  24.         
  25.          ;
  26.         
  27.          (  )
  28.               .  . ;
  29.         
  30.          (  )
  31.             printf( \'<p>\’ . __(\’Hello. My name is %1.\’, \’example\’) . \'</p>\’,  );
  32.         
  33.          (  )
  34.             printf( \'<p>\’ . __(\’I am a %1.\’, \’example.\’) . \'</p>\’,  );
  35.         
  36.          ;
  37.     }
  38.     
  39.      update( ,  ) {
  40.          = ;
  41.         
  42.         [\’title\’] = ( [\’title\’] );
  43.         [\’name\’] = ( [\’name\’] );
  44.         
  45.         [\’sex\’] = [\’sex\’];
  46.         [\’show_sex\’] = [\’show_sex\’];
  47.          ;
  48.     }
  49.     
  50.      form(  ) {
  51.         
  52.          = ( \’title\’ => __(\’Example\’, \’example\’), \’name\’ => __(\’John Doe\’, \’example\’), \’sex\’ => \’male\’, \’show_sex\’ => true );
  53.          = wp_parse_args( () ,  ); ?>
  54.         <!– Widget Title: Text Input –>
  55.         <p>
  56.             <label =><?php _e(\’Title:\’, \’hybrid\’); ?></label>
  57.             <input id= name= value= style= />
  58.         </p>
  59.         <!– Your Name: Text Input –>
  60.         <p>
  61.             <label =><?php _e(\’Your Name:\’, \’example\’); ?></label>
  62.             <input id= name= value= style= />
  63.         </p>
  64.         <!– Sex: Select Box –>
  65.         <p>
  66.             <label =><?php _e(\’Sex:\’, \’example\’); ?></label>
  67.             <select id= name= = style=>
  68.                 <option <?php  ( \’male\’ == [\’format\’] )  \’selected=\’; ?>>male</option>
  69.                 <option <?php  ( \’female\’ == [\’format\’] )  \’selected=\’; ?>>female</option>
  70.             </select>
  71.         </p>
  72.         <!– Show Sex? Checkbox –>
  73.         <p>
  74.             <input = type= <?php checked( [\’show_sex\’], true ); ?> id= name= />
  75.             <label =><?php _e(\’Display sex publicly?\’, \’example\’); ?></label>
  76.         </p>
  77.     <?php
  78.     }
  79. }
  80. ?>

原文:http://justintadlock.com/

    © 版权声明
    THE END
    喜欢就支持一下吧
    点赞0分享
    评论 抢沙发

    请登录后发表评论

      暂无评论内容