• I am creating a plugin that needs to display eternal 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.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    How you inject data depends on the nature of the data and where and how it should appear. There’s a number of techniques, none are any more right or wrong than another, it just depends.

    What you should not do is make any assumptions about how themes contain or style their pages. There’s over 3K themes in the WP repository, many times that out in “the wild”. Only a relative few have the exact same container structure. There’s a reasonable chance there’s a header and footer that will be loaded by respective function calls, but it’s not an absolute given. The chances of there being a sidebar is even smaller. AFAIK calling get_sidebar(), get_footer(), and get_header() will do no harm even though it may not result in anything being displayed.

    Assuming there will be #content, #main, etc. classes or ID at all is a stretch. Assuming they occur in a particular order is foolish IMO. Templates were not really intended to be supplied by plugins at all, at least not the kind you select when making a new page. Of course there are all manner of plugins that use templates. They have their place, but you have to let the theme do what it does with no assumptions on your part.

    Another way to inject content is with shortcodes. A page could be automatically created with the shortcode as content when your plugin is activated. The main advantage here is your content will be fully wrapped in containers intended by the theme to be applied to normal page content. When you use your own template, you essentially discard such containers and create your own, with unpredictable results.

    You could also inject content by using some hook. This is better for supplementary content that needs to appear at a particular spot amongst normal site content. You could also use javascript/jQuery to inject content. The problem here is you cannot know the container structure used by the theme, so about the only reliable option is to work with the inner HTML of the <body> element, the only container you can be sure will exist.

Viewing 1 replies (of 1 total)

The topic ‘Displaying Eternal Data – Not Posts’ is closed to new replies.