2

Here is the specification for the filter for the wp_get_attachment_url filter:

/**
 * Filter the attachment URL.
 *
 * @since 2.1.0
 *
 * @param string $url     URL for the given attachment.
 * @param int    $post_id Attachment ID.
 */

And this is my attempted filter:

function mfeu_filter_attachment_url( $url, $post_id ) {
  return (get_post_meta($post_id,'_mfeu_external_url',true) != '') ? get_post_meta($post_id,'_wp_attached_file',true) : $url;
}
add_filter('wp_get_attachment_url','mfeu_filter_attachment_url');

I am consistently getting the following errors:

[24-Feb-2016 21:15:55 UTC] PHP Warning:  Missing argument 2 for mfeu_filter_attachment_url() in .../wp-content/plugins/media-external-url/media-external-url.php on line 20
[24-Feb-2016 21:15:55 UTC] PHP Notice:  Undefined variable: post_id in .../wp-content/plugins/media-external-url/media-external-url.php on line 21

It seems as though the filter isn't meant to accept two arguments, even though the description in the core WordPress file post.php makes it seem so! My filter needs to be able to know which attachment it's dealing with in order to get the attachment's meta and use that to make a decision on how to return the url. Can anybody see what's wrong?

1 Answer 1

4

I think you're missing the number of callback input arguments, so try this one instead:

add_filter( 'wp_get_attachment_url','mfeu_filter_attachment_url', 10, 2 );
                                                                  |  |
                                                 Priority    _____|  |
                                      Number of arguments    ________|

The default is 10, 1, so you're currently only passing on the $url and not the second $post_id argument.

1
  • Perfect. Exactly what I was looking for. Didn't even think to look in the docs for add_filter. Thank you! Commented Feb 24, 2016 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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