Web Development, Wordpress

Creating PHP Scripts in WordPress

As WordPress becomes more popular as a website development platform it is constantly be pushed to the limits. What should be simple blogging platform has been expanded to full blown websites with stores and a host of other integrations. However, for every addition there isn’t a plugin to drop in. In many cases your site may just act a middle layer between two APIs and you just need to push data. I won’t go into all the cases but the following steps can be used for a variety of problems.

To create a custom script, say for a web-hook or third-party integration the solution is actually quite simple. The trick is to use what WordPress has already allotted you: templates.

  1. Create a New Template Page
  2. Within the theme folder create a new template file with a PHP extension. For example, I’m writing an endpoint to handle a web-hook from Recurly so I name the file template-recurly-webhook.php.

  3. Add Template Comment to File
  4. Once you’ve created the file place the following comment block at the top of the file. Give it an appropriate name for what you plan on using the file for. From here you can write any code that needs to be executed. Keep in mind you’ll have access to all of WordPress’s functions since we’re working within the templates. Once finished upload the file back to the server if you haven’t already.

    /*
    * Template Name: Recurly Webhook Template
    */
    
  5. Create A New WordPress Page
  6. In order to make an endpoint for this new script we need to piggyback on the WordPress MVC like framework. Create a new page and give it a name that accurately reflects URL that you want it to be. Then from the template drop-down you should find the new template file you created. Publish the page and your done. Now when you visit that page your code will execute. A fun trick without having to tear apart the insides of WordPress or writing a plugin.