Skip to content

Instantly share code, notes, and snippets.

@JustinSainton
Created April 17, 2013 00:13
Show Gist options
  • Save JustinSainton/5400735 to your computer and use it in GitHub Desktop.
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 …
<?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;
}
@JustinSainton
Copy link
Author

The get_post_meta() call on Line 11 will also invoke this filter - it would be an infinite loop without removing the filter. The has_filter() call may indeed be unnecessary (added it after continued hair pulling), but removing it doesn't affect the output.

@JustinSainton
Copy link
Author

Also, FWIW, checked the output of remove_filter() to ensure it was successfully removing the filter, and it is.

@pippinsplugins
Copy link

What is $check? I'm not familiar with the get_post_metadata filter.

@ericmann
Copy link

Couple of things:

  1. The has_filter() check is unnecessary
  2. If you're only running on '_wpsc_product_metadata' === $meta_key you should hard-code that in the get_post_meta check.
  3. The way you're trying to set $value['no_shipping'] implies $single will be set to true ... I'd hard-code that as well for clarity
  4. Since the priority is the default (10) there's no reason to specify that or number of args for remove_filter

I would do a check after grabbing $value to make sure it's an array. When $single is set to true, and the meta key doesn't exist, you get back an empty string. Trying to set an array key on a string will err.

If the function is returning null, that means the conditional is failing. There should be no way the return inside the conditional would return null - the get_post_meta check will return either the value or an empty string ...

@JustinSainton
Copy link
Author

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()

@ericmann
Copy link

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 ...

@JustinSainton
Copy link
Author

@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.

@JustinSainton
Copy link
Author

Hmm, actually, I think your point about the associative array is probably the issue...

@JustinSainton
Copy link
Author

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