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;
}
}