0

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.

1 Answer 1

0

I'm not sure if you are missing some code in the constructor where you write //code here :

function __construct() {
    //code here
 }

Try replacing it with:

function __construct() {
    parent::__construct(
       'byad_countdown_widget', // Base ID
        __('Byad Countdown', 'byad_countdown'), // Name
       array( 'description' => __( 'The Byad Countdown Widget', 'byad_countdown' ), ) // Args
    );
}

Your updated code example gives the following output:

<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>

on my 3.8.1 install with the TwentyTwelve theme.

So the filter seems to be working.

9
  • Thank you for you answer. I didn't put my code of the __construct() function just to be concise. I've edited my question so you see the code.
    – anou
    Mar 18, 2014 at 7:54
  • I checked your updated code and the filter worked, see the generated HTML above.
    – birgire
    Mar 18, 2014 at 8:14
  • Thanks for your testing, I'm glad it worked. Now I have to find why it doesn't on my website :-)
    – anou
    Mar 19, 2014 at 8:56
  • Maybe you got some extra byad_title filter call-backs in functions.php from your testing ? Did you try this on the default theme?
    – birgire
    Mar 19, 2014 at 9:04
  • Any debug news?
    – birgire
    Apr 5, 2014 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.