Can I run custom php on specific pages in wordpress?

I’m making a wp site as a new frontend to an in house system that I designed a while back.

I want to be able to run custom php for grabbing different bits and peices of stuff from my database (mysql) and displaying dynamic data etc , but can’t seem to do this.

I thought the code snippets plugin may have helped, but whilst that does run custom PHP, its not call-able on a per page basis, but rather runs no matter where I am, which is not suitable.

Any help appriciated.

1 Answer
1

If you are using a child or custom theme, I encourage you to look at the Codex’s Template Hierarchy.

The short answer to your question is yes, you can run custom code on any page you’d like.

The longer answer is still yes, but it’s a bit more involved in what your end product is going to look like.

I’ll paste in the part about pages from the aforementioned page:

The template file used to render a static page (page post-type). Note that unlike other post-types, page is special to WordPress and uses the following path:

  • custom template file – The page template assigned to the page. See get_page_templates().
  • page-{slug}.php – If the page slug is recent-news, WordPress will look to use page-recent-news.php.
  • page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php.
  • page.php
  • singular.php
  • index.php

So, you have the ability to create any one of these custom templates, and apply them to your page, running any code you need.

Leave a Comment