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


Comments

3 responses to “Enable Shortcodes in Custom Fields”

  1. Dieter Schummer Avatar
    Dieter Schummer

    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

    1. 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/

  2. Dieter Schummer Avatar
    Dieter Schummer

    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

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.