0

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')
));
3

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);
}
  • +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

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.