1

Just updated another Wordpress site from PHP 7.4 to 8.1 and needed to update the deprecated create_function. Here's the old code:

add_filter( 'loop_shop_per_page', create_function( '$cols', 'return '.$sp_products_per_page.';' ), 20 );

I updated it to:

add_filter( 'loop_shop_per_page', function($cols) { return $sp_products_per_page; }, 20);

But it still doesn't seem to be working correctly. Is this syntax correct?

1 Answer 1

3

From the two functions you have, I assume it's a scope issue.

$sp_products_per_page is declared/exists on a global scope, and because you use an anonymous function, it's not accessible from the function scope.
So instead of

add_filter( 'loop_shop_per_page', function($cols) { return $sp_products_per_page; }, 20);

You'll need to add a use keyword

add_filter( 'loop_shop_per_page', function($cols) use ($sp_products_per_page) { return $sp_products_per_page; }, 20);

A bit of formatting and we have this

add_filter( 'loop_shop_per_page', function($cols) use ($sp_products_per_page) {
    return $sp_products_per_page;
}, 20);
Sign up to request clarification or add additional context in comments.

Comments

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.