• 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

    (@adrianboston)

    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

    (@adrianboston)

    or get the widget instances from the widgetfactory

    Thread Starter adrianboston

    (@adrianboston)

    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.