This is one of my all-time useful and favorite WordPress shortcodes ever.  It’s so simple, just put the code below into your active theme’s function.php file and now you have a very flexible and powerful shortcode where you can embed external PHP — provided the PHP code is in a file on your own site, of course!

function insert_script($atts){

extract(shortcode_atts(array("path" => '',"vals"=>''), $atts));
	ob_start();
		include($_SERVER['DOCUMENT_ROOT'].$path);
	return ob_get_clean();
}

add_shortcode('insert_script', 'insert_script');

Here’s an example of its use.  If you aren’t familiar with PHP’s parsestr() function, please click here.  It’s a handy way to take any regular query string of variables you’d find in a URL, like ?s=1&action=go and “parse” them out into variable/value matches like:  $s=1, $action=”go”, and so on….

This way, you pass useful variables in a verbose way  in your code — it’s pretty transparent so if the code you’re including isn’t cryptic and is easy to follow, then so will the use of this shortcode in your WordPress pages and posts!

Just put this shortcode in any of your pages or posts, or widgets if you’ve managed to turn on shortcodes in your widgets.  If not, click here to see how easy that is.

[insert_script path='/includes/db/database-connection.php' vars="cmd=show_table"]
//Will find the script at the web root includes folder, parse the variables in the query string "vars" with the PHP command "parsestr()" and execute the PHP


It comes in handy — to be able to use those dynamic and very useful shortcodes in more places.  I’m sure the developers of WordPress have a reason to make developers have to put this extra line in the active theme’s function.php file, so here it is.  Yes, it’s that simple!


add_filter('widget_text', 'do_shortcode');


<?php define('WP_DEBUG', true); ?>

When you’re in development mode, it’s useful to get errors in scripts as they occur.  Just place this line anywhere in your functions.php file for your active child theme and you will start seeing debug feedback complete with script names and line numbers.  You can just remove this line, or set “WP_DEBUG” to false if you want to shut off debugging or definitely before you go live!


Find the line in your child theme’s “comment.php” file where it calls on the function comment_form() and replace that line with this:

<?php comment_form(array('comment_notes_after' => '')); ?>