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 ){
    $include_name = $atts['name'];
    include_once $include_name;
    $retVal = ob_get_contents();
    return $retVal;
add_shortcode( 'include', 'include_file' );
  • Use your shortcode by including [include name='./whatever.php'] in your page or post.

No comments: