Skip to content
Advertisement

Using PHP WordPress variable do_shortcode for dynamic content

I’m building a WordPress site and my aim to have users be able to place custom shortcodes in the backend of WordPress on blog posts and have that content display on the site.

I’m using a simple text field Advanced Custom Field to do this (below.) but am struggling to integrate this with the do_shortcode variable.

Any help would be greatly appreciated, thanks.

video-id

Get Advanced Custom Field input to display as plaintext

<?php if( get_field('brand_video') ):?>
    <?php the_field('brand_video');?>
<?php endif;?>

Get WordPress to output video

<?php echo do_shortcode(""); ?>

My attempt of combining the two

<?php if( get_field('brand_video') ):?>
    <?php echo do_shortcode(the_field('brand_video'));?>
<?php endif;?>

Advertisement

Answer

In do_shortcode replace the_field with get_field.

the_field is return a value. get_field is print a value.

<?php if( get_field('brand_video') ):?>
    <?php echo do_shortcode(get_field('brand_video'));?>
<?php endif;?>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement