Created
April 17, 2013 00:13
-
-
Save JustinSainton/5400735 to your computer and use it in GitHub Desktop.
Here's the gist of it (buh dun dun pssssh) - The '_wpsc_product_metadata' meta key is an array of metadata. There's a key in that array that I always want to return false ( '0' in this case). I'd have thought that filtering get_post_metadata, removing the filter, grabbing the value, modifying and returning would do the trick. It doesn't. Always …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'get_post_metadata', 'zao_edit_per_product_shipping', 10, 4 ); | |
function zao_edit_per_product_shipping( $check, $object_id, $meta_key, $single ) { | |
if ( '_wpsc_product_metadata' === $meta_key && has_filter( 'get_post_metadata', 'zao_edit_per_product_shipping' ) ) { | |
remove_filter( 'get_post_metadata', 'zao_edit_per_product_shipping', 10, 4 ); | |
$value = get_post_meta( $object_id, $meta_key, $single ); | |
$value['no_shipping'] = '0'; | |
add_filter( 'get_post_metadata', 'zao_edit_per_product_shipping', 10, 4 ); | |
return $value; | |
} | |
return $check; | |
} |
Also, note the special conditions inside get_metadata
around the return of $check
. From core:
if ( null !== $check ) {
if ( $single && is_array( $check ) )
return $check[0];
else
return $check;
}
So rather than returning $value
, we should be returning array( $value )
otherwise, since $value
is an associative array, $check[0]
will have issues ...
@ericmann - thanks for all the points of clarity. All of that is a bit secondary, as it doesn't really affect the output. Even just running the remove_filter() line and then return get_post_meta([...] - that returns null - which seems as if it's not removing the action properly.
Hmm, actually, I think your point about the associative array is probably the issue...
Freaking @ericmann. Works beautifully. Hugs are on the way.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basically it's just a null variable - get_metadata checks to see if you've filtered it to be anything but null - if you have, it returns your special value, if it's null, it continues on with the meta cache bucket check in get_metadata()