How to display (plugin?) output on a single page (not a widget, probably not a short code) - template, plugin, combination?

I've seen many variants of this question but none of the answers I've found yet have answered my question. Not

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!

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!

Share Improve this question edited Mar 26, 2019 at 18:13 vaxhax asked Mar 26, 2019 at 16:36 vaxhaxvaxhax 135 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

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.

<?php
/*
Plugin Name: My Plugin
Description: Lorem ipsum dolor sit amet.
Version: 1.0
Author: You
Author URI: https://example
*/

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 != '') {

      status_header(200);
      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:

<div>
  <?php echo MyPlugin::Foo();?>
  <?php echo MyPlugin::Bar();?>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745656206a4638557.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信