• 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.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @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

    (@twd)

    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;
      }
    }

    Hi @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

    Thread Starter TWD

    (@twd)

    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.
    • This reply was modified 6 years, 2 months ago by TWD.
    Moderator bcworkz

    (@bcworkz)

    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

    (@twd)

    @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.
    • This reply was modified 6 years, 2 months ago by TWD.

    Hi @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

    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. Reason: Clarification
Viewing 7 replies - 1 through 7 (of 7 total)

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