Tom
Tom

Reputation: 159

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("[video id=204]"); ?>

My attempt of combining the two

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

Upvotes: 0

Views: 996

Answers (1)

Team Dolphin
Team Dolphin

Reputation: 273

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;?>

Upvotes: 2

Related Questions