Title: Return (and call) shortcode from php function
Last modified: January 17, 2017

---

# Return (and call) shortcode from php function

 *  Resolved [premmels](https://wordpress.org/support/users/premmels/)
 * (@premmels)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/return-shortcode-from-php-function/)
 * Hi there,
    on my website I want to call different shortcodes depending on the
   value of some variables.
 * Here is a simple version of my problem:
    1) user fills out a form which will 
   result in some variables that are attached to a URL, e.g. URL?var=18 2) a php
   function embedded in a shortcode ([simple]) reads out the variable ‘var’ 3) depending
   on the value of ‘var’ the simple-shortcode should return a new shortcode that
   should be called with different arguments -> either [shortcode]foo[/shortcode]
   or [shortcode]bar[/shortcode]
 * Here is a minimal version of my simple-shortcode (php function) reading out the
   URL and returning the new shortcode:
 *     ```
       //[simple]
       function simple_shortcode() {
       	 $var = $_GET['var'];
       	 if ($var > 12){
       	    return '[shortcode]foo[/shortcode]';
       	    }
       	    else {
       	    	 return '[shortcode]bar[/shortcode]';
       		 }
       }
       add_shortcode('simple', 'simple_shortcode');
       ```
   
 * This doesn’t result in the shortcode being called with either ‘foo’ or ‘bar’ 
   as an argument but just in a return of ‘[shortcode]xxx[/shortcode]’ as a string.
 * —-
 * Adjusting [simple] to just return either ‘foo’ or ‘bar’ and using
    `[shortcode][
   simple][/shortcode]` on the website doesn’t work either.
 * —-
 * I hope I made clear what I want to achieve.
 * Are there any ideas how to do it (differently)?
 * (I’m quite new at php and it’s possible that the above code isn’t the prettiest
   version possible but it works in principle.)
 * Thanks,
    Patrick
 * edit: the shortcode [shortcode]xxx[/shortcode] I want to call is from a plugin
   and not a shortcode of myself.
    -  This topic was modified 9 years, 2 months ago by [premmels](https://wordpress.org/support/users/premmels/).
    -  This topic was modified 9 years, 2 months ago by [premmels](https://wordpress.org/support/users/premmels/).
    -  This topic was modified 9 years, 2 months ago by [premmels](https://wordpress.org/support/users/premmels/).

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

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 2 months ago](https://wordpress.org/support/topic/return-shortcode-from-php-function/#post-8665911)
 * You want to use the function do_shortcode to execute a shortcode
 * [https://developer.wordpress.org/reference/functions/do_shortcode/](https://developer.wordpress.org/reference/functions/do_shortcode/)
 * `return do_shortcode( '[foo]stuff[/foo]' );`
 *  Thread Starter [premmels](https://wordpress.org/support/users/premmels/)
 * (@premmels)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/return-shortcode-from-php-function/#post-8665930)
 * You saved my day 🙂
    Thank you Steve

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

The topic ‘Return (and call) shortcode from php function’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [premmels](https://wordpress.org/support/users/premmels/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/return-shortcode-from-php-function/#post-8665930)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
