Skip to content

Instantly share code, notes, and snippets.

@salcode
Created September 8, 2017 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salcode/8ecdec92044b084881b99e78067b4b0b to your computer and use it in GitHub Desktop.
Save salcode/8ecdec92044b084881b99e78067b4b0b to your computer and use it in GitHub Desktop.
Display PHP output on a WordPress page

Context

https://twitter.com/felafel/status/906162644125659136

@salcode Met you at WC Lancaster. Looking to embed existing php code (not a snippet) in a WP page. Tried a few plugins. Suggestion?

My Thoughts

Displaying PHP output on a WordPress page can vary depending on where you want to display the output.

Display in the Content

I'd use a WordPress shortcode (I'm giving a talk on Creating Your First Shortcode at WordCamp Philly 2017).

Display Where There is a WordPress Action

This is most likely relevant if you're using the Genesis framework. In this case you can hook into the relevant action and put your relevant output in the function you attach to the hook.

Display Template Specific

Finally, you can modify the specific template being displayed. This is going to depend a lot on your theme. One important thing to remember is you can create a page template specific to the post_id, (e.g. page-375.php), see https://wphierarchy.com/.

Good Luck

I hope this helps. I'd be interested to hear how you ultimately solve the problem.

@felafel
Copy link

felafel commented Oct 25, 2017

I ended up taking the page.php for the theme and making my own template page. I just inserted my code in the content area. I am thinking of adding the custom page code as a php include so that it is more clean and maintainable. Making it a widget was not working for me so tried the template and it did. I got the suggested steps from here:

https://stackoverflow.com/questions/2810124/how-to-add-a-php-page-to-wordpress

P.S,. that wphierarchy site is cool.

Thanks for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment