Wednesday, February 22, 2012

Arbitrary Code Plugin Redux

A while back I wrote a horrific plugin for getting arbitrary PHP code into a Wordpress install. But there's a much easier way. Since you're obviously willing to edit PHP files anyway:
  • Open up you functions.php for your theme. (You could create a file that functions.php includes, too, if you want to get fancy.)
  • Get your shortcode function in there:

function include_file( $atts ){
    ob_start();
    $include_name = $atts['name'];
    include_once $include_name;
    $retVal = ob_get_contents();
    ob_end_clean();
    return $retVal;
}
add_shortcode( 'include', 'include_file' );
  • Use your shortcode by including [include name='./whatever.php'] in your page or post.

No comments: