The WordPress shortcode (and PHP function) on this page will render a Change.org petition progress bar onto your WordPress post or page. As the count petition incrementally increases the color of the graph will change until it has reached its goal (100%) - at which time the graph will change to green.
Before the shortcode function will work, you must first register for an API Key here .
Using the sample petition here , the following will be rendered into your WordPress page or post (we have no affiliation with the campaign whatsoever... and yes, he was sacked).
Petition is now closed.
While the HTML generated in our example is quite minimal, quite a lot of information is returned by the API and stored locally in your own WordPress database (as a time-limited transient). Alter the reference to
$num_words to return more text, and alter the actual HTML with the data referenced in the code.
Shortcode used was as follows:
An example of a campaign without text is as follows:
Petition is now closed.
Display Other Types of Data
To show a count only, use
[changedata]. The result: 0. The same shortcode can be used to return other array values via an attribute. For example,
[changedata type="percent"] returns NAN%, and
[changedata type="created_at"] returns 1st January 1970.
Copy and paste the WordPress function into your theme's
functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.
If you download the plugin, you'll find an option to update your API key in the
The first function retrieves the change data and stores it as a WordPress transient.
One you have the data, you can render it any manner of your choosing. The following will take the cached data and return a progress bar.
The following attributes apply to the first function.
font: normal 12px verdana.
cache="3600 * 12).
Return Data Types
The second function returns specific data from the array returned by the Change API.
Date Type Attributes
cache="3600 * 12). Try and keep it the same as the first function to avoid caching two responses of the same data.
letter_bodyattribute is used, it will be truncate to
end_atattribute is used, it will be formatted with the PHP date() function. Default is
jS F Y.
A PHP function to be used outside of WordPress is included below. Usage requires Simple Cache.
- While we've used a fairly crude CSS div for rendering the percentage graph, it's often more effective to use Google charts or any of the other open source graphing applications written for PHP applications.
- The incremental colors used in the progress bar can be altered by altering the
$bgcolorvariable. Additionally, the percentage at which the bar changes color can be altered in the same block of code.