Routing a custom php on wordpress

If you’re coming from a Laravel background, you’re going to hate WordPress routing.

For our high-functioning WordPress websites, I had to write a whole bunch of routing workarounds to Laravel-ise routes, and it’s definitely not “The WordPress Way.”

There’s no neat WordPress way to add routes in code. Your best bet is to leverage the default routing by creating a Page at the URL you want, and then use a custom Page Template.

Here’s how.

I’m going to assume you have control over your theme and it’s located here: yoursite/wp-content/themes/your-theme

Add a new file:

yoursite/wp-content/themes/your-theme/mygetform.php

Add some WordPress-magic to the top of this file:

<?php
/**
 * Template Name: Custom Page - My Get Form
 * Description: Here's my custom page.
 */
/* include your template header here, if you need/want it. */

/* insert your custom code here */

/* include your template footer here, if you need/want it. */

Now head to yoursite.com/wp-admin and navigate to Pages > Add Page.

In your Page Attributes metabox (which normally hides on the right hand side somewhere) you’ll now be able to select “Custom Page – My Get Form” from the Template drop down.

This will now run all PHP you’ve dropped into this file (or included, etc).

If you’ve got forms and stuff, you could also include it in this file and that would work. The most WordPress way to handle GET/POST form submissions is by leveraging admin-post.php, which is best documented in this article.

Leave a Comment