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.

I'm getting an php-error on an server that runs php version 5.2.17 which doesn't support anonymous functions. I've never heard of that before (really new to php) but found out that only php 5.3.x supports the following code:

 add_action('init', function() use($name, $args) {  

Is there any way to solve that problem? It works fine on version 5.3.10 but I jhave to use an old host not using php 5.3...

This is the full code to create the custom posts:

/* Add Post Type */
function add_post_type($name, $args = array() ) {   
    if ( !isset($name) ) return;

    $name = strtolower(str_replace(' ', '_', $name));

    add_action('init', function() use($name, $args) {   
        $args = array_merge(
            array(
                'label' => 'Members ' . ucwords($name) . '',
                'labels' => array('add_new_item' => "Add New $name"),
                'singular_name' => $name,
                'public' => true,
                'supports' => array('title', 'editor', 'comments'),
            ),
            $args
        );

        register_post_type( $name, $args);
    });
}


add_post_type('Netherlands', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));
share|improve this question
    
are you basing it on this: wp.tutsplus.com/tutorials/creative-coding/… ? –  janw Jun 4 '12 at 19:15
    
No, the code doesn't come from that article. Found it on the Wordpress forums. –  Niels Pilon Jun 4 '12 at 20:34
add comment

1 Answer

up vote 3 down vote accepted

You have to rewrite your code without closures. It could look like this:

function wpse54191_plugin_init() {
    add_post_type('Netherlands', array(
        'supports' => array('title', 'editor', 'thumbnail', 'comments')
    ));
}
add_action('init', 'wpse54191_plugin_init');

/* Add Post Type */
function add_post_type($name, $args = array() ) {   
    if ( !isset($name) ) return;

    $name = strtolower(str_replace(' ', '_', $name));
    $args = array_merge(
        array(
            'label' => 'Members ' . ucwords($name) . '',
            'labels' => array('add_new_item' => "Add New $name"),
            'singular_name' => $name,
            'public' => true,
            'supports' => array('title', 'editor', 'comments'),
        ),
        $args
    );

    register_post_type( $name, $args);
}
share|improve this answer
    
+1 Anonymous functions inside action hooks is generally frowned upon. Great rewrite. –  EAMann Jun 4 '12 at 19:29
    
Thanks a lot, that resolved the PHP-error! I'm only having some problems displaying the custom posts now but I'll look into that. –  Niels Pilon Jun 4 '12 at 20:28
add comment

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.