I'm having difficulties to find out what I'm doing wrong in my use of apply_filter() in a widget class.
Here's some samples of my code:
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' );
As you can see I've made twice the use of apply_filter to be able to hook into the variable passed to my widget: byad_title and byad_jsdata.
But when in my theme function.php file I do the following:
function my_title($title){
$title = 'you know what?';
return $title;
}
add_filter('byad_title','my_title');
Nothing happen, even if I change the priority of the add_filter function
I'm looking for any advice, because after running the net and different answers and possibilities, I haven't had any success to make it work.