NielsPilon
NielsPilon

Reputation: 500

unexpected T_FUNCTION with php 5.2.17 but fine on localhost and php 5.3.10

I"m getting an unexpected T_FUNCTION php error after uploading my Wordpress files to a server running php version 5.2.17.

The theme works fine on localhost (with MAMP) and there are also no errors on my own server which runs php version 5.3.10.

What can be wrong or what can be done to solve this error?

This is the line that causes the error:

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

And the entire functions.php file looks like this:

<?php 

/* 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')
));


add_post_type('Belgium', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Germany', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('France', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('United-Kingdom', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Ireland', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Spain', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Portugal', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

    add_post_type('Italy', array(
    'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

I'm really new to php and only use it for Wordpress theming. Any help is really appreciated.

Upvotes: 3

Views: 1932

Answers (3)

Mike B
Mike B

Reputation: 32145

add_action()'s second parameter is of type callback.

Pre 5.3, this is usually a string representing a function:

add_action('init', 'myFunction');

function myFunction() { echo 'init'; }

There are alternatives such as create_function and other syntaxes to use when dealing with objects.

5.3 onward, anonymous functions are allowed:

add_action('init', function() { echo 'init'; });

Upvotes: 1

fullpipe
fullpipe

Reputation: 366

anonymous functions available from PHP 5.3.0

Upvotes: -1

Naftali
Naftali

Reputation: 146302

You cannot have anonymous functions in PHP less than 5.3

Rework your code so that it does not involve anonymous functions and it should work on your older server.

Upvotes: 7

Related Questions