Forums

widget cannot call add_filter (4 posts)

  1. adrianboston
    Member
    Posted 6 months ago #

    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.

  2. adrianboston
    Member
    Posted 6 months ago #

    ive thought about trying to load it early but that seemed to fail..
    aka

    add_action( 'plugins_loaded', create_function( '', 'register_widget("XXX");' ) );
  3. adrianboston
    Member
    Posted 6 months ago #

    or get the widget instances from the widgetfactory

  4. adrianboston
    Member
    Posted 6 months ago #

    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.

Reply

You must log in to post.

About this Topic