Title: Extending a function
Last modified: February 5, 2024

---

# Extending a function

 *  [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/)
 * How can I insert this:
 *     ```wp-block-code
       <strong> <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?> </strong>
       ```
   
 * to this:
 *     ```wp-block-code
       add_shortcode('incipit', 'translate_incipit');
       function translate_incipit() {
         global $post;
         $german = get_post_meta( $post->ID, 'incipit_de', true );
         if ( 'de_DE' == get_locale() && ! empty( $german )){
            return $german;
         } else {
            return get_post_meta( $post->ID, 'incipit_it', true );
         }
       }
       ```
   
 * what I want to do is having the category name printed before the value of “get_post_meta”,
   having in mind that I should print the name of the category eventually translated
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fextending-a-function%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 15 replies - 1 through 15 (of 43 total)

1 [2](https://wordpress.org/support/topic/extending-a-function/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/extending-a-function/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/extending-a-function/page/2/?output_format=md)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17400867)
 * The code you want to insert needs to be modified for use in a shortcode callback
   because shortcode callbacks must not ever echo out content. Instead, all output
   should be collected into a single variable. Before starting a foreach loop, initialize
   a variable by assigning an empty string or the first part of the output. For 
   example:
    `$output = '';` or `$output = '<strong>';`
 * Anywhere content is output, such as with `echo`, instead concatenate the output
   to $output. For example:
    `$output .= $category->cat_name . ' ';` But if the 
   category name needs to maybe be translated it will be more complicated than this.
   Addressed below. Also, are you sure about `cat_name`? That’s an unusual object
   property if we’re using WP_Term objects. I think you want `$category->name`.
 * After the foreach loop completes, don’t forget the closing strong tag:
    `$output.
   = '</strong>';`
 * Where all of this goes into your current shortcode callback depends on where 
   you want output to occur in relation to post meta content. Before or after? Then
   add the category code before or after the post meta code.
 * Modify the post meta code. Instead of returning content, concatenate post meta
   content to $content. In the end after all is said and done, you will finally 
   do:
    `return $content;`
 * Now, how to work in possible translations of category names. The first thing 
   your function should do is get the locale’s 2 character language code so you 
   can use it to get the right translation. Let’s say the language code is assigned
   to `$lang`. Then you could get the right language’s post meta with something 
   like:
    `$output .= get_post_meta( $post->ID, 'incipit_'.$lang, true );`
 * You’d do something similar for category names, except there is only term meta
   for non-Italian languages. If the locale language is “it”, $category->name is
   fine, but otherwise it has to come from term meta. Thus you need to determine
   the right value to output before concatenating it to $output. Instead of just
   `
   $output .= $category->name . ' ';` do something like: `$output .= 'it' == $lang?
   $category->name : get_term_meta( category->term_id, 'tipologia_'.$lang, true).'';`
 * In case you’re not familiar with the `$condition ? 'its true':'its false';` construct,
   it’s a kind of shorthand for typical if/then/else statements. It’s called a “
   ternary” statement. It’s equivalent to:
 *     ```
       if ( $condition ) {
         'its true';
       } else {
         'its false';
       }
       ```
   
 * Will this become the code I was asking for in your other topic about filtering
   a taxonomy for German? Then I guess I’m answering my own question 🙂 No filtering
   is necessary with this approach.
 * Do note that this requires you to alter the meta key names to be similar to tipologia_de,
   tipologia_en, tipologia_fr, etc. This means updating the code that saves term
   custom field data related to translated term names.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17401600)
 * First of all I made a mistake about the shortcode I needed to use. That said,
   it makes sense a function like this?
 *     ```wp-block-code
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
         global $post;
   
       $content= get_post_meta( $post->ID, 'description_it', true );
   
         $german = get_post_meta( $post->ID, 'description_de', true );
   
   
        if ( 'de_DE' == get_locale() && ! empty( $german )){
             $output .= $category->name . ' '. $german ;
         } else {
            $output .= $category->name . ' '. $content ;
         }
       }
       ```
   
 * I have just transformed the post meta content into a variable, or better 2 variables,
   according to the language, than I keep the conditional structure and change the
   output according to the language
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17401615)
 * I see now that I havent solutioned the category term name in german
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17403867)
 * Whether the meta data exists or not does not invalidate the shortcode. You’re
   approach is fine if it works for you. It’s more cumbersome than what I was suggesting,
   but whatever works is good. However, you’d need to modify the code anytime you
   add another language. I was trying to avoid that by dynamically constructing 
   the meta key based on the current locale’s language. Thus the shortcode code 
   wouldn’t need to change when another language is added.
 * But you’d still need additional meta box fields in the term editor screen, as
   well as the save code for each language. Right now you apparently have none of
   this in place. It’s the same concept as you’ve already done for custom taxonomy
   terms. Just be sure you’re thorough in adjusting copies of what you’ve already
   done to work for categories as well. For example, when you rename the function
   name, be sure the name in the add_filter() call is also changed to match. Be 
   sure every instance of the custom taxonomy name is changed to “category”.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17405144)
 * Since I desire more languages I’ll try to undestand better your code
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17405299)
 * I think my own code has some errors because the shortcode outputs nothing. I 
   can’t see where is the problem. I checked the code with [https://phpcodechecker.com/](https://phpcodechecker.com/)
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17409142)
 * I tryed to write a new code according to your suggestions, it makes a sense?
 *     ```wp-block-code
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
         global $post;
   
       $content= get_post_meta( $post->ID, 'description_it', true );
   
         $german = get_post_meta( $post->ID, 'description_de', true );
   
   
        if ( 'de_DE' == get_locale() && ! empty( $german )){
               $output .= 'de' == $lang ? $category->name : get_term_meta( category->term_id, 'tipologia_'.$lang, true) .' '. $german ;
         } else {
            $output .= 'it' == $lang ? $category->name : get_term_meta( category->term_id, 'tipologia_'.$lang, true) .' '. $content;
         }
       }
       ```
   
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17409579)
 * About the above code, with php checker “No issues detected.” but it broke the
   site
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17410533)
 * I’ve not checked your code thoroughly, but the glaring problem is you forgot `
   return $output;` at the end.
 * Additionally, you need to extract the 2 char language code from the 5 char locale
   value (with substr()) and assign it to `$lang`. Then you can get post meta of
   whatever language (assuming the meta keys are all similarly named) with just 
   this alone:
    `$content = get_post_meta( $post->ID, 'description_'.$lang, true);`
 * Then you can generate output in whatever language with just this:
    `$output .
   = get_term_meta( $category->term_id, 'tipologia_'.$lang, true) .' '. $content;`
   No language conditionals needed, everything depends on the value of $lang. But
   you’ll still want to check for empty values and set $output to some default value
   instead of empty.
 * A couple more issues though. You should initialize $content to an empty string
   before trying to concatenate with `.=`. And currently $category is undefined 
   so you cannot properly do $category->term_id. I’m not sure where that would come
   from though. Maybe wp_get_post_categories()?
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17411536)
 * I tryed with the following
 *     ```wp-block-code
       //prova shortcode descrizione tedesco e tutte le lingue
   
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
         global $post;
   
       $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
   
       return   $output .= get_term_meta( $category->term_id, 'tipologia_'.$lang, true) .' '. $content;
   
        }
       ```
   
 * I think something is missing, maybe $lang should be defined somewhere? This code
   has no output in my test for real
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17415333)
 * Yes PHP despises undefined variables 🙂 You need to assign the current 2 char
   language code to $lang. You can extract it from the locale with substr().
    `$
   lang = substr( get_locale(), 0, 2);`
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17415682)
 * Ok, I did the following:
 *     ```wp-block-code
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
         global $post;
       $lang = substr( get_locale(), 0, 2);
       $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
   
       return   $output .= get_term_meta( $category->term_id, 'tipologia_'.$lang, true) .' '. $content;
   
        }
       ```
   
 * But I get in return only $content, the get_term_meta doesnt appear
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17416479)
 * Looks like `$category` is also undefined. The term we’re after isn’t actually
   a category, it’s tipologia, right? Maybe the variable name ought to be `$tipologia`
   to prevent confusion?
 * Whatever the name, you need to get the term assigned to the current post. You
   can do that with [wp_get_post_terms()](https://developer.wordpress.org/reference/functions/wp_get_post_terms/).
   Note that an array of term objects are returned, even if there is only one term
   assigned to the post. So you’d need to extract the first element (index `[0]`)
   out of the array before using its term_id property.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17417201)
 * I’d like to have both the category and the taxonomy (tipologia) in the function,
   so I will concatenate 2 get_term_meta. After having visited the link you wrote,
   it seems to me I could do the following:
 *     ```wp-block-code
        get_term_meta( int $post_id, string[0], $category->term_id, 'tipologia_'.$lang, true)
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/#post-17419443)
 * You cannot add extra args to function calls. get_term meta() takes 3 args and
   that’s it. To display meta data for two terms you need to make two separate calls.
   You may then concatenate the returned values together.
 * Be sure all variables that you pass to a function have proper values assigned.

Viewing 15 replies - 1 through 15 (of 43 total)

1 [2](https://wordpress.org/support/topic/extending-a-function/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/extending-a-function/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/extending-a-function/page/2/?output_format=md)

The topic ‘Extending 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/)
 * 43 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17453737)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
