0

I have a plugin I have developed for a client which works fun on my server but doesn't work on theirs, they are on godaddy so I am guessing it's an issue with their PHP version?

add_filter('template_include', function ( $template ) {
 if(is_tax('hha_cats')){
  $template = dirname( __FILE__ ) . '/templates/category-hhavideo.php';
 }
 if( is_post_type_archive( 'hhavideo' ) ){
  $template = dirname( __FILE__ ) . '/templates/archive-hhavideo.php';
 }
 return $template;
 }, PHP_INT_MAX, 2 );

This code was written by another SE user after I was having another issue (here), is there a way to make this work with older versions of PHP? (Without checking, I believe her server is 5.2 and mine is 5.3).

2
  • just don't use anonymous functions, they have a very niche usefulness and are bad for readability and maintenance of your code. Commented Dec 11, 2014 at 5:37
  • @MarkKaplun I agree, but they are still very useful IMO when used correctly. Commented Dec 11, 2014 at 6:12

2 Answers 2

1

EDIT

Undeleted my answer as the code seems to have helped the OP. Originally got beaten by @TomJNowell, so some content might be the same

ORIGINAL ANSWER

This is more PHP related than Wordpress. Anonymous functions, the syntax used in the code given, was only introduced in PHP 5.3 and will not work in older versions. If you are sure that your client is using PHP 5.2, it is most probably the reason for the error.

You can just make a slight modification to the code to get it to work on older versions

add_filter('template_include', 'my_function_name', PHP_INT_MAX, 2 );
function my_function_name( $template ) {
 if(is_tax('hha_cats')){
  $template = dirname( __FILE__ ) . '/templates/category-hhavideo.php';
 }
 if( is_post_type_archive( 'hhavideo' ) ){
  $template = dirname( __FILE__ ) . '/templates/archive-hhavideo.php';
 }
 return $template;
 } 
0
1

PHP 5.2 is very old, as is 5.3, both of you should upgrade. Especially your client on 5.2. Aside from major security fixes and massive performance gains, PHP 5.3 introduced closures and anonymous functions, e.g.:

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

The above code is valid in PHP 5.3, and will generate syntax errors in 5.2, which is why your code, which uses an anonymous function, fails on your clients server.

For your code to be 5.2 compatible, you will need to define the function separately and pass in its name rather than defining it in place.

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

However, instead, I strongly recommend you both update to PHP 5.5 for security reasons or find a host with at least PHP 5.4. At the time of writing PHP 5.6 is the latest version, and PHP 5.2, and 5.3 are no longer supported or maintained.

Sidenote: Your code uses an anonymous function in a filter, which can be troublesome to debug and to remove. e.g. what do you pass into remove_action if the function has no name?

3
  • I just checked and my server is running 5.5, I believe the clients is 5.2 as it's godaddy and encountering a few issues. Pieter's post (which has now been deleted) fixed my issue, but upvoted yours for the helpful reply. Commented Dec 10, 2014 at 11:13
  • It is possible to change your PHP version on GoDaddy, as implied by this forum posts, but failing that, contacting godaddy support should help
    – Tom J Nowell
    Commented Dec 10, 2014 at 11:45
  • I've incorporated some of Pieters answer into mine
    – Tom J Nowell
    Commented Dec 10, 2014 at 11:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.