Title: widget cannot call add_filter
Last modified: August 20, 2016

---

# widget cannot call add_filter

 *  [adrianboston](https://wordpress.org/support/users/adrianboston/)
 * (@adrianboston)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/widget-cannot-call-add_filter/)
 * im trying to alter content on posts by way of a widget.
    the widget has a section
   in a sidebar that displays categories. and a section on each post.
 * all works except add_filter is not fired, when placed INSIDE the class. why inside
   the class? i need a boolean on whether to fire or not. that flag is a widget 
   control option, selected by the user. (outside of course, it is just fired as
   expected)
 *     ```
       class XXX extends WP_Plugin {
             function widget( $args, $instance ) {
   
       	   if ($instance['displayonpost'])
       	                add_filter( 'the_category', 'filter_the_category');
   
                  //display categories on widget side
   
             }
       }
   
       //outside of class, but should be fine
       function filter_the_category() {
       ...
       }
       ```
   
 * one possibility. seems like the above WP_Widget::widget function is called late
   in the WP sequence.
    i have tried placing that add_filter call in the WP_Widget::
   WP_Widget constructor but the instance properties are not set.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [adrianboston](https://wordpress.org/support/users/adrianboston/)
 * (@adrianboston)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/widget-cannot-call-add_filter/#post-2394379)
 * ive thought about trying to load it early but that seemed to fail..
    aka
 *     ```
       add_action( 'plugins_loaded', create_function( '', 'register_widget("XXX");' ) );
       ```
   
 *  Thread Starter [adrianboston](https://wordpress.org/support/users/adrianboston/)
 * (@adrianboston)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/widget-cannot-call-add_filter/#post-2394431)
 * or get the widget instances from the widgetfactory
 *  Thread Starter [adrianboston](https://wordpress.org/support/users/adrianboston/)
 * (@adrianboston)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/widget-cannot-call-add_filter/#post-2394456)
 * ok, i used options
    options are set in the update function of the Plugin class
 *     ```
       class XXX extends WP_Plugin {
   
             function update( $new_instance, $old_instance ) {      /* Save the form changes here */
       	   $hooks = strip_tags( $new_instance['hook'] );
       	   $opts = array('hooks'=>$hooks);
       	   update_option('unique_option_nameXX', $opts);//put hooks into database
             }
       }
   
       //elsewhere in the main file, above or below. no matter.
   
       if ( is_active_widget(false, false, 'widgetname', true) ) {
          $options = get_option('unique_option_nameXX');//returns options.
          if ($options) {
             $post_hooks = explode(',',$options['hooks']);//comma separated values, if any                            
   
             foreach ($post_hooks as $hook)
             {
       	 if ($hook && function_exists($hook)) {
       	    add_filter($hook,  'filter_callback');
       	 }
             }
          }
       ```
   
 * function filter_callback($content)
    { //do whatever to content return $content;}
 * that is messy but does it.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘widget cannot call add_filter’ is closed to new replies.

## Tags

 * [add_filter](https://wordpress.org/support/topic-tag/add_filter/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 1 participant
 * Last reply from: [adrianboston](https://wordpress.org/support/users/adrianboston/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/widget-cannot-call-add_filter/#post-2394456)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
