0

I am creating a plugin that needs to display external data, not wordpress posts.

What I have done: registered rewrites so wp recognizes my archive/single URLs, hooked into template_include to direct it to use my template if the requested URL was one of mine, and then in the template I call a function to load the external data and display it in post/archive format.

This works fine as long as I dont switch templates. Unfotunately, there is a variation in twentyfourteen and twentyfifteen template structure, in which #content and #main are inverted.

  • TwentyFifteen: body > #page > #content > #primary > #main
  • TwentyFourteen: body > #page > #main > #primary > #content

This means that whatever one I code my plugin template for, it is broken in the other.

twentyfifteen/single.php

get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php
    // Start the loop.
    while ( have_posts() ) : the_post();

twentyfourteen/single.php

get_header(); ?>

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
        <?php
            // Start the Loop.
            while ( have_posts() ) : the_post();

So my question(s) are:

  • Am I going about this the right way? Do you know of a better way to inject external data?
  • Why are twentyfifteen tags inverted? Is this not a bug? Because I checked going back to twentlyeleven, and they are all in the order ...#main > #primary > #content.
  • Is there a way to do this, without requiring including a template, so that it works on more sites? The templates are going to be problematic on each site the plugin is used on, for just such reasons. I would prefer to forgo including a template, and somehow hook into archive.php and single.php to display my content, given the URL matches one of my requests.
0

You can hook the content of any post and request data you want through WordPress HTTP API. Like this:

add_filter('the_content', 'my_content_178750', 1, 99);

function my_content_178750($content) {
    return wp_remote_retrieve_body(wp_remote_get('http://example.com'));
}

Also there is possibility to check path you're on and decide what you want to get depending on that.

  • I like this idea, but it doesnt allow me to alter the title. Im also not sure how it would work on an archive page, where the_content I believe is called repeatedly. Any idea how to handle that situation? – Brad Feb 20 '15 at 16:07
  • Marko, thank you very much for this idea! I have done a little more research on it, and found that I can actually add query values to a page name, so if I have a page at /cars/ I can add a rewrite rule that accepts /cars/123/, which I did not know previously was possible. I was able to retrieve the value 123 in the_content. What Im not sure how to do now, is know ahead of time which page 'cars' is, so that I configure the rewrite ahead of time. – Brad Feb 20 '15 at 17:03
  • can you recommend a filter or action earlier in wordpress execution so that I can load the external data and have it ready for consumption by the_title and the_content? – Brad Feb 20 '15 at 18:57
  • Thank you all for your answers. I have it set up now where I intercept a page by slug, and override the_title and the_content which works much nicer, as it doesnt require a custom template. I would like to alter this now so that instead of a using a page, it uses an archive. Is it standard practice here to respond with additional questions inline, or post a new topic? – Brad Feb 20 '15 at 22:15
  • Glad that you're going in right direction now. On archive (or index) pages you probably have excerpt, not contents - I'm only guessing here, since I don't know how your files look like. If that's the case you can also use the_excerpt filter - see here that it's used to retrieve it. – mjakic Feb 22 '15 at 10:08
0

Yes there is a simple way to make it work with most any template.
This is the exact reason so many plugins use shortcodes to inject data or other wise add whatever feature the plugin is designed for. Template structure is far too unpredictable, to do it the way you are currently pursuing.

So right from the API it is simple as naming your shortcode and then adding it to your plugin.

function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
  • Shortcodes wont work because the URLs are rewritten. /cars/ for the archive, /cars/123/ for the detail on car 123. I could have users add a shortcode to a page, but then how would it work for the detail page? – Brad Feb 20 '15 at 16:09
0

As per Skribe wrote before... I will just try to give more details on what he meant if you are not already used to what are shortcodes...

Shortcodes have been created to display different content in different manners, when they are incorporated in posts or pages.

You can see here a current list of existing shortcodes included inside WordPress (as per 2015-02-20):

http://en.support.wordpress.com/shortcodes/

What Skribe is referring about the API can be found here: (as per 2015-02-20) you will find more details about shortcode:

http://codex.wordpress.org/Shortcode_API

You can see that adding the shortcode [gallery id="123" size="medium"] in a post or a page will display pictures. (Read more for exact details).

What you can also do for Plugins is to create you own ShortCode. What this will do, it will control where you want and how you want your Plugin to appear. A ShortCode is included in a page or a post so it will always appear where it should (if that's where you want it). So with the code Skribe was giving you:

function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

You would create a PAGE or A POST with the ShortCode [foobar] inside and the it would display the function called by this shortcode... In this instance it would write on the screen "foo and bar".

If you want your Plugin to appear somewhere else then a posts or a page, you should look into WordPress Hooks. If you need more details about hooks let me know I will add to my answer. But considering your are talking about MAIN and CONTENT having your own ShortCode should be what you need.

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.