Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Just wondering what the preferred method of using anonymous functions is.

First I had some code like this:

function page_columns( $columns ) {
    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => 'Title',
        'author' => 'Author',
        'template' => 'Template',
        'date' => 'Date'
    );

    return $columns;
}
add_filter('manage_edit-page_columns', 'page_columns');

But I know WordPress discourages using variables for things that will only be used once so I switched it to

function page_columns( $columns ) {
    return array(
        'cb' => '<input type="checkbox" />',
        'title' => 'Title',
        'author' => 'Author',
        'template' => 'Template',
        'date' => 'Date'
    );
}
add_filter('manage_edit-page_columns', 'page_columns');

But now I'm thinking why even use a function at all?

add_filter('manage_edit-page_columns', function(){
    return array(
        'cb' => '<input type="checkbox" />',
        'title' => 'Title',
        'author' => 'Author',
        'template' => 'Template',
        'date' => 'Date'
    );
});

What is the preferred method? Any reason why? Perhaps update the WordPress Coding Standards?

share|improve this question

2 Answers 2

Use the second option. Adding anonymous functions as filters and actions is highly discouraged!!!

If you add an anonymous function in this fashion, it's impossible to remove later on down the road by other plugins and code. So please, please, please don't do that.

share|improve this answer

As EAMann said usage of anonymous functions prevents further removing of a function you strapping in.

However I would not discourage people from using it but make them aware of what they are doing.

Using an anonymous function is definitely a way to go in case when you need to make an irreversible changes.

share|improve this answer

Your Answer

 
discard

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.