0

I am a Laravel developer that is learning WordPress. I have written some PHP code which displays some tables and some HTML and some get forms. At the minute I require the file on the index page in order to display it. I want to change that and only add a menu on the index page with links to my created files. Now I want to route the file, so when I write its URL I see the page from that file. e.g. mywpsite.com/mygetform How do I do this?

  • to display in a page something generated in PHP, you can use a shortcode : codex.wordpress.org/Function_Reference/add_shortcode – mmm Oct 8 '17 at 23:25
  • in the WordPress philosophy, we don't start to write PHP code and after adding it to the website. It's recommanded to start from what is already shown with WordPress and then searching how we can change this by using hooks, shortcodes, custom post type, widgets, etc. – mmm Oct 8 '17 at 23:31
0

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.

0

Agree with the comments, but still might as well provide an answer for someone coming from a place of some sophistication, and trying to learn.

A preferred method in most real world cases would be to open up the appropriate template file functioning as the equivalent of index.php (in the main directory), which will be set by your theme (according to the hierarchy): Most likely either home.php or index.php and possibly front-page.php, depending both on the particular theme and on general settings. Those template files will be found in the main directory of the active theme (in wp-content/themes/).

Since the question implies that the new "index" page will have no other content, you could in theory delete everything you don't want in the final HTML or understand, from header to footer, and replace it with you code, but most likely (why use Wordpress otherwise?), you'll want to keep almost everything except for the stuff between while if (have_posts() ) and its related endif, if present. ('The Loop', in other words.)

Furthermore, you might as well start now creating child themes and doing the modifications there.

Another alternative will likely be to add the code via shortcode or some other method involving a custom function or conceivably a plugin, possibly to a page set as the home page.

Since it's WordPress, there will be additional alternatives...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.