Title: Auto-Increment a Variable Each Time a Shortcode is Called
Last modified: August 19, 2016

---

# Auto-Increment a Variable Each Time a Shortcode is Called

 *  [mhoiland](https://wordpress.org/support/users/mhoiland/)
 * (@mhoiland)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/auto-increment-a-variable-each-time-a-shortcode-is-called/)
 * Hello,
 * I’m writing a plugin that requires a unique identifier if a post contains more
   than one instance of the shortcode.
 * Here’s a shortcode function for example:
 *     ```
       function foo() {
       	$i = 0;
       	$i++;
       	return $i;
       	}
       add_shortcode('foo','foo');
       ```
   
 * If I enter the shortcode twice in a post like this…
 *     ```
       [foo]
       [foo]
       ```
   
 * it will return this…
 *     ```
       1 1
       ```
   
 * But I would like it to return…
 *     ```
       1 2
       ```
   
 * Any ideas?

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

 *  Thread Starter [mhoiland](https://wordpress.org/support/users/mhoiland/)
 * (@mhoiland)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/auto-increment-a-variable-each-time-a-shortcode-is-called/#post-1575859)
 * Nobody with mad shortcode skills?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/auto-increment-a-variable-each-time-a-shortcode-is-called/#post-1575884)
 * not sure if it will work, but look into ‘static’ php variables:
    [http://www.tutorialspoint.com/php/php_static_variables.htm](http://www.tutorialspoint.com/php/php_static_variables.htm)
 *  Thread Starter [mhoiland](https://wordpress.org/support/users/mhoiland/)
 * (@mhoiland)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/auto-increment-a-variable-each-time-a-shortcode-is-called/#post-1575903)
 * Brilliant! Works perfectly.
 *     ```
       function foo() {
       	STATIC $i = 0;
       	$i++;
       	return $i;
       	}
       add_shortcode('foo','foo');
       ```
   

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

The topic ‘Auto-Increment a Variable Each Time a Shortcode is Called’ is closed 
to new replies.

## Tags

 * [increment](https://wordpress.org/support/topic-tag/increment/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 3 replies
 * 2 participants
 * Last reply from: [mhoiland](https://wordpress.org/support/users/mhoiland/)
 * Last activity: [15 years, 11 months ago](https://wordpress.org/support/topic/auto-increment-a-variable-each-time-a-shortcode-is-called/#post-1575903)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
