我很难找出我在使用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_title
和
byad_jsdata
.
但在我的主题中function.php
文件I执行以下操作:
function my_title($title){
$title = \'you know what?\';
return $title;
}
add_filter(\'byad_title\',\'my_title\');
即使我更改了
add_filter
作用
我在寻找任何建议,因为在运行了网络和不同的答案和可能性之后,我还没有成功地让它发挥作用。