Integrating WordPress Into PHP Scripts

Overview

Today, I had to use a custom PHP script that I created a while ago in a new WordPress application. Instead of developing a WordPress plugin to handle this existing code, I choose instead to use WordPress directly inside my script.

Using WordPress inside your script is a great option if you have a lot of custom PHP code.

When you have a large PHP code base from your previous development efforts. There are typically other libraries inside your code, such as Symfony, Zend Framework, Pear, etc, that you don’t want to take apart. You have working code and you want it to stay together.

In my case, I had created a form for data collection for a client. But this was no ordinary form. It had over 100 form variables. They were made up of text input, radio boxes, checkboxes, and textarea’s. I wanted to use this form directly inside my WordPress application. Plus I wanted the integration to easy.

The solution was actually quite simple.

Existing PHP Code

For example, assume this is my existing PHP code (it is a simple script to illustrate this point).

</p> <form name="datacollection" method="post" action="index.php"> <input type="text" name="first_name" value="<php echo $_POST['first_name'];?>&#8220;><br /> <input type="submit" value="Submit Data"><br /> </form> <p>

But the problem is, that is only the form to collect the data. What about the look and feel of the existing site. How can I get the above code to look like the same template layout of my WordPress application.

For example, one solution is to add the raw HTML. But that is both time consuming, and it is not dynamic. If the WordPress template changes, I have to go in here and manually change it again. Not something I want to do.

<head><br /> <title>My PHP Form Page<br /> </head><br /> <body></p> <form name="datacollection" method="post" action="index.php"> <input type="text" name="first_name" value="<php echo $_POST['first_name'];?>&#8220;><br /> <input type="submit" value="Submit Data"><br /> </form> <p></body><br /> </html>

But that wouldn’t exactly work. I would have to match the navigation, include the style sheets, and make sure the HTML matched exactly the look and feel of my main website.

The Better Solution

Instantiate WordPress, and use the built in functions inside your code.

<php // include the WordPress loader file $root = $_SERVER['DOCUMENT_ROOT']; require( $root. '/wp-load.php' ); // call the WordPress header function get_header(); ?></p> <form name="datacollection" method="post" action="index.php"> <input type="text" name="first_name" value="<php echo $_POST['first_name'];?>&#8220;><br /> <input type="submit" value="Submit Data"><br /> </form> <php // include the footer get_footer(); ?>

Do you see how easy that was? WordPress is instantiated right in my page. Then I instruct the page to call the header and footer functions of WordPress.

The header and footer functions provide everything I need for the template of my WordPress site. It automatically populates the HTML, CSS, and any Javascript up on top, and on the bottom it closes any tags that were open.

Conclusion

Let PHP do the work. There is no need to duplicate the WordPress code in pure HTML when you can have it generated automatically by WordPress itself.

The added benefit is that if the main WordPress site layout ever changes, the template wrapping your custom PHP code will reflect those changes immediately.



Source

Leave a Reply