I'm on a plugin, where I want to provide a way to my clients to filter data before saving. I know the best way is to provide a filter hook. So I introduced a new filter hook to filter all the post data, including post data and post meta data.
But then I found that, there's already a hook available for post data. So I realized that, it's unnecessary to introduce another hook to filter post data.
Now I'm wondering is there any such hook available for post meta data? If so, then I won't need to provide any new filter hook, I can direct my users to those filter hooks from the core.
I found an article by Igor Benic about metadata filter hook. I tried with the following code and some of its variances:
function wpse22728_meta($check, $object_id, $meta_key, $meta_value, $unique) {
if( get_post_type($object_id) == 'mycpt' ) {
if( $meta_key == 'mycustomkey' ) {
// var_dump($meta_value); //value going to store is: 'old_value'
return $meta_value = 'new_value';
}
}
}
add_filter('add_post_metadata', 'wpse22728_meta', 20, 5);
add_filter('update_post_metadata', 'wpse22728_meta', 20, 5);
But actually failed.
How can I filter meta data before storing them? Is there any other hook available?