• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2785

Original Genesis & WooCommerce Tutorials & 6000+ Guaranteed Code

Snippets

  • Try Premium
  • Log in

Enable Shortcodes in Custom Fields

If you paste any type of shortcode in the value field of the custom fields meta box, it won’t work.

By default, WordPress doesn’t automatically parse shortcodes added to custom fields.

To make your shortcodes execute, you need to modify the code you use to get the post meta.

In this tutorial, i’ll provide 3 code snippets which show you what the code looks without modification for parsing shortcodes as well as with the correct modification to the code so all your shortcodes will print correctly in your custom fields.

Here’s the Post Edit screen in the backend which shows you 2 custom fields that work with the code below:

custom-field-shortcode

Here’s the solution:

Without do_shortcode

add_action( 'loop_start', 'custom_field_without_shortcode' );
function custom_field_without_shortcode() {  
$wos = get_post_meta( get_the_ID(), 'without_shortcode', true );
    if ( $wos ) {
echo '<div class="without-shortcode">' . $wos . '</div>';
    }
}

With do_shortcode

All we have added is do_shortcode

add_action( 'loop_start', 'custom_field_with_shortcode' );
function custom_field_with_shortcode() {
$ws = get_post_meta( get_the_ID(), 'with_shortcode', true );
    if ( $ws ) {
echo '<div class="with-shortcode">' . do_shortcode( $ws ) . '</div>';
    }
}

Or genesis specific custom fields:

add_action( 'genesis_entry_footer', 'genesis_custom_field_with_shortcode' );
function genesis_custom_field_with_shortcode() {
$custom_field = genesis_get_custom_field('your-custom-field-key');
if ( $custom_field ) {
echo '<div class="with-shortcode">' . do_shortcode( $custom_field ) . '</div>';
    }
}

Or use printf

add_action( 'genesis_entry_footer', 'genesis_custom_field_with_shortcode' );
function genesis_custom_field_with_shortcode() {
$custom_field = genesis_get_custom_field('your-custom-field-key');
if ( $custom_field ) {
printf(  '<div class="your-class">%s</div>', do_shortcode( $custom_field ) );
    }
}

Related Code Snippets

  • Add Shortcodes In Hook Locations Using Functions
  • 14 Ways To Code Custom Fields

Custom Fields

Reader Interactions

Comments

  1. Dieter Schummer says

    May 14, 2022 at 12:25 pm

    Hi Brad,
    meanwhile we have found that your code do_shortcode works an the page shows us the correct shortcode value for [email] like that

    test@test.com

    but maybe you have an idea for that:

    we are using beaver themer with conditional logic for show/hide text blocks
    https://docs.wpbeaverbuilder.com/beaver-themer/conditional-logic/beaver-themer-conditional-logic/

    in our example
    show text block if Post custom field “with-shortcode” is equal “test@test.com”
    THIS DOESN’T WORK

    IT WORKS WITH
    show text block if Post custom field “with-shortcode” is equal “[email]”

    Any ideas?

    Thanks & Regards

    Log in to Reply
    • Brad Dalton says

      May 14, 2022 at 12:40 pm

      I can write the code for you if you order a custom tutorial and provide more details however i do not work with Beaver Builder as there is no need. This may help https://docs.wpbeaverbuilder.com/beaver-themer/field-connections/connections-to-wp-custom-fields/test-for-values-in-wordpress-custom-field-shortcode/

      Log in to Reply
  2. Dieter Schummer says

    May 14, 2022 at 10:29 am

    Hi Brad,
    could it be that these solution doesn’t works?

    We have try the following:
    create and fill local shortcode
    [email] = test@test.com

    custom field
    name your-custom-field-key
    value [email]

    we try all your above 4 solutions and putting the the code in our child theme functions.php
    but custom field response with [email] and not with test@test.com

    Any ideas?

    Thanks & Regards
    Dieter

    Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags
 

Loading Comments...
 

You must be logged in to post a comment.