Using PHP in WordPress3 Posts and Pages

A project we are working on at Captain Jack Communications sent me googling for a solution today, when we needed to extend WordPress and provide some custom database programming within a particular page. From my own personal dabblings in such things, I knew there was a plugin (or two) that allowed you to put PHP in text box widgets and pages.

I went to the page for Exec PHP plugin, and despite reading the warnings, decided to give it a try – no luck.  It seems it does not work on the latest version of WordPress.  I also learned of another one that would do includes, but again, no luck.  So, after thinking for a bit, I came up with the only solution, and maybe it actually is the best solution.

I connected to the server via FTP and navigated to the themes folder, and downloaded the page.php file of the theme I was using.  I renamed it, in this case, rankings.php and uploaded it.  Then I used my FTP client to change the permission to 777 so it could be edited from within wordpress.

From there, I edited the new file, making sure the first few lines were:

[sourcecode language="php"]
<?php
/*
Template Name: Rankings
*/

get_header();
?>
[/sourcecode]

After that, I had the entire page the same as the theme’s default template, and where I wanted my PHP to be I added this line:

[sourcecode language="php"]
<? include ABSPATH . "/wp-content/custom-php/ranking_page.php"; ?>
[/sourcecode]

I then uploaded my PHP script(s) to that new custom-php directory where they lived happily ever after.

Now, when I add a page in the admin area, on the right hand side under Page Template, it asks me if I want the rankings.php page or the template’s default.  I choose the rankings.php page, hit publish, and my code is now integrated into that page only.

I hope this makes sense.  If anybody knows of a PHP plugin that actually works with WordPress 3, please let me know.  But I think this way is far more secure and coder-friendly.

Speak Your Mind