Home / custom field / Get custom field value with shortcode
  • How to Get custom field value with shortcode

    get_custom_field_value_with_shortcode

    Adding this PHP code to the functions.php of your wordpress theme will enable you to get custom field values using shortcode.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    add_shortcode('field', 'shortcode_field');
    function shortcode_field($atts){
         extract(shortcode_atts(array(
                      'post_id' => NULL,
                   ), $atts));
      if(!isset($atts[0])) return;
           $field = esc_attr($atts[0]);
           global $post;
           $post_id = (NULL === $post_id) ? $post->ID : $post_id;
           return get_post_meta($post_id, $field, true);
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
     [field "my_key"]
     [field "my_key" post_id=1]
    

    ( WordPress codex functions, hooks, in this snippet. )

    get_post, get_post_meta, add_shortcode, shortcode_atts, esc_attr,
  • Pingback: Tweets that mention Wordpress Get custom field value with shortcode – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com()

  • Adding this code in function.php disable my website. What was I doing wrong?

    Shovan Sargunam
    from http://tamilbabyname.org team

    • Are you getting any errors?

      • A ) is missing in line 6, before return;

        Works fine besides that :)

  • Kevin, I think this is EXACTLY what I’ve been hoping to find. Thank you for it.I want to be certain how to use it. When would you use the 2nd one instead of the 1st one? :
    [field “my_key”][field “my_key” post_id=1]

    Thank you.

    • Well if you add the shortcode to a post you can use  [field “my_key”] my_key is the name of the custom field. The other method [field “my_key” post_id=1] will let you define the post ID to get a specific posts custom field value.

      • Could you have HTML code in a custom field that “outputs”? For example:

        Today is the greatest day in the world.
        Blah
        [field “list_of_images”]
        The images above are grand, aren’t they?
        Blah
        [field “html_block1”]
        Blah
        [field “html_block2”]
        Thanks for reading my blog.

        And, just to be clear, if you don’t add a “post_id=”, it’ll just pull the custom field for the post/page you’re already on–the current post, right?

        • 1. With no ID yes it will just pull the current posts custom field.
          2. I can’t see why not should work fine you should be able to anything in the field and it will display.

        • I guess what I’m asking is if it renders custom field content as if it were entered in the Visual Editor or as if it was entered in the HTML Editor.

        • No it will render HTML just fine.

  • When trying to put custom field content within another shortcode, depending on how the other shortcode is setup, could you do away with the double-quotes?
    For example:
    [my_field_name]
    instead of
    [field "my_field_name"]

    That way a shortcode like this could work (right?):
    [contact-form to="[my_field_name]"]
    Because if a shortcode with quotes is within a shortcode with quotes, the quote/bracket stuff at the end gets it confused (i.e. doesn’t work).

    I don’t see any double-quotes in your function/snippet, so I wasn’t sure.

    Thanks bunches!

    • I tried [field]my_field_name[/field] and it didn’t work.

      • I’m posting details on nested shortcode tomorrow take a look it will give you a better idea of how it needs to work with embedded shortcode

    • No you can’t do that it would no longer function. What is it that you are trying to do with the nested shortcodes? nesting shortcode is not going to work either it will just break.

  • Pingback: 30 Extremely Useful Wordpress Code Snippets & Hacks | Web Design Habits()

  • Séb

    Thanks for that article, it really help me a lot :)

  • Séb

    Thanks for that article, it really help me a lot :)

  • Shafar

    Thanks! Was searching on google last 2 days!!!

  • Jb C.

    Doesn’t output anything within a Grid Element text block, in a Post Grid (Visual Composer)

  • Jarod Emerson Thornton

    This is awesome. Thank you so much!