在插件/窗口小部件类中使用APPLY_FILTER

时间:2014-03-16 作者:anou

我很难找出我在使用apply_filter() 在小部件类中。

以下是我的一些代码示例:

class Byad_CountDown_Widget extends WP_Widget {
  function __construct() {
    parent::__construct(
      \'byadcd_widget\',  // Base ID of widget
      __(\'BYAD Countdown Widget\', \'byad-countdown\'), // Widget name will appear in UI
      // Widget description & class
      array( 
        \'description\' => __( \'Widget for displaying a countdown (Years (opt.), Days, Hours, Minutes, Seconds)\', \'byad-countdown\' ), 
        \'classname\' => \'byad-countdown\',
      ) 
    );

  }

  public function widget( $args, $instance ) {

    $title = \'<span class="byad-title">\' . $instance[\'title\'] . \'</span><div class="arrow-container"><div class="arrow-up gray"></div><div class="arrow-up white"></div></div>\';
    $title = apply_filters( \'byad_title\', $title );//Try to add the possibility of changing the widget title via add_filter()

    // before and after widget arguments are defined by themes
    echo $args[\'before_widget\'];

    // some code to implement some vars

    if ( is_active_widget( false, false, $this->id_base, true ) ) {
      wp_enqueue_script( \'byad-countdown\', plugins_url( \'js/byad-countdown.js\' , __FILE__ ), array(\'jquery\'), \'1.0\', true );

      $params = apply_filters(\'byad_jsdata\', array(
        \'byadClass\' => \'countdown-display\',
        \'byadImg\' => \'/byad-countdown/images/skull.png\',
        \'byadAlt\' => \'.\',
        \'year\' => __(\'Years\', \'byad-countdown\'),
        \'month\' => __(\'Months\', \'byad-countdown\'),
        \'day\' => __(\'Days\', \'byad-countdown\'),
        \'hour\' => __(\'Hours\', \'byad-countdown\'),
        \'min\' => __(\'Minutes\', \'byad-countdown\'),
        \'sec\' => __(\'Seconds\', \'byad-countdown\'),
      ) ); //Try to modify the array passed to the JS file

      wp_localize_script( \'byad-countdown\', \'jbydCD_Data\', $params );
    }

    echo \'<div class="countdown-display"  data-root="\' . plugins_url() . \'"></div>\';

    if ( ! empty( $title ) ) {
      echo $args[\'before_title\'] . $title . $args[\'after_title\'];
    }

    echo $args[\'after_widget\'];
  }

  // Widget Backend 
  public function form( $instance ) {
    if ( isset( $instance[ \'title\' ] ) ) {
      $title = $instance[ \'title\' ];
    }
    else {
      $title = __( \'My Default title\', \'byad-countdown\' );
    }
    //here goes Widget admin form
  }

  // Updating widget replacing old instances with new
  public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
    return $instance;
  }
} // Class Byad_Countdown_Widget ends here


// Register and load the widget
function byad_load_widget() {
    register_widget( \'Byad_Countdown_Widget\' );
}
add_action( \'widgets_init\', \'byad_load_widget\' );
如您所见,我已经两次使用apply\\u filter来钩住传递给我的小部件的变量:byad_titlebyad_jsdata.

但在我的主题中function.php 文件I执行以下操作:

function my_title($title){
  $title = \'you know what?\';
  return $title;
}
add_filter(\'byad_title\',\'my_title\');
即使我更改了add_filter 作用

我在寻找任何建议,因为在运行了网络和不同的答案和可能性之后,我还没有成功地让它发挥作用。

1 个回复
最合适的回答,由SO网友:birgire 整理而成

我不确定您编写的构造函数中是否缺少一些代码//code here :

function __construct() {
    //code here
 }
尝试将其替换为:

function __construct() {
    parent::__construct(
       \'byad_countdown_widget\', // Base ID
        __(\'Byad Countdown\', \'byad_countdown\'), // Name
       array( \'description\' => __( \'The Byad Countdown Widget\', \'byad_countdown\' ), ) // Args
    );
}
更新后的代码示例给出了以下输出:

<aside id="byadcd_widget-2" class="widget byad-countdown">
    <div class="countdown-display"  data-root="http://example.com/wp-content/plugins"></div>
    <h3 class="widget-title">you know what?</h3>
</aside>
在我的3.8.1 使用“TwentyEleven”主题安装。

所以过滤器似乎在工作。

结束