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 ) );
}
}
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
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/
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