I've seen many variants of this question but none of the answers I've found yet have answered my question. Not new to PHP but new to WP and yes I've read a lot of docs. So this is where I'm diving in to WP development, please be gentle.

This question is about the best way to display the following:

The client needs a custom user page that will display a combination of data from both an external API result and internal WP content (which will be keyed off that result using a custom field).

For this view (which the client calls a dashboard but I hesitate to use due to WP nomenclature), should I create a custom template and do all the work in there? Should I do this in a plugin in combination with a custom page template?

It seems like most of the answers that are closest to what I'm doing have ended up going in a widget direction. This needs to be a full page view, displaying a list of events and other related assets ( all of which will be WP CPT) that the logged in user owns based on the query of an external CMS API.

I'm tempted to just do all this in a custom page template, but it feels like it has enough reusable logic that it should be packaged in a plugin. It won't be part of a regular post so using a shortcode seems unlikely.

Is it terribly bad practice to use a custom template to hold the code and do the work? All this data and work will only be displayed on this one view. If the logic is contained in a plugin, what's the fastest way to tell that plugin to dump its output on a given page and only on that page?

I'm out in the boonies on mobile so I can edit and clarify as needed, but some guidance even to specific docs would be helpful. Too many tutorials about how to do an api call, not enough (for me) about where best to run that and how to specify where the output goes. I don't need something in every footer or a widget in the sidebar. Were this a straight php project it would be a single file, but I want to do things the right way for WP.

Thank you!


1 Answer 1


In other common PHP frameworks you normally propagate a new route and have a controller as callback print the desired output. In WordPress something like that doesn't seem to be built in - at the moment.

But what I'd recommend is to somehow mimic this approach.

  1. In a custom plugin have a rule that when a certain custom URL is being called a certain custom template will be used to render the main content.
  2. In that plugin provide a function (or multiple functions) that takes care of the business logic and prepares the desired output.
  3. Inside the certain custom template from the first step call that function.

For example.

Plugin Name: My Plugin
Description: Lorem ipsum dolor sit amet.
Version: 1.0
Author: You
Author URI: https://example.com

class MyPlugin {

  public function __construct() {

    add_action('template_include', [$this, 'template_suggestion'];

  function template_suggestion($template) {

    global $wp;

    $current_slug = $wp->request;

    $custom_template = locate_template(['foobar.php']);

    if ($current_slug == 'lorem-ipsum' && $custom_template != '') {

      return $custom_template;

    return $template;

  public static function Foo() {

    // Do whatever you need to do to build your markup.
    $output = '<div>';
    $output .= 'Foo';
    $output .= '</div>';

    return $output;

  public static function Bar() {

    // Do whatever you need to do to build your markup.
    $output = '<div>';
    $output .= 'Bar';
    $output .= '</div>';

    return $output;


new MyPlugin();

And then inside foobar.php simply:

  <?php echo MyPlugin::Foo();?>
  <?php echo MyPlugin::Bar();?>
  • Thank you, this looks like a pretty straightforward combination of template and plugin, at least as straightforward as it can be. I like the simplicity of the calls in the template once the pipeline is set up. Wish I could just call the functions from any arbitrary code without having to do the template check. If I have a post set to use a custom template by default is there anything I can omit or is this just all necessary to get the code in the template to be able to talk to the plugin (which should already be loaded "somewhere")?
    – vaxhax
    Mar 26, 2019 at 17:41
  • @vaxhax – You could call $foo = MyPlugin::Foo(); anywhere you want as soon as "My Plugin" is activated. Any template, any plugin.
    – leymannx
    Mar 26, 2019 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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