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

Premise is a fascinating system that ties all those things together into one system (I’m an affiliate).  You don’t even necessarily need to use a Genesis theme (although I’d recommend it) – you can use any current WordPress setup.  Check it out!

Share

Comments

  1. Another Dave says:

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

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

  3. This is a great post! Very easy to read, good job!

  4. Jonathan,
    Thanks, amigo!
    Folks, check out Jonathan’s own Genesis tutorials, he’s got some good stuff there.
    Dave

  5. 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”

  6. 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.

Speak Your Mind

*