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:
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 ) );
}
}
Was This Tutorial Helpful?