0

Possible Duplicate:
How can I use PHP closure function like function() use() on PHP 5.2 version?

I'm trying to run this on a server that's running php 5.2.

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

    // execute custom post type code here

});
};

The 2nd line is throwing an unexpected T_FUNCTION error, I suspect its cause of the "use" operator. Can someone help point me as to how I can rewrite this function to run in php 5.2?

2
  • I guess it is because you haven't specified curly braces after function(). but what actually you are trying to do? Commented Aug 11, 2012 at 19:32
  • @Hafiz I'm making a custom post type helper function to put in my functions.php file in my wordpress theme. Makes making CPTs faster and more convenient. Followed this from a tutorial a while ago. As for te curly braces, it comes after the "use($name,$args)" as that statement as i understand it makes the variables available to the function below. anyway it runs fine on my MAMP which has PHP 5.3. my production has 5.2 Commented Aug 11, 2012 at 20:40

2 Answers 2

1

See this function:-

/* Add Post Type */

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome this looks good, will try it right away
0

This answer seems to provide a good solution for what you're trying to do in PHP 5.2: converting anonymous functions to user-defined functions.

Converting Code with Anonymous functions to PHP 5.2

Good luck! And try and upgrade your PHP version :P

1 Comment

Thanks, looks like i have to catch up on that topic. Im on a shared hosting package without access to the WHM or shell, so I dont think I can upgrade it myself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.