Title: add_shortcode trouble&#8230;
Last modified: August 21, 2016

---

# add_shortcode trouble…

 *  Resolved [Lafeuille](https://wordpress.org/support/users/lafeuille/)
 * (@lafeuille)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/)
 * Hi everyone,
 * My propose is to create a shortcode to add videos where I want in a post (not
   only at the top or bottom).
    I normally get the video using this code: `<?php
   dp_video($post->ID, get_option('dp_single_video_autoplay')); ?>`
 * So I decide to put in functions.php this code:
 *     ```
       function featured_video($post) {
       	dp_video($post->ID, get_option('dp_single_video_autoplay'));
       	echo "TOTO WHERE ARE YOU?";
       }
       add_shortcode('featured_video', 'featured_video');
       ```
   
 * And I had in the post content `[featured_video]` where I want to show the video
   but nothing is showing not even the echo(:))…
 * Please can someone tell me what I’m doing wrong?
    Cheers.

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

 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106260)
 * Firstly, presumably you have registered your function to respond to the shortcode,
   like this:
 *     ```
       add_shortcode('shortcode', 'functionName');
       ```
   
 * Then you need to have your function RETURN the text. It is tempting to have your
   function ECHO the text, but this is wrong! It may seem to work, it may nearly
   work, but it wont necessarily put the text in as a substitute for the shortcode
   at the correct place.
 * The whole story is here: [http://codex.wordpress.org/Function_Reference/add_shortcode](http://codex.wordpress.org/Function_Reference/add_shortcode)
 * Commonly the function looks like this (there are more complex options possible
   too, like that take arguments):
    `function doShortCodeXYZ() { r = ” if( sometest)
   r .= ‘Some html’; r .= ‘other html’; //and so on, with finally return r; }
 * Another option (which I am just learning about) is to call ob_start() and capture
   all the echo text, here is the referance: [http://php.net/manual/en/ref.outcontrol.php](http://php.net/manual/en/ref.outcontrol.php)
 *  Thread Starter [Lafeuille](https://wordpress.org/support/users/lafeuille/)
 * (@lafeuille)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106285)
 * Hi RossMitchell,
 * Thank you for your reply!
 * With your help it was easy to return a string but not a function.
    I tried: `
   return dp_video($post->ID, get_option('dp_single_video_autoplay'));`
 * But it’s doing nothing…
 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106329)
 * Could you show all your shorcode relevant code ?
 * What happens if you replace the above “return dp_video( … );”
    with “return ‘
   Unique PHRase here.’;” does it get show at the right place ?
 *  Thread Starter [Lafeuille](https://wordpress.org/support/users/lafeuille/)
 * (@lafeuille)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106332)
 * This is the first shortcode I created 🙂
 * I try this:
 *     ```
       function featured_video($post) {
       	$content = '<div id="video-single"><p>TOTO IS HERE BUT WHERE IS THE VIDEO?</p>' . dp_video($post->ID, get_option('dp_single_video_autoplay')) . '</div>';
           return $content;
       }
       ```
   
 * and the <p> content appears at the right place but still not the video 🙁
 * Any idea!
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106347)
 * Try using output buffering to capture the contents of the dp_video() call:
 *     ```
       function featured_video($post) {
       	$op = '';
       	ob_start();  // Start output buffering
       	dp_video($post->ID, get_option('dp_single_video_autoplay'));
       	echo "<p>TOTO WHERE ARE YOU?</p>";
       	$op .= ob_get_clean();  // Add the buffer contents to $op
       	return $op;
       }
       add_shortcode('featured_video', 'featured_video');
       ```
   
 *  Thread Starter [Lafeuille](https://wordpress.org/support/users/lafeuille/)
 * (@lafeuille)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106354)
 * So, I can see the <p> at the right place but still no video showing.
    🙁
 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106355)
 * Then presumably there is something wrong inside your dp_video funcgtion.
    This
   is likely that $post is not what you expect. You would then have to workout how
   to get the $post->ID value into your function. Suggest that you put: print_r(
   $post); In the above fn, OR readup on how the shortcode fn is called.
 *  Thread Starter [Lafeuille](https://wordpress.org/support/users/lafeuille/)
 * (@lafeuille)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106357)
 * You rock guys 🙂
    So the final and working code (took out $post parameter in 
   the call function and use print_r($post) instead of $post->ID)
 *     ```
       function featured_video() {
       	$op = '';
       	ob_start();  // Start output buffering
       	dp_video(print_r($post), get_option('dp_single_video_autoplay'));
       	$op .= ob_get_clean();  // Add the buffer contents to $op
       	return $op;
       }
       add_shortcode('featured_video', 'featured_video');
       ```
   
 * Cheers
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106359)
 * I think this would work also:
 *     ```
       function featured_video() {
       	global $post;
       	$op = '';
       	ob_start();  // Start output buffering
       	dp_video($post->ID, get_option('dp_single_video_autoplay'));
       	$op .= ob_get_clean();  // Add the buffer contents to $op
       	return $op;
       }
       add_shortcode('featured_video', 'featured_video');
       ```
   
 *  Thread Starter [Lafeuille](https://wordpress.org/support/users/lafeuille/)
 * (@lafeuille)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106361)
 * Yes it’s also working 🙂
 *  Thread Starter [Lafeuille](https://wordpress.org/support/users/lafeuille/)
 * (@lafeuille)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106380)
 * > I think this would work also:
 * In fact it works much more better 🙂
    The previous code were always sorting the
   same video independently to the post ID…
 * Thanks again!

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

The topic ‘add_shortcode trouble…’ is closed to new replies.

## Tags

 * [add_shortcode](https://wordpress.org/support/topic-tag/add_shortcode/)
 * [function](https://wordpress.org/support/topic-tag/function/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 3 participants
 * Last reply from: [Lafeuille](https://wordpress.org/support/users/lafeuille/)
 * Last activity: [11 years, 10 months ago](https://wordpress.org/support/topic/add_shortcode-trouble/#post-5106380)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
