Title: Modifying a function
Last modified: January 10, 2024

---

# Modifying a function

 *  [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/modifying-a-function-2/)
 * This code let me switch the description from italian to german, I’d like to have
   as output not only the custom meta field content, but at the beginning of the
   description you should read the category (name of the destination) and the taxonomy(
   typology of accomodation); these terms too should be translated, in particular
   the taxonomy term. At the moment 2 different shortcodes and 2 different custom
   fields do this job (and throw css the category name and the taxonomy term are
   in a different colour and are bold), but I have to fill to many fields each time
   I insert a new apartment. Here is the code to modify
 *     ```wp-block-code
       //SHORTCODE DESCRIZIONE IN TEDESCO
   
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
         global $post;
         $german = get_post_meta( $post->ID, 'description_de', true );
         if ( 'de_DE' == get_locale() && ! empty( $german )){
            return $german;
         } else {
            return get_post_meta( $post->ID, 'description_it', true );
         }
       }
       ```
   
 * First of all I would create 2 other variables and declare them, such as $category-
   german and $tipology-german, than in the “return” I should have something like
   $category-german, $tipology-german, $german; the return for italian too should
   be different. Keeping in mind that I could add other languages. Not so many but
   al least english
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fmodifying-a-function-2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/modifying-a-function-2/#post-17333664)
 * > I have to fill too many fields each time
 * I sympathize, but the only alternative I know of would be to utilize some sort
   of translation API to auto-populate the translated meta fields any time you add
   a term, description, or title in Italian. Machine translations don’t always do
   a good job, but the result is usually understandable even if not totally correct.
 * For shortcode handlers, you return the complete HTML of whatever the shortcode
   is supposed to do. If $category-german, $tipology-german, $german all belong 
   together in the same HTML, then that will work. But if each needs to be in a 
   different part of the page you’ll likely need different shortcodes or an altered
   template. I don’t know what sort of HTML you need, but here’s an example:
    `return'
   <span style="color: red; font-weight: 700;">'. "$category-german, $tipology-german,
   $german" . '</span>';`
 * Of course your code would need to get the values for $category-german and $tipology-
   german. If you are translating to multiple languages, maybe including “german”
   in variable names would be confusing. Maybe use “local” or “translated” or nothing
   extra instead?
 * To get various translations based on locale, you should extract out the language
   part from the actual locale because the same language might have multiple locales,
   such as en_US, en_GB, en_AU, etc. Assign the extracted language to a var for 
   use in the meta key name. For example:
 *     ```
       $lang = substr( get_locale(), 0, 2 );
       $desc_trans = get_post_meta( $post->ID, "description_$lang", true );
       // repeat similar for other meta values
       ```
   
 * This way you don’t need if/else statements to get the right translation for any
   language you’ve translated for. However, you might want to still do an empty()
   check and fall back to something in case the specific translation is not available.
    -  This reply was modified 2 years, 5 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: code format fixed

Viewing 1 replies (of 1 total)

The topic ‘Modifying a function’ 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/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [2 years, 5 months ago](https://wordpress.org/support/topic/modifying-a-function-2/#post-17333664)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
