3

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);
    unset($form_fields['post_excerpt']);
    unset($form_fields['post_content']);
    unset($form_fields['url']['helps']);
    $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?

1

If this is really your code:

<?php
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:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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