Using Shortcodes in a Genesis Custom Field

Raw WordPress Custom Fields – Nutritionally Sound?

In a previous post I had showed an example of how to display a custom field using Genesis. Site visitor Michael had asked if he could use shortcodes in a Custom Field, and I was unable to answer at the time. Today I finally got a chance to test this, and indeed, adding the shortcode a the custom field simply displays the raw shortcode rather than the intended output.

I figured out a way to get it to work! Caveat – a small amount of PHP coding is needed (I can already see people cowering in fear). But take heart, it’s not too bad. Just be ready with FTP in case you make a typo! If any of this makes you nervous, ask a programmer to do it for you instead.

Ancient Custom of Blessing the Fields

Shortcodes Outstanding In Their Field

My example displays the custom field contents just above the main sidebar. The same technique could be used for displaying it elsewhere in the theme. (See this helpful visual Genesis hook guide) In my test I had both some text and a shortcode for the venerable Contact Form 7, and it worked great. In Genesis, the following block of code would be placed in your Theme Functions file (functions.php), or in your own custom functions file for even more cleanliness and upgrade ease. If you’re handy with PHP, this will be pretty simple. Just note that my custom field is named “slogan” in this case, and would be replaced with the name of your custom field. I found that I needed to use a global variable so that my function would have access to the Post info – that was key. Then we simply apply “do_shortcode” to the custom field so that the shortcode is activated. Finally, we echo that out to a spot in the theme.

add_action( 'genesis_before_sidebar_widget_area', 'custom_field_process' );

function custom_field_process() {
  global $post;
  if ( get_post_meta($post->ID, 'slogan', true) )
    echo do_shortcode(get_post_meta($post->ID, 'slogan', $single = true));
}

Apart from intellectual curiosity, I had wondered why Michael might want this capability. I supposed that if you had a reasonably proficient user, this would allow him/her to display plugin output (and extra text if desired) in a sidebar of a single Post, for instance. Whereas if you put a shortcode in the widget itself, you get it on every Post that has a sidebar. Shortcodes are somewhat user-friendly, certainly moreso than raw PHP code. And from there you can extrapolate where else in the theme this might be useful. So essentially, you’re allowing your user to enter some fairly fancy content on a Post, beyond the usual text and photos.

Exodus from Genesis

For any of you not using Genesis (yes, I am an affiliate), I think the same technique could be adapted for use in any up-to-date WordPress setup. The only difference would be that you would use a hook other than the Genesis one above.

Quick Shameless Plug

While I’m on the subject, for those of you who might be interested in doing one (or all) of the following using WordPress:

  • Membership sites
  • Landing pages
  • Selling digital downloads
  • Starting a community forum
  • Having secure content areas

 

8 Comments


  1. Bravo! I will promptly go about finding a use for this.

    Reply

    1. Another Dave,
      Sounds great! Hope you’ll share a link if you implement it.
      Dave

      Reply

  2. Thank you! Can you tell me how I would make a name appear before the output of the shortcode on the post? for example… “name: slogan”

    Reply

    1. You could do something like
      echo 'name: slogan';
      before the do_shortcode part above.

      I’d suggest brushing up on at least the basics of PHP. I see people on the Genesis forum who are indignant because Genesis doesn’t spoonfeed everything to you like some other frameworks do. Someone who won’t touch PHP code should actually not buy Genesis at all for development, although if they just want to buy a nice Genesis pre-made child theme and not alter it too much, that’s probably fine.

      Reply

  3. Hi there!

    I’m trying to add a Custom Slider to my Genesis child theme. The only instructions from the Slider folks are to add in the php somewhere.

    I tried placing this in home.php and broke the site. Is there a different method to add this in Genesis? I saw some folks adding shortcodes through the functions.php file.

    Thanks!

    -Dan

    Reply

    1. Daniel,
      No, It’s the same as you would do it in any WordPress theme. So you’ll need to double-check your syntax, and possibly read up on PHP syntax. Adding shortcodes to WordPress per se is a completely separate topic, out of scope here. Look here for ideas.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *