search

Home  >  Q&A  >  body text

I get an error in WordPress function when I filter the page's alias

<p>This works fine on the store page but throws an error on other pages. </p> <p>This has to do with if(is_page('shop')). </p> <p><br /></p> <pre class="brush:html;toolbar:false;">function divi_engine_body_class( $classes ) { if(is_page('shop')) { $classes[] = 'woocommerce-page-SGARAGLINO'; return $classes; } } add_filter('body_class','divi_engine_body_class',99999);</pre> <p><br /></p> <p>Error message: PHP Fatal Error: Uncaught TypeError: array_unique(): Parameter #1 ($array) must be of type array, in /wordpress/core/6.2.2/wp-includes/post-template.php: Null is given at 861. </p>
P粉946336138P粉946336138547 days ago518

reply all(1)I'll reply

  • P粉269530053

    P粉2695300532023-08-18 14:05:48

    This function only returns the $classes variable if is_shop() is true, when in fact it should always return the $classes variable:

    function divi_engine_body_class( $classes ) {
       if ( is_page( 'shop' ) ) {
            $classes[] = 'woocommerce-page-SGARAGLINO';
        }
    
        return $classes;
    }
    

    Filters must always return a value; if they do not return a value, an error will almost always be raised.

    reply
    0
  • Cancelreply