• May 29, 2023

WordPress integration in PHP scripts

General description

Today, I had to use a custom PHP script I created a while back in a new WordPress app. Rather than develop 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. There are usually other libraries within your code, such as Symfony, Zend Framework, Pear, etc., that you don’t want to take apart. You have a working code and you want it to stay together.

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

The solution was actually 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 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 layout of my WordPress app?

For example, one solution is to add the raw HTML code. But that is very time consuming and not dynamic. If the WordPress template changes, I have to go in here and change it back manually. It’s not something I 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 style sheets, and make sure the HTML exactly matched 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(); ?>

See how easy it was? WordPress is instantiated directly on my page. I then instruct the page to call the WordPress header and footer functions.

The header and footer features provide everything I need for my WordPress site template. It auto-fills HTML, CSS, and any Javascript at the top, and closes any open tags at the bottom.

Conclusion

Let PHP do the work. There’s no need to duplicate WordPress code in pure HTML when you can have WordPress auto-generate it.

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 Reply

Your email address will not be published. Required fields are marked *