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

I'm writing a plugin using OOP. I realise that filters and action hooks need to be placed inside the constructor method in WordPress like so:

add_action('wp_enqueue_scripts', array($this, 'add_js_css'));

As far as I understand, it should be the same for filters:

add_filter('attachment_fields_to_edit', array($this, 'attachment_fields', 15, 2));

The attachment_fields() method should return an array (I've tested it outside of the class and it works), but it seems to return null when called inside the class. I'm guessing it's because it's called inside of the class, but I'm not sure what to do about it!

If it's useful, the method looks like this:

public function attachment_fields($form_fields) {
    global $post;
    $file = wp_get_attachment_url($post->ID);
    $form_fields['url']['label'] = 'URL';
    $form_fields['url']['html'] = "<input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($file) . "' /><br />";
    $form_fields['buttons'] = array(
    'label' => '',
    'value' => '',
    'html' => "<input type='submit' class='button' name='send[$post->ID]' value='" . esc_attr__( 'Add MP3' ) . "' />",
    'input' => 'html'
    return $form_fields;

Any advice?

share|improve this question

If this is really your code:

add_filter('attachment_fields_to_edit', array($this, 'attachment_fields', 15, 2));

The reason it doesn't work is because of a misplaced parenthesis. Try this:

add_filter('attachment_fields_to_edit', array($this, 'attachment_fields'), 15, 2);
share|improve this answer

Might be caused by the missing $post parameter in your attachment_fields method. I realize you're doing global $post, but attachment_fields_to_edit does expect 2 arguments...

Try changing your method to:

public function attachment_fields($form_fields, $post) {

(I'm using attachment_fields_to_edit in a class, and it works for me).

Edit: Also, I've always used a reference to $this when hooking in OOP:

add_filter( 'attachment_fields_to_edit', array ( &$this, 'attachment_fields', 15, 2 ) );
share|improve this answer
Hi @goto10! Thanks, but that shouldn't (and unfortunately doesn't) make a difference :( Does your code look basically the same as mine? – Richard Sweeney Jul 5 '12 at 18:19
I can't answer my own question, so I'll put this here. I solved the problem by removing the filter priority, so: add_filter('attachment_fields_to_edit', array($this, 'attachment_fields')); works, though I'm not sure why. Incidentally, I always learnt to use a reference to $thisin WordPress too, but it doesn't seem to make a difference. – Richard Sweeney Jul 5 '12 at 18:37
Hi Richard! Hrmm... Yeah mine is similar. I'm using priority 11 instead of 15 (don't think that matters), and my class is being instantiated on the plugins_loaded hook (also shouldn't matter, and I'm using that hook for unrelated reasons). Oh, I see you've solved it now. Goody! :D – goto10 Jul 5 '12 at 18:38
Ahh, I guess the reference to $this was a PHP 4 thing. Source: stackoverflow.com/questions/5622573/… – goto10 Jul 5 '12 at 18:44
Good to know @goto10 - cheers! – Richard Sweeney Jul 6 '12 at 6:42

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.