0

I was trying to pass variables in filtes. This works perfectly for me.

class my_class {
 function my_class() {
    add_filter('wp_list_categories', array(&$this,'cat_count_span'));
 }
function cat_count_span($links) {
   $links = str_replace('</a> (', '</a> <span>', $links);
   $links = str_replace(')', '</span>', $links);
   return $links;
   }
 } 
new my_class();

But now I want to execute the filter based on theme options value. Say I have my theme option value $search = ot_get_option('search_header'); based on this value I want to add filter and also controll the output.Like this way

class my_class {
   function my_class() {
     add_filter('wp_list_categories', array(&$this,'cat_count_span'));
   }
 function cat_count_span($links) {
   $search = ot_get_option('search_header');
   if ($search === 'on') {
   $links = str_replace('</a> (', '</a> <span>', $links);
   $links = str_replace(')', '</span>', $links);
   return $links;
   }
   }
 } 
new my_class();
  • 2
    Move the return outside the conditional? – s_ha_dum Dec 5 '15 at 10:27
1

I'm not very sure why you would want to do this is a class, as you can simply make use of spaghetti filter function here.

If you really need to do this in a class, there are a few issues with your class

  • We are moving towards PHP 7, and WordPress has eventually woke up and also moving toward PHP 7 now with its classes. PHP 4 type constructors died with the dinosaurs and should not be used. Never ever try to support people that are using EOL'ed PHP versions. Don't compromise performance and security to support EOL'ed versions. Note, PHP 4 type constructors are depreciated in PHP 7

Warning

Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.

  • Constructors should do class initialization, and should not perform task. You should not use constructors to add filters or actions. Move that outside your class

  • It is good practice to name public methods public function function_name(), and not just function function_name()

  • In general here, a filter should always return something. Your filter is failing because if your condition fails, your filter don't have a fallback as it returns nothing

CLASS METHOD

You can rewrite your class to something like this: (NOTE: Since we are well passed PHP5.3, it is good to make use of namespacing when constructing classes. There are many good tutorials about this subject)

class MyClass
{
    /**
     * Class instance.
     *
     * @see get_instance()
     * @type object
     */
    protected static $instance = NULL;

    // Access the working instance of the class
    public static function get_instance()
    {
        NULL === self::$instance and self::$instance = new self;
        return self::$instance;
    }

    // Use the __construct() method for constructors
    public function __construct() 
    {
        // Add initialization here. Do not add add_filter/add_action here
    }

    public function CatCountSpan( $links )
    {
        $search = ot_get_option('search_header');

        if ($search === 'on') {
            $links = str_replace('</a> (', '</a> <span>', $links);
            $links = str_replace(')', '</span>', $links);
        }

        // Move the return statement outside the condition
        return $links;
    }
} // end of class

You can then add your filter as follow, outside the class

add_filter(
    'wp_list_categories',
    [MyClass::get_instance(), 'CatCountSpan']
);  

GREAT RESOURCES ON-SITE

NORMAL SPAGHETTI USING CLOSURES

As I said, I do not really the see the usefulness of a class here, I would probably just use a normal filter function

add_filter( 'wp_list_categories', function ( $links )
{
    $search = ot_get_option('search_header');

    if ($search === 'on') {
        $links = str_replace('</a> (', '</a> <span>', $links);
        $links = str_replace(')', '</span>', $links);
    }

    // Move the return statement outside the condition
    return $links;
});

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.