Title: Filter issue
Last modified: April 1, 2020

---

# Filter issue

 *  [TWD](https://wordpress.org/support/users/twd/)
 * (@twd)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/)
 * How can I make this code more robust?
    i.e. so it doesn’t throw an error if the
   ICL_LANGUAGE_CODE constant doesn’t exist. I have used this code snippet on WPML
   sites to attach different font-families depending on the language class attached
   to the <body> tag.
 *     ```
       add_filter('body_class', 'append_language_class');
       function append_language_class($classes){
         $classes[] = ICL_LANGUAGE_CODE;
         return $classes;
       }
       ```
   
    -  This topic was modified 6 years, 2 months ago by [Jan Dembowski](https://wordpress.org/support/users/jdembowski/).

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

 *  [mark l chaves](https://wordpress.org/support/users/mlchaves/)
 * (@mlchaves)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/#post-12611129)
 * Hi [@twd](https://wordpress.org/support/users/twd/),
 * Give something like this a try.
 *     ```
       define('ICL_LANGUAGE_CODE', 'something');
   
       $classes[] = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : false; 
       var_dump($classes); // Dumps 'something'
       ```
   
 *     ```
       // define('ICL_LANGUAGE_CODE', 'something');
   
       $classes[] = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : false; 
       var_dump($classes); // Dumps false
       ```
   
 *  Thread Starter [TWD](https://wordpress.org/support/users/twd/)
 * (@twd)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/#post-12611200)
 * I don’t follow.
 * What about something simple like this?:
 *     ```
       add_filter('body_class', 'append_language_class');
       function append_language_class($classes){
         if(ICL_LANGUAGE_CODE){
           $classes[] = ICL_LANGUAGE_CODE;
           return $classes;
         }
       }
       ```
   
 *  [mark l chaves](https://wordpress.org/support/users/mlchaves/)
 * (@mlchaves)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/#post-12611319)
 * Hi [@twd](https://wordpress.org/support/users/twd/),
 * Your code would throw a warning or error depending on which PHP version used.
 * I suggest you run both my examples and your code as a proof to yourself what 
   will work and what won’t. That’s the beauty of coding–like mathematics you can
   always test your answer 😉
 * Thanks!
 * P.s. Learn more about the `defined` function here [https://www.php.net/manual/en/function.defined.php](https://www.php.net/manual/en/function.defined.php)
 *  Thread Starter [TWD](https://wordpress.org/support/users/twd/)
 * (@twd)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/#post-12611493)
 * Ah thanks. I’m learning already.
    Didn’t realize that there is a different way
   to check for the existence of variables,
 * `isset()`
 * compared to constants
 * `defined()`
 * looks like I’ve got a lot of study to do.
 * I’m a bit confused about “something” in your example though.
    Can you flesh that
   out a bit?
 * And why do I need to write the function twice?
    The two code examples are identical,
   no?
    -  This reply was modified 6 years, 2 months ago by [TWD](https://wordpress.org/support/users/twd/).
    -  This reply was modified 6 years, 2 months ago by [TWD](https://wordpress.org/support/users/twd/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/#post-12613112)
 * The constant definition in the second is commented out so no definition is made.
   Thus the output is different.
 * Do you know about the ternary operator? `boolean ? 'something' : 'another';`
   
   It’s equivalent to `if ( boolean ) 'something' else 'another';` except it returns
   the value where if statements do not.
 *  Thread Starter [TWD](https://wordpress.org/support/users/twd/)
 * (@twd)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/#post-12614805)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/)
 * OK. At least the second snippet makes sense now.
 * Why is it necessary to include this line?
    `var_dump($classes); // Dumps false`
    -  This reply was modified 6 years, 2 months ago by [TWD](https://wordpress.org/support/users/twd/).
    -  This reply was modified 6 years, 2 months ago by [TWD](https://wordpress.org/support/users/twd/).
 *  [mark l chaves](https://wordpress.org/support/users/mlchaves/)
 * (@mlchaves)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/#post-12614841)
 * Hi [@twd](https://wordpress.org/support/users/twd/),
 * I recommend you **run **the code yourself so learn what it’s doing. You can also
   Duck Duck Go or Google `var_dump` function.
 * [https://www.php.net/manual/en/function.var-dump.php](https://www.php.net/manual/en/function.var-dump.php)
 * `var_dump` is useful for testing and debugging! So, it’s necessary for testing
   and proofing.
 * Once you prove the code works, you can remove the `var_dump` call. You should
   really be testing all this code yourself as proof.
 * Thanks!
    -  This reply was modified 6 years, 2 months ago by [mark l chaves](https://wordpress.org/support/users/mlchaves/).
      Reason: Clarification

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

The topic ‘Filter issue’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 3 participants
 * Last reply from: [mark l chaves](https://wordpress.org/support/users/mlchaves/)
 * Last activity: [6 years, 2 months ago](https://wordpress.org/support/topic/noobie-php-question/#post-12614841)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
