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).
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.
My PHP Form Page
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.
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.
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.[ad_2]