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%2Fpage%2F2%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 15 replies - 16 through 30 (of 43 total)

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

 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17420590)
 * yes, I didnt want to have just one “get_post_meta”, I’ll have one for the category
   terms and one for the taxonomy terms, but I cant understand your “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.”
 * So I dont know what I should add to
 * `get_post_meta( $post->ID, 'description_'.$lang, true );`
 * to make it visible
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17422207)
 * Concatenate the return value from get_post_meta() to whatever else you want the
   shortcode to display. Concatenate to the variable your code eventually returns
   in the end.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17423883)
 * I think the following code cant work
 *     ```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, 'category_'.$lang, true) . get_term_meta( $category->term_id, 'tipologia_'.$lang, true) .' '. $content;
   
        }
       ```
   
 * Because I dont have a field “tipologia_de”, neither a “category_de”. I tryed 
   to change my field for category in german into “category_de” but I should have
   done a lot of faults because I could even see the field, I worked on
 *     ```wp-block-code
       add_action( 'category_edit_form_fields', "category_add_term_field");
   
       function category_add_term_field( $term ){
          $category = get_term_meta( $term->term_id, 'function_category', true );
           echo '<tr class="form-field">
             <th scope="row"><label for="function_category">Nome tedesco</label></th>
             <td><input type="text" name="function_category" value="'.$category.'"  />
             <p class="description">Nome del termine in tedesco</p></td>
           </tr>';
       }
   
       add_action( 'saved_category', 'update_category');
       function update_category ( $term_id ) {
         update_term_meta( $term_id, 'function_category', sanitize_text_field ( $_POST['function_category'] ) );
       }
       ```
   
 * Everywhere I see “category” I replace by “category_de”, this should be simple
   but…
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17424123)
 * Or also in the shortcode should I use archive_de? So that I would syncronize 
   the search box terms with the archive title filter: [https://pastebin.com/Xid3M1Ua](https://pastebin.com/Xid3M1Ua)
 * This filter in particular is for the category
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17425966)
 * Looks like you’re currently saving under the key “function_category”, but when
   you add other languages everything will work much better with “_de” appended (
   or whatever the current language is aside from the default Italian, “_en”, “_fr”,
   etc.
 * In order to change over to “category_de”, tipologia_de”, “category_en”, etc.,
   first modify the input and saving code. Change all instances of “function_category”
   to “category_de”. Similar for the Tipologia taxonomy terms. If “archive_de” works
   better for you, then use that instead. I’ve not been able to keep track of what
   meta keys you use for what so I cannot judge which is better. What matters is
   having a language suffix and being consistent in whatever names you decide to
   use.
 * To update all the meta data you’ve already input, use the Better Search and Replace
   plugin. Search the meta tables for all instances of “function_category” and replace
   with “category_de” or whatever you decide to use.
 * To get the shortcode working after the above changes, I think all you need to
   do is change `$content` to `$output`.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17427159)
 * Ok, I’ve been able to pass from “function_category” to “category_de”, I wonder
   why my modified shortcode doesnt work about category terms:
 *     ```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, 'category_'.$lang, true) .' '. $content;
   
        }
       ```
   
 * I cant get the “get_term_meta” printed
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17428745)
 * Remove this part of the return line: `$output .=`. It serves no purpose and causes
   an undefined variable $output error.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17428865)
 * I eliminated it but get_term_meta is still invisible 🙁
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17431973)
 * $category is undefined. Try adding:
    `$category = wp_get_post_category( $post-
   >ID )[0];`
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17432001)
 * This breaks the site
 *     ```wp-block-code
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
         global $post;
       $lang = substr( get_locale(), 0, 2);
   
       $category = wp_get_post_category( $post->ID )[0];
   
       $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
   
       return   $output .= get_term_meta( $category->term_id, 'category_'.$lang, true) .' '. $content;
   
        }
       ```
   
 * but also
 *     ```wp-block-code
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
         global $post;
       $lang = substr( get_locale(), 0, 2);
   
       $category = wp_get_post_category( $post->ID )[0];
   
       $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
   
       return   $output .= $category .' '. $content;
   
        }
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17433170)
 * Remove this part of the return line: `$output .=`. It serves no purpose and causes
   an “undefined variable $output” error.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17433293)
 * I did but the layout is broken. I tryed both the above codes without `$output.
   =`
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17434509)
 * There’s no `wp_get_post_categpory()` function, it’s `wp_get_post_categories()`.
 * Getting the zeroth element with `[0]` (assigned to $category) gets us a WP_Term
   object. You cannot return an object from a shortcode handler, you have to extract
   out a string property. Like this:
    `return $category->name .' '. $content;`
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17435347)
 * If I do
 *     ```wp-block-code
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
         global $post;
       $lang = substr( get_locale(), 0, 2);
   
       $category = wp_get_post_categories( $post->ID )[0];
   
       $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
   
       return  $category.' '. $content; 
   
        }
       ```
   
 * “$category” returns a number, if i replace “$category” by “$category->name” I
   get nothing, neither deleting the [0]
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/2/#post-17436975)
 * I see, you’re just getting a category ID. The doc page is a little misleading
   about what is returned. The doc page also says:
 * > The results from wp_get_post_categories() aren’t cached which will result in
   > a database call being made every time this function is called. Use this function
   > with care. For performance, functions like get_the_category() should be used
   > to return categories attached to a post.
 * That’s good advice, if you use get_the_category() instead you’ll get an array
   of term objects like I was expecting. Despite “get_the_category” using the singular
   form of category, it does indeed return an array multiple categories if they 
   exist. So you do still need the `[0]` part afterwards.

Viewing 15 replies - 16 through 30 (of 43 total)

[←](https://wordpress.org/support/topic/extending-a-function/?output_format=md) 
[1](https://wordpress.org/support/topic/extending-a-function/?output_format=md) 
2 [3](https://wordpress.org/support/topic/extending-a-function/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/extending-a-function/page/3/?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
