Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm unsuccesfully trying to use the add_{$meta_type}_metadata. This filter is called inside add_metadata function (wp-includes/meta.php:31 in Wordpress 3.4.2). In particular I wanna hook "add_post_metadata" filter among others. My target is to execute custom code to add,update,get custom meta, so I don't break wordpress way to do things.

body of function add_metadata

     function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique =    false) {
[...]
$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key,     
$meta_value, $unique );
if ( null !== $check )
    return $check;

 [...]

$ckeck after filter execution should contain the value I return from the function "filter_negozi_add_metadata"

This is the code I use (inside my plugin):

 add_action('init', array('my_plugin','init'));
class my_plugin
{
 const META_KEY='my_plugin_key';
public static function init()
{
    new self();
}

public function __construct()
{
    $this->add_filters();
}

protected function add_filters()
{       
///function below works
add_filter('get_post_metadata',array(&$this,'filter_negozi_get_metadata'),10,4);
//the one below instead doesn't work 
$ret=add_filter('add_post_metadata',array(&$this,'filter_negozi_add_metadata'),10,5);
//$ret contains true so add_filter works correctly
}
public function filter_negozi_add_metadata($foo=null,$object_id, $object_id, $meta_key, $meta_value, $unique )
{
    if($meta_key==self::META_KEY)
    {
$ret='foo';
    }
    return $ret;
}

}

The really strange fact is that global $wp_filter doesn't contain my function even if $ret is true. Thanks.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Ok I've found the solution... I have had to call add_filter outside my plugin class, because of Wordpress hooks priority. With the actual code, I call add_filter after the 'init' event, that's probably too late. If I move this line of code:

 add_filter('add_post_metadata',array(&$this,'filter_negozi_add_metadata'),10,5);

on top of the file, the hook works like expected. So to recap, this is the wroking code:

 add_action('init', array('my_plugin','init'));
 //filter goes here
 add_filter('add_post_metadata',array('my_plugin','filter_negozi_add_metadata'),10,5);

class my_plugin
{
 const META_KEY='my_plugin_key';
public static function init()
{
    new self();
}

public function __construct()
{
    $this->add_filters();
}

protected function add_filters()
{       
add_filter('get_post_metadata',array(&$this,'filter_negozi_get_metadata'),10,4);


}
public static function filter_negozi_add_metadata($foo=null,$object_id, $meta_key, $meta_value, $unique )
{
    //if condition doesn't match return a null value to allow wordpress to behave in the standard way
    $ret=null;
    if($meta_key==self::META_KEY)
    {
        $ret='foo';
    }
    return $ret;
}

}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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