Title: Custom Fields and Shortcode together on Functions.php
Last modified: August 20, 2016

---

# Custom Fields and Shortcode together on Functions.php

 *  Resolved [lucianofiorini](https://wordpress.org/support/users/lucianofiorini/)
 * (@lucianofiorini)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/)
 * Hello everyone, I was looking to do this, but because I could not find a solution,
   I decided to share in the forum for those who really know this. Here goes:
 * What I need to do is, when inserting a field of custom fields in a post, this
   complete a shortcode on the function.php to be called in the post that I want
   in the following way [hello].
 * Here are step example of the codes I use.
 * First create the shortcode with this code:
 *     ```
       function mi_shortcode() {
          return 'Hello Manolo!';
       }
   
       add_shortcode('hello', 'mi_shortcode'
       ```
   
 * And what i need is that adding a custom is called “manolo” this is completed 
   within the short code.
 * So I tried the following code, but I have not worked:
 *     ```
       add_shortcode('hola', 'myshortcode'
       function myshortcode() {
          return 'Hola <?php echo get_post_meta($post->ID, 'manolo', true); ?>!';
       }
       ```
   
 * Could anyone help me? Did I explain well what I need?
 * Thanks in advance for any help you can give me!
 * Greetings!

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

 *  [nsathees](https://wordpress.org/support/users/nsathees/)
 * (@nsathees)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313331)
 * why not write the code directly in the template?
    First check ob the custom field
   exist, and if yes
 * `echo '[' . $customfield . ']'`;
 *  Thread Starter [lucianofiorini](https://wordpress.org/support/users/lucianofiorini/)
 * (@lucianofiorini)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313364)
 * nsathees, i dont write the code directly in the template because i want to put
   it in the middle of the text on the post content, so i think that using shortcodes[
   example] is the best way, but i dont know how to make the code.
 * Thanks for any help you can give me!
 * Greetings!
 *  Thread Starter [lucianofiorini](https://wordpress.org/support/users/lucianofiorini/)
 * (@lucianofiorini)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313433)
 * Hi, I tried with the following code, but I have not worked:
 * add_shortcode(‘hola’, ‘myshortcode’);
    function myshortcode() {return ‘Hola’ .
   get_post_meta($post->ID, ‘prueba’, true) . ;}
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313434)
 * Your code has a stray “.” in it. Try:
 *     ```
       add_shortcode( 'hola', 'myshortcode' );
       function myshortcode() {
           global $post;
           return 'Hola ' . get_post_meta( $post->ID, 'prueba', true );
       }
       ```
   
 *  [expat](https://wordpress.org/support/users/expat/)
 * (@expat)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313435)
 * hi, to interrupt this conversation:
 * 1. whats the difference in using shortcode and hacking the template?
 * 2. dont they both use functions.php?
 * 3. is shortcode like bbcodes?
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313436)
 * 1. Shortcodes are used by the user in posts, pages, and (if you enable this) 
   widgets. Putting the code directly in the template will require the user to manually
   edit that template if they want to change something.
 * 2. They _can_.
 * 3. A bit:
 * [http://codex.wordpress.org/Shortcode_API](http://codex.wordpress.org/Shortcode_API)
   
   [http://wpcodesnippets.info/blog/shortcode-fundamentals-part-1.html](http://wpcodesnippets.info/blog/shortcode-fundamentals-part-1.html)
 *  [expat](https://wordpress.org/support/users/expat/)
 * (@expat)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313437)
 * And what if i as admin, need custom fields to display in a certain way and lock
   the user into a certain display?
 * eg. if I need WP “excerpt” to display RED AND BOLD by default?
 *  [Big Bagel](https://wordpress.org/support/users/big-bagel/)
 * (@big-bagel)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313438)
 * Shortcodes return whatever you want them to.
 *     ```
       $output = '<span style="color: red; font-weight: bold;">';
       $output .= /* Whatever this shortcode is supposed to return */;
       $output .= '</span>';
   
       return $output;
       ```
   
 *  Thread Starter [lucianofiorini](https://wordpress.org/support/users/lucianofiorini/)
 * (@lucianofiorini)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313439)
 * Thanks Big Bagel! This is the code i need!
 * Thanks to all!

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

The topic ‘Custom Fields and Shortcode together on Functions.php’ is closed to new
replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 9 replies
 * 4 participants
 * Last reply from: [lucianofiorini](https://wordpress.org/support/users/lucianofiorini/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/custom-fields-and-shortcode-together-on-functionsphp/#post-2313439)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
