Title: pass variable to do_shortcode()
Last modified: August 21, 2016

---

# pass variable to do_shortcode()

 *  [psnepsts](https://wordpress.org/support/users/psnepsts/)
 * (@psnepsts)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/pass-variable-to-do_shortcode/)
 * Hello,
 * Having the hardest time trying to pass a variable (the Youtube link) to the do_shortcode()
   function.
 *     ```
       $YTcode = the_field('add_video');
       echo $YTcode;
       echo do_shortcode('[fve]'.$YTcode.'[/fve]');
       ```
   
 * the_field grabs the URL as entered in a custom field. The first echo outputs 
   the URL as entered. The second echo does nothing.
 * Any hints would be lovely.
 * [http://wordpress.org/plugins/fluid-video-embeds/](http://wordpress.org/plugins/fluid-video-embeds/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Jon-B](https://wordpress.org/support/users/jon-b/)
 * (@jon-b)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/pass-variable-to-do_shortcode/#post-4396448)
 * I’m using a custom field to display a video using the shortcode in my single 
   page template:
 *     ```
       <?php
       // Get the video URL and put it in the $video variable
       $videoID = get_post_meta($post->ID, 'youtube_url', true);
       // Check if there is in fact a video URL
       if ($videoID) {
       	echo '<div class="videoContainer">';
       	// Echo the embed code via oEmbed
       	echo do_shortcode('[fve]http://youtu.be/' . $videoID . '[/fve]' );
       	echo '</div>';
       }
       ?>
       ```
   
 * The “youtube_url” is what I put as the custom field and then I just put the youtube
   video ID/code (the alphanumeric part of the video url) whatever you want to call
   it as the variable.
 * For example [http://www.youtube.com/watch?v=nGvOMUZrqVM](http://www.youtube.com/watch?v=nGvOMUZrqVM)
   I’d just put nGvOMUZrqVM in. You can obviously edit the code as you wish. I’m
   going to tinker with it in a bit to see if I can just put the whole url in so
   it should also work with vimeo. Don’t know if you ever worked it out but I was
   looking for this so thought I’d share if others got stuck.
 *  [Jon-B](https://wordpress.org/support/users/jon-b/)
 * (@jon-b)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/pass-variable-to-do_shortcode/#post-4396449)
 * So I did some fiddling and I think the following code not only lets you use vimeo
   as well as youtube but is probably more simple for users as well as they can 
   just put the whole url in the box.
 *     ```
       <?php
       // Get the video URL and put it in the $video variable
       $videoID = get_post_meta($post->ID, 'video_url', true);
       // Check if there is in fact a video URL
       if ($videoID) {
       	echo '<div class="videoContainer">';
       	// Echo the embed code via oEmbed
       	echo do_shortcode('[fve]' . $videoID . '[/fve]' );
       	echo '</div>';
       }
       ?>
       ```
   
 * I changed “youtube_url” to “video_url” as the custom field name. You can use 
   pretty much whatever name you want though, as long as it matches your code.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘pass variable to do_shortcode()’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/fluid-video-embeds_746591.svg)
 * [Fluid Video Embeds](https://wordpress.org/plugins/fluid-video-embeds/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/fluid-video-embeds/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/fluid-video-embeds/)
 * [Active Topics](https://wordpress.org/support/plugin/fluid-video-embeds/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/fluid-video-embeds/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/fluid-video-embeds/reviews/)

## Tags

 * [do_shortcode](https://wordpress.org/support/topic-tag/do_shortcode/)
 * [template](https://wordpress.org/support/topic-tag/template/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 2 replies
 * 2 participants
 * Last reply from: [Jon-B](https://wordpress.org/support/users/jon-b/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/pass-variable-to-do_shortcode/#post-4396449)
 * Status: not resolved