So you created a PHP based web application and now you want to integrate it into WordPress. No problem. I will show you how to accomplish this. The benefit to “pressing” your PHP web-based application is that when your site’s design updates, because someone has added a new post or page and thereby, updating the site’s navigation; you do not have to go back and update the web-application.
For this demonstration, we are going to use a simple calculator that I built for Alltec Corporation and integrate it into a post on this site (I will change the “real” values of this calculator). Note: this is a quick, simple way to get your php application into Wordpress and may not be suitable for all applications.
Step One: Removing Unnecessary HTMLWordPress is going to generate the layout of your page: html head, footer, sidebars, navigation, etc. So, you will want to remove these item out of your code. Basically all that you want is the HTML code necessary to display your application.
- Remove any head information, <head></head>
- Remove <html></html> tags
- Remove <body></body> tags
- Remove any code relating to navigation, side bars, header images or logos
All that should remain is the center content where your application would be displayed.
Step Two: Checking Variable and Function Names
You will need to check your code to make sure you haven’t used any WordPress or theme variable and function names. For example, $name, $value, $deprecated, $autoload, $option, $option_name, $newvalue are variables used in the function update_option. If your PHP code contains any of these names, you should change them to ensure your plugin works correct and actually installs. It is a wise idea to name your variables uniquely such as bc_variableName or bc_FuntionName. This will make sure your plugin functions correctly. If you plan on publishing your plugin, which requires more review and planning than this tech-tip, giving your variable and function names descriptive, unique names is a must.
- Make sure your variable and function name do not conflict with your theme and WordPress.
- Give your variables and functions meaningful, plugin dependent names.
Step Three: Create Your File Headers
The first thing you must include in your main PHP is the standard plugin information header. Without it, WordPress will not recoginze your plugin; it will never be activated and it will never run.
At minimum you have to have the line:
- Plugin Name line
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin’s Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
Step 3: Create plugin hooks
In this example, we will prepare the code for WordPress hook, add_shortcode. The add_shortcode function will give a name that can be called in your post and tell WordPress what function of your code to execute. When you designed your php application, you may have set it up to handle user events: has the form has not been submitted, do this; has the form been submitted, do this. Following this action handling code, you may have a list of functions. You will want to wrap these action handlers in a function. In this example, I have wrapped all my action handlers in the function run_bc(). I used links back to the Bracket Calculator PHP file so the user could click the link and run another calculation. Since we are pressing this file, we will need to change those URLs with a permalink. You can use the get_permalink to reference the page or post to which the form will be submitted.
- Wrap the action handling events, which call other application functions, in a function to be called by WordPress when your short code is used
- Make sure that your form’s action uses the get_permalink function
Next we will want to add our short code function. In this case, our function will have the short code text used to call the plugin into action and the function which it will run.
- add_shortcode(‘bracket_calculator’, ‘run_bc’)
Activating your plugin and add it to your website
Now all we have to do is install our plugin and add it to a page. To install your plugin, you will need to upload it to your web-server. First you will want to create a folder that is descriptive of the project’s name. For this program, my folder name is bracket-calculator. If you plan on using the upload function of WordPress, you will need to zip the folder. Then use the interface to upload the zipped folder. WordPress will unzip it and put it in the correct directory. If you are going to manually upload the folder’s content, you will upload the folder to the wp-content/plugins folder.
After you have uploaded your program, you will go to the plugins interface of the WordPress administrative panel. If your code is correct and it is uploaded to the correct directory, you should see it listed as Inactive in the plugin interface. Activate the plugin by clicking the Activate link.
Now it is time to determine how you are going to post this application: as a page or a post. In this example, I am posting it as a post. However, I normally find that I post my PHP applications as pages. Choose the type of post you will create. Open up the new post/page. Now you will add your shortcode that will call your plugin and cause it to run on your page.
[ bracket_calculator ]Note: the spaces before and after “bracket_calculator must removed. These were added to prevent WordPress from embedding the application in this post.
Preview your page to test it. If you have gotten any page not found errors, it might because one of your variable names is one that WordPress or your themes uses. After you have confirmed that it works, you can publish your page.
Upcoming Tech-tips based on this entry
- Programming a simple PHP application
- Making your code better
- Adding a WordPress Options page for your WordPress plugin
- Adding database to your PHP application
These are some of the resources I used to create my application
Viewed 19280 times