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?
If this is really your code:
The reason it doesn’t work is because of a misplaced parenthesis. Try this: