Mar 202010
 

Recently I installed the simple, but beautiful Suffusion theme by Sayontan Sinha. This theme is using the funtions.php file extensively, the same place where my shortcode definitions were stored. Sayontan will probably fix this in the near future by including a custom functions.php file from another location. I also used some PHP code in two sidebar widgets that was executed by the Exec-PHP plugin.

I thought, why not use a plugin to define shortcodes and execute PHP code snippets? I couldn’t find one, so I wrote one myself. You can find it in the WordPress repository as Shortcode Exec PHP.

Example shortcode to print your age:

extract(shortcode_atts(array('birthdate' => 'Jan 1, 1980'), $atts));
return floor((time() - strtotime($birthdate)) / (60 * 60 * 24 * 365.2425));

If you named the shortcode age, you can execute it in pages, posts, comments, widgets and/or RSS feeds like this:

[age birthdate="Mar 20, 2010"]

Please, use the forum for questions, bug reports and feature requests.

If you know a nice shortcode, you can leave it in the forum too.

Install now

Listing posts

If you want to list posts or pages on a post, page or in a widget, you can use the code below.

// Prevent recursion
global $outerPost;
if ($outerPost)
	return '';
 
// Parse parameters
extract(shortcode_atts(array('query' => 'post_type=post'), $atts));
$query = html_entity_decode($query);
 
// Create new post loop
global $post;
$outerPost = $post;
$my_query = new WP_Query($query);
while ($my_query->have_posts()) {
	$my_query->the_post();
	setup_postdata($my_query->post);
 
	// Process post
	echo '<em>'; the_title(); echo '</em>';
	echo '<div style="font-size: xx-small; border: 1px solid;">';
	the_content();
	echo '<div>';
}
$post = $outerPost;
$outerPost = null;
setup_postdata($post);

Example usage:

[list_posts query="author_name=marcel"]

Look here for possible query parameters.
Be sure the shortcode option ‘Output echoed‘ is checked.

Using forms

Example of form handling:

if (isset($_POST['form_select']))
	echo 'You selected ' . $_POST['form_select'] . '<br />';
?>
<form method="post">
<select name="form_select">
<option selected="selected" value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
<input type="submit" value="Submit" />
</form>

Be sure the shortcode option ‘Output echoed‘ is checked.

 Posted by at 16:11  Tagged with:

  600 Responses to “WordPress plugin: Shortcode Exec PHP”

  1. Support questions can be asked in this forum now.

  2. I need to pass variables from the query string into posts, form submits, etc.. for example… http://internetmarketingsuperfriends.com/get-a-free-episode/?src=google&kw=internet+marketing&subid=22&cid=44

    For some reason this is completely escaping me how to capture these vars from the query string??

    I am using Thesis & Themedy, have custom.php file with no mods.

    I have the shortcode “first” created, enabled, and output echoed, then also placed into the post here http://internetmarketingsuperfriends.com/2011/05/12/whats-up/?first=Mike

    thanks in advance for any guidance you can offer.

    – Mike

  3. Hi,

    Thanks for a great plugin which I use extensively in my games site.

    I’ve searched through the files but can’t find where the php code for each shortcode is stored.

    Please could you advise where the code is to put my mind at rest, I’d hate to attempt an update of some sort and lose it all, as I’m not a great coder and would never be able to recreate.

    Thanks
    Mark

    • You can use the export function to export all shortcodes definitions, including the code you have written. The shortcode definitions are stored as options and therefore not easily accessible.

  4. Hi,

    Can I use the code from addthis.com to display the social mediabar through your plugin?
    The code looks like this:

    var addthis_product = ’wpp-262;
    var addthis_config = {”data_track_clickback”:true,”data_track_addressbar”:false};if (typeof(addthis_share) == ”undefined”){ addthis_share = [];}

    Thanks,
    Dennis