Technology admin  

Integrating WordPress into PHP Scripts

General description

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 to use WordPress directly within my script.

Using WordPress within 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. Usually there are other libraries within your code, like Symfony, Zend Framework, Pear, etc., that you don’t want to disassemble. You have a work code and you want it to stay attached.

In my case, I had created a data collection form for a customer. But this was not an ordinary way. It had more than 100 shape variables. They consisted of text input, radio boxes, check boxes, and text areas. I wanted to use this form directly within my WordPress application. Also, I wanted integration to be easy.

Actually, the solution was quite simple.

Existing PHP code

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

</p> <form name="datacollection" method="post" action="index.php"> <p><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’s just the way to collect the data. What about the appearance of the existing site? How can I make the above code look like the same template design of my WordPress application?

For example, one solution is to add the raw HTML. But that takes a long time and is not dynamic. If the WordPress template changes, I have to go here and change it again manually. It is not something you want to do.

<head><br /> <title>My PHP form page<br /> </head><br /> <body data-rsssl=1></p> <form name="datacollection" method="post" action="index.php"> <p><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 stylesheets, and make sure the HTML exactly matches the look and feel of my main website.

The best solution

Create an instance of WordPress and use the built-in functions within 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"> <p><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 it was? WordPress is instantiated directly on my page. Then I instruct the page to call the WordPress header and footer functions.

The header and footer functions provide everything I need for my WordPress site template. It automatically completes the HTML, CSS and any Javascript in the upper part, and in the lower part it closes all the tags that were open.

Conclution

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

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

Leave A Comment