WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;

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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
Perfect. Exactly what I was looking for. Didn't even think to look in the docs for add_filter. Thank you! – Samuel Reid Feb 24 at 22:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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