Title: PHP Code inside Shortcodes
Last modified: August 19, 2016

---

# PHP Code inside Shortcodes

 *  [Alberto](https://wordpress.org/support/users/ahorihuela/)
 * (@ahorihuela)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/php-code-inside-shortcodes/)
 * Hey guys,
 * I have been struggling to figure this out. Is there any way to write a php code
   inside a shortcode to display it in a template? He is what I mean:
 * Take this:
    `<?php echo do_shortcode("[link item='example']"); ?>`
 * And turn it into this:
    `<?php echo do_shortcode("[link item='SOME PHP HERE']");?
   >`
 * What I am trying to do is define the item=’value’ in each post customfield and
   have the shortcode read that data as php, instead of defining the content inside
   the ” as text.
 * Is that possible somehow?
 * Thank you gang!

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

 *  [racer x](https://wordpress.org/support/users/racer-x-1/)
 * (@racer-x-1)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/php-code-inside-shortcodes/#post-1882532)
 * This should work:
 * `do_shortcode("[link item='{$variable}']");`
 * (Note: use double quotes on the outer.)
 *  [Curtiss Grymala](https://wordpress.org/support/users/cgrymala/)
 * (@cgrymala)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/php-code-inside-shortcodes/#post-1882549)
 * That should work, as should:
 *     ```
       do_shortcode('[link item="' . $variable . '"]');
       ```
   
 * Keep in mind that this is obviously only possible when calling the `do_shortcode()`
   function from within a PHP file. There wouldn’t really be any safe way to use
   PHP code within an actual shortcode in a post or page.
 *  Thread Starter [Alberto](https://wordpress.org/support/users/ahorihuela/)
 * (@ahorihuela)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/php-code-inside-shortcodes/#post-1882592)
 * AWESOME GUYS! Thanks for the help here – will test it out tonight and see how
   it works.
 *  Thread Starter [Alberto](https://wordpress.org/support/users/ahorihuela/)
 * (@ahorihuela)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/php-code-inside-shortcodes/#post-1882815)
 * Guys – thanks again for the help – worked like a charm!
 *  [racer x](https://wordpress.org/support/users/racer-x-1/)
 * (@racer-x-1)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/php-code-inside-shortcodes/#post-1882816)
 * You are very welcome!
 *  [darkscot](https://wordpress.org/support/users/darkscot/)
 * (@darkscot)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/php-code-inside-shortcodes/#post-1882998)
 * Trying to put php inside the shortcode. Each page assigned a number $id_number
 * <?php echo do_shortcode(“[events label='{$id_number}’]”); ?>
 * This should show only those events with label value which matches the page number.
   It ends up showing them all. Any ideas anyone?
 * p.s. <?php echo $id_number ?> does work and returns the value of the page number.

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

The topic ‘PHP Code inside Shortcodes’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 4 participants
 * Last reply from: [darkscot](https://wordpress.org/support/users/darkscot/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/php-code-inside-shortcodes/#post-1882998)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
