Title: Multiple Instances Breaks Shortcode
Last modified: August 20, 2016

---

# Multiple Instances Breaks Shortcode

 *  [ryanve](https://wordpress.org/support/users/ryanve/)
 * (@ryanve)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/multiple-instances-breaks-shortcode/)
 * I had initially wrote a shortcode that displayed author profiles based on id.
   For example `[user-profile id="1"]` would display the profile block for author
   1. It worked. I was able to use it multiple times within a page.
 *     ```
       function user_profile( $atts, $content = null ) {
       extract(shortcode_atts(array('id' => ''), $atts));
       include ('user-profile.php');
       }
       ```
   
 * …but since the shortcode output was showing up before other entry content regardless
   of its place in the code ([same issue here](http://wordpress.org/support/topic/how-to-put-the_content-tag-above-shortcode?replies=5))
   I added the fix described [here](http://wordpress.org/support/topic/plugin-called-via-shortcode-appears-at-the-wrong-place-on-post?replies=5):
 *     ```
       function user_profile( $atts, $content = null ) {
       	extract(shortcode_atts(array('id' => ''), $atts));
       	function get_user_profile() {include ('user-profile.php');}
       	ob_start();
       	get_user_profile();
       	$output_string=ob_get_contents();
       	ob_end_clean();
       	return $output_string;
       	}
       ```
   
 * …which worked to solve the positioning problem but broke multiple instances of
   the shortcode. `[user-profile id="1"]` works but `[user-profile id="1"] [user-
   profile id="2"]` breaks it—the page just stops loading at that point.
 * The html/php in `user-profile.php` is viewable [here](http://dev.airve.com/code/user-profile_20110605.txt).
 * **How can I modify the shortcode function to allow multiple instances?**

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

 *  Thread Starter [ryanve](https://wordpress.org/support/users/ryanve/)
 * (@ryanve)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/multiple-instances-breaks-shortcode/#post-2117877)
 * Problem solved!
 * I updated the code in [user-profile.php](http://dev.airve.com/code/user-profile_20110606.txt)
   code so that it was all PHP and did not use any echos. Then I changed the shortcode
   function to:
 *     ```
       function user_profile( $atts, $content = null ) {
       	extract(shortcode_atts(array('id' => ''), $atts));
       	include ('user-profile.php');
       	return $user_hcard;
       	}
       ```
   
 * I can now use the shortcodes wherever needed:
 *     ```
       [user-profile id="1"]
       [user-profile id="2"]
       ...
       ```
   
 * Nothing but net!
 *  Thread Starter [ryanve](https://wordpress.org/support/users/ryanve/)
 * (@ryanve)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/multiple-instances-breaks-shortcode/#post-2117882)
 * Now the only thing I’m wondering is if there’s a way to set the ID to be the 
   $post->ID by default so that if [user-profile] was used without specifying an
   ID, then it would still work. Any ideas?
 *  Thread Starter [ryanve](https://wordpress.org/support/users/ryanve/)
 * (@ryanve)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/multiple-instances-breaks-shortcode/#post-2117962)
 * Solved that too—see [this thread](http://wordpress.org/support/topic/shortcode-attribute-default-value-post-id).

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

The topic ‘Multiple Instances Breaks Shortcode’ is closed to new replies.

## Tags

 * [author](https://wordpress.org/support/topic-tag/author/)
 * [function](https://wordpress.org/support/topic-tag/function/)
 * [ob_end_clean](https://wordpress.org/support/topic-tag/ob_end_clean/)
 * [ob_get_contents](https://wordpress.org/support/topic-tag/ob_get_contents/)
 * [ob_start](https://wordpress.org/support/topic-tag/ob_start/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [profile](https://wordpress.org/support/topic-tag/profile/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [user](https://wordpress.org/support/topic-tag/user/)

 * 3 replies
 * 1 participant
 * Last reply from: [ryanve](https://wordpress.org/support/users/ryanve/)
 * Last activity: [14 years, 10 months ago](https://wordpress.org/support/topic/multiple-instances-breaks-shortcode/#post-2117962)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
