-1

this old php script I pasted from the internet is no longer supported. create_function no longer works and I would like to replace it. However I'm not capable of finding a modern solution. My PHP skills are too bad to even understand how this used to work. Does anyone know a quick fix? I would greatly appreciate it!

//Gets post cat slug and looks for single-[cat slug].php and applies it
add_filter('single_template', create_function(
    '$the_template',
    'foreach( (array) get_the_category() as $cat ) {
        if ( file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php") )
        return TEMPLATEPATH . "/single-{$cat->slug}.php"; }
    return $the_template;' )
);
Foolix
  • 3
  • 4

1 Answers1

1

This type of conversion is actually extremely simple because the strings involved are constants (note the single quotes). So, there is nothing going in or out of the function except its parameters.

That means that what you have is a normal, ordinary function.

So:

$newFunction = function($the_template) {
    foreach((array)get_the_category() as $cat) {
        if (file_exists(TEMPLATEPATH . "/single-{$cat->slug}.php")) {
            return TEMPLATEPATH . "/single-{$cat->slug}.php";
        }
    }
    return $the_template;
};

add_filter('single_template', $newFunction);

I think this should work too (only minimal changes):

$newFunction = function($theTemplate) {
    foreach(get_the_category() as $cat) {
        $php = TEMPLATEPATH . "/single-{$cat->slug}.php";
        if (file_exists($php)) {
            return $php;
        }
    }
    return $theTemplate;
};

Update

You do not even need to declare the function, but can use an anonymous function: notice that the anonymous body is simply the second parameter to the old create_function, while the first parameter specified the anonymous parameters.

add_filter(
    'single_template',
    function($theTemplate) {
         foreach(get_the_category() as $cat) {
         $php = TEMPLATEPATH . "/single-{$cat->slug}.php";
         if (file_exists($php)) {
            return $php;
         }
         return $theTemplate;
    }
);
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • Dear LSerni, thank you very much for your comment This was extremely helpful for me! Bless you :) - (For all of those who wonder - The second code snippet works, when one adds this at the end: add_filter('single_template', $newFunction); – Foolix Jan 07 '22 at 23:22
  • I have another piece of code where the function occurs. Could you please help me translate this as well? `add_action( 'widgets_init', create_function('', 'return register_widget("FreigeistRandomGalleryWidget");') ); ` – Foolix Jan 08 '22 at 21:02
  • @Foolix it is the same principle; this newFunction2() simply takes no parameters. The body of the function is the second string. When the arguments to create_functions are single-quoted strings the conversion is straightforward. – LSerni Jan 08 '22 at 22:18
  • thanks for commenting. I made this out of it but it won't function. What am I missing? `add_action( 'widgets_init', function() { return register_widget("FreigeistRandomGalleryWidget"); } );` PS: I appreciate your help very much! – Foolix Jan 09 '22 at 13:36
  • @Foolix you did it exactly right. It *should* work. Unless maybe the `register_widget` function relies on the common scope...? But that would be crazy. Is this by any chance WordPress? Wouldn't it be better to update the WordPress installation to the newest version? – LSerni Jan 09 '22 at 14:54
  • Ok... It returns a "Fatal error: Uncaught Argument". You wouldn't know why that could be, would you? (Thank you!) – Foolix Jan 09 '22 at 16:51
  • I'm afraid not. I'd have to look at the software to be able to make a guess. I suspect the error is actually *not* in that point, but somewhere else nearby. – LSerni Jan 09 '22 at 17:42