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 .
The Result
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: [change url="https://www.change.org/p/sack-salim-mehajer-and-auburn-council"]
.
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.
The Shortcode
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 Tools
menu.
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.
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
Shortcode Attributes
The following attributes apply to the first function.
apikey
url
width
height
data
data="0"
.font
font: normal 12px verdana
.cache
cache="3600 * 12
).num_words
Return Data Types
The second function returns specific data from the array returned by the Change API.
Date Type Attributes
apikey
url
type
cache
cache="3600 * 12
). Try and keep it the same as the first function to avoid caching two responses of the same data.num_words
letter_body
attribute is used, it will be truncate to num_words
.format
created_at
or end_at
attribute is used, it will be formatted with the PHP date() function. Default is jS F Y
.PHP Function
A PHP function to be used outside of WordPress is included below. Usage requires Simple Cache.
Considerations
- 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
$bgcolor
variable. Additionally, the percentage at which the bar changes color can be altered in the same block of code.
Download
Title: Show a Change.org Progress Bar in WordPress (Plugin)
Description: Show a Change.org Progress Bar with WordPress Shortcode or PHP.
Download • Version 0.2, 4.2K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (2.2K) PHP Code & Snippets, (2.3K)