Support » Developing with WordPress » body_class array to string causing error

  • Resolved apt2it


    If I add body_class filter to my theme, few other plugins cause error as follows.

    Fatal error: Uncaught Error: [] operator not supported for strings

    My code:

    function BodyClass(array $classes){    
        $classes[]  = 'scroller';    
        return $classes;
    apply_filters( 'body_class', 'BodyClass');

    I reviewed the plugin code, the code use similar to my code above, to add their classes. But the other plugin code receive $classes as string instead of array.

    – If I disable my filter, they work correctly.
    – any error_log is not logging anything, so assuming its not called at all before the error is raised.

    Any guidance is appreciated.

    Below is call stack.

    Call stack:
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter apt2it


    My WordPress version is 5.8.2

    Hey @apt2it,

    You’re trying to use add_filter instead?

    add_filter( 'body_class', 'BodyClass');

    You’re using apply_filters(). The parameter $classes to the body_class filter is an array. But you’re overriding the filter and passing a string ‘BodyClass’. So, when other plugins use this filter, they’re getting this string instead of the $classes array. Hence the error. The ‘BodyClass’ you’re using is not a callback function but a string parameter.

    Thread Starter apt2it


    Oops…! you caught me napping!!

    Thank you Sanjeev.

    The issue is solved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.