Integrating WordPress Into PHP Scripts

Overview

As we speak, I had to make use of a customized PHP script that I created some time in the past in a brand new WordPress software. As a substitute of creating a WordPress plugin to deal with this current code, I select as a substitute to make use of WordPress straight inside my script.

Utilizing WordPress inside your script is a superb possibility if in case you have a number of customized PHP code.

When you’ve gotten a big PHP code base out of your earlier growth efforts. There are sometimes different libraries inside your code, akin to Symfony, Zend Framework, Pear, and so forth, that you do not wish to take aside. You could have working code and also you need it to remain collectively.

In my case, I had created a kind for information assortment for a consumer. However this was no unusual kind. It had over 100 kind variables. They had been made up of textual content enter, radio packing containers, checkboxes, and textarea’s. I wished to make use of this way straight inside my WordPress software. Plus I wished the mixing to straightforward.

The answer was really fairly easy.

Current PHP Code

For instance, assume that is my current PHP code (it’s a easy script for instance this level).

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

However the issue is, that’s solely the shape to gather the info. What about the feel and appear of the present website. How can I get the above code to appear to be the identical template format of my WordPress software.

For instance, one answer is so as to add the uncooked HTML. However that’s each time consuming, and it’s not dynamic. If the WordPress template modifications, I’ve to go in right here and manually change it once more. Not one thing I wish to do.

<head><br /> <title>My PHP Type Web page<br /> </head><br /> <body data-rsssl=1></p> <form name="datacollection" method="post" action="index.php"> <enter sort="textual content" identify="first_name" worth="<php echo $_POST['first_name'];?>&#8220;><br /> <input type="submit" value="Submit Data"><br /> </form> <p></body><br /> </html>

However that would not precisely work. I must match the navigation, embody the type sheets, and ensure the HTML matched precisely the feel and appear of my principal web site.

The Higher Answer

Instantiate WordPress, and use the in-built capabilities 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"> <enter sort="textual content" identify="first_name" worth="<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 straightforward that was? WordPress is instantiated proper in my web page. Then I instruct the web page to name the header and footer capabilities of WordPress.

The header and footer capabilities present all the things I would like for the template of my WordPress website. It mechanically populates the HTML, CSS, and any Javascript up on prime, and on the underside it closes any tags that had been open.

Conclusion

Let PHP do the work. There is no such thing as a have to duplicate the WordPress code in pure HTML when you may have it generated mechanically by WordPress itself.

The additional benefit is that if the primary WordPress website format ever modifications, the template wrapping your customized PHP code will mirror these modifications instantly.



Source by Paul Markovich

You May Also Like

 

ad blocker noticead blocker notice