Title: Sharing attributes between shortcodes
Last modified: January 30, 2019

---

# Sharing attributes between shortcodes

 *  [Felty](https://wordpress.org/support/users/felty/)
 * (@felty)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/sharing-attributes-between-shortcodes/)
 * I am working on a plugin that will award points for watching Twitch streams embedded
   on a WordPress site.
 * So far I am going to have:
 * `[vy-twitch]` The video.
    `[vy-twitch-chat]` The chat. `[vy-twitch-vyps]` The
   optional location where you want to show the stats.
 * Now… You obviously need `[vy-twitch channel=yourchannel]` at least once, but 
   I thought to make it easier on the user to put it in just the video one (since
   you always will need that) but if they use the optional chat and stats on the
   same page…
 * Is there a way to read the attribute from the one shortcode and propagate it 
   to all the other ones?

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

 *  [Howdy_McGee](https://wordpress.org/support/users/howdy_mcgee/)
 * (@howdy_mcgee)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/sharing-attributes-between-shortcodes/#post-11151003)
 * What if the user ( by accident or intention ) has multiple twitch videos on the
   same page? I think you’re better off having the channel set in each shortcode.
   Trying to daisychain these shortcodes will only lead to headache down the line
   as the plugin grows and features get added.
 * For usability it may be best to create a page to generate these shortcodes for
   the user. Maybe a post type that generates the shortcodes and allows the user
   to set options for the different shortcode.
 * – – – – – –
 * If you really want to do something like this you can search the content for your
   specific shortcode: [https://stackoverflow.com/a/32525101/800452](https://stackoverflow.com/a/32525101/800452)
 *  Thread Starter [Felty](https://wordpress.org/support/users/felty/)
 * (@felty)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/sharing-attributes-between-shortcodes/#post-11151182)
 * That’s a good point. Generally, I was assuming it would be the content creator
   doing this themselves which would only be one stream at a time, but if you had
   a curator, that would be more of an issue.
 * I’ll require each of them to be set for each, but I’d still be interested if 
   it’s possible as run into it before with other things I’ve done.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/sharing-attributes-between-shortcodes/#post-11154537)
 * Shortcodes essentially call a function whose return is echoed out. Similar issues
   arise in any PHP function wanting data that is out of its scope. Thus similar
   solutions can be used for shortcodes. Globals, static class properties, cookies,
   session vars could all be used to get values that would otherwise be out of scope.
 * Your shortcode handler could take passed attributes as normal and set the chosen
   global scope mechanism. If no attribute value is passed, attempt to get the value
   from that global scope mechanism. Do some default action if neither are available.
   In the case of possibly multiple attributes used by one person, the value used
   ends up being the last value explicitly passed. It’s similar to trying to use
   global $post outside of the loop. Something we can do, but it’s not really a 
   good idea.

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

The topic ‘Sharing attributes between shortcodes’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 4 months ago](https://wordpress.org/support/topic/sharing-attributes-between-shortcodes/#post-11154537)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
