Title: body_class array to string causing error
Last modified: January 5, 2022

---

# body_class array to string causing error

 *  Resolved [apt2it](https://wordpress.org/support/users/apt2it/)
 * (@apt2it)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/body_class-array-to-string-causing-error/)
 * 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';    
           error_log(gettype($classes));
           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:
   
       qi_addons_for_elementor_add_mobile_class()
       wp-includes/class-wp-hook.php:303
       WP_Hook::apply_filters()
       wp-includes/plugin.php:189
       apply_filters()
       wp-content/themes/sanp22/inc/theme-support.php:525
       require()
       wp-content/themes/sanp22/functions.php:15
       include()
       wp-settings.php:546
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [apt2it](https://wordpress.org/support/users/apt2it/)
 * (@apt2it)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/body_class-array-to-string-causing-error/#post-15219788)
 * My WordPress version is 5.8.2
 *  [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/body_class-array-to-string-causing-error/#post-15220476)
 * Hey [@apt2it](https://wordpress.org/support/users/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](https://developer.wordpress.org/reference/hooks/body_class/)
   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](https://wordpress.org/support/users/apt2it/)
 * (@apt2it)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/body_class-array-to-string-causing-error/#post-15222528)
 * Oops…! you caught me napping!!
 * Thank you Sanjeev.
 * The issue is solved.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘body_class array to string causing error’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [apt2it](https://wordpress.org/support/users/apt2it/)
 * Last activity: [4 years, 5 months ago](https://wordpress.org/support/topic/body_class-array-to-string-causing-error/#post-15222528)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
