1

I'm currently studying WordPress and want to create a custom Post Type and have copied code from a tutorial that I am following. The code in the tutorial works but when I copy/paste it into custom page template, I get the following error:

"Parse error: syntax error, unexpected T_FUNCTION in /home/databasename/public_html/wptheme/demo/wp-content/themes/name/albums.php on line 3"

I have Googled the issue for hours and there are multiple options but the issue is that I am a beginner and don't know exactly what to change. I know that the problem is on line 3 but that's it. Shown below is my code that is located in my custom page template. Any help would be greatly appreciated!

 <?php 

    add_action('init', function() { /*this is line 3*/
    register_post_type( 'album', array(
        'public' => true,
        'label' => 'Album',
        'labels' => array('add_new_item' => 'Add New Album')
        'supports' => array('title', 'editor'),
        'taxonomies' => array('post_tag')
    )); 
});
3
  • 1
    It seems wherever this code is running, they are not using PHP 5.3. Lambda functions are only available since then. Commented Mar 3, 2012 at 11:55
  • You are probably using a version of PHP ( < 5.3) that doesn't yet support anonymous functions using function() {} - you'll have to use create_function() instead or update Commented Mar 3, 2012 at 11:56
  • Could it be that the video tutorial is outdated? If so, what should I change in the code to get it working? Commented Mar 3, 2012 at 11:56

1 Answer 1

0

You are running an outdated version of php, i.e. php <5.3, which does not support anonymous functions which are used in this code.

While you could rewrite the code to use a named function, or create_function, you should really just update your php installation to a version from this decade. php 5.2 (or even older versions) is not supported anymore, and therefore insecure.

Additionally, your code is missing a comma at the end of one of the lines:

    'labels' => array('add_new_item' => 'Add New Album')
//                                                      ^^
Sign up to request clarification or add additional context in comments.

9 Comments

I am testing the site on a live server (Hostgator) and if I'm not misstaken, they use PHP 5.3 but I could be wrong.
For whatever reason, the folks at hostgator run a totally outdated version of php. Here's there help article on how to switch to 5.3. I'd also consider switching hosting to a service that runs supported php versions by default, and allows you to use modern ones like php 5.4.
Thank you Phihag for your help. Could you tell me how it's possible I can use any WordPress Theme on that server but not my own theme?
Sorry Phihag, it has partially worked. I have updated the PHP to 5.3 but I now have a new error after updating: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home/db/public_html/theme/demo/wp-content/themes/name/albums.php on line 8 of the same code shown in my first post. Should I create a new thread or do you have a sollution?
Updated the answer (the code was in the question). In general, feel free to ask questions, as long as every question stands on its own, and includes intention, code, and error message, just like this one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.