• Resolved therks

    (@therks)


    I’m working on a plugin, and I’m having a tough time getting it to do something neatly.
    For the sake of simplicity, let’s say all I want to do is the following: When navigating to https://www.mysite.com/wp/?test=1 it will display (using the “page” template) my own custom content.
    So far, I have something like this:

    class test {
    	function dopage () {
    		include(get_page_template());
    		exit;
    	}
    }
    
    if ($_GET['test']) {
    	$_GET['p'] = -1;
    	add_action('template_redirect', array('test', 'dopage'));
    }

    But I’m having problems with that because 1) I can’t get my own content to appear in the appropriate place. 2) I still get the “Edit this entry” appearing on the page.
    Say all I wanted was for “<h1>Hello World</h1>” to appear where normal page content would appear on that page (where the_content() is in the template), how would I do that?

    Sorry if this kind of thing has been asked before, I looked through the forums a bit but didn’t see anything helpful, and I really didn’t know what to search for. I’m not even particularly happy with my choice of topic title (rather vague) but I didn’t know how to be more descriptive of what I want. If someone could direct me to another forum post that goes over the same thing, or even just a plugin that does something like this already that I could pick apart and study I’d be really greatful.
    Thanks a bunch.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter therks

    (@therks)

    Anyone?

    jbauguss

    (@joshbaugussnet-1)

    One thing to note. Unless you are planning on using filters for the post query, the page template won’t know what to do. It relies on having a post query run.

    I’m doing something similar but my template_redirect loads a template that doesn’t rely on posts/pages. I lookup my own data in a different table not related to wordpress. therefore, using a template like page.php that relies on the loop will result in a whole lot of nothing (since no blog pages/posts were looked up)

    example for your hello world.
    function dopage () {
    include(TEMPLATEPATH . “/dopage.php”);
    exit;
    }

    make dopage.php in the template dir with this content
    <?php get_header();?>
    hello world
    <?php get_footer();?>

    i think that would work. Basically, what would happen is, wp is gonna look at the url, sees wp from /wp/?test=1 will recognize wp as a page or category. this results in a 404. when your template_redirect runs, you intercept by looking at test= and use a different template.

    another way to handle the url can be learned from Custom Query Page

    at the bottom of that page are 2 real important sections. Custom Archives and Permalink to Custom Archives.

    i hope this helps.

    Thread Starter therks

    (@therks)

    Thank you very much for your reply. Using a custom template as you suggested was well within my means, but I was sort of hoping to have this plugin work seamlessly with any theme, without having the end user have to edit or create any files at all, I’m starting to think that may be impossible. I haven’t read through your link yet but I will do so now and see what I can come up with.
    Thanks again.

    jbauguss

    (@joshbaugussnet-1)

    well, there are problems with relying on the default templates for custom content. Biggest problem is the loop. Most themes will have the loop followed by a handler for if no posts/page were found (if(have_posts) fails) so while you could attach something before or after the loop via a plugin hook, you would end up with your content and then a not found message. I think offering a custom template is pretty much a must.

    the only option i can think of other than that is to simulate a post by making use of the query hooks and creating a post object so the loop can find something. Not sure I can really help much there as I’ve never tried that.

    oh, another option….I just did this actually. just use a content filter. Have your plugin rely on the creation of a page or post with a custom tag in it like {replaceme} and you can replace that tag with your custom content. You then let them rely on the page/post templates as well as the freedom to name the page what they want and surround the custom content with whatever they want.

    Thread Starter therks

    (@therks)

    Sorry I meant to reply to this forever ago just to say that I’ve followed your last suggestion. Thanks for the advice.

    Hey – Thanks for this advice, its just what I was looking for.

    Couldn’t you take it a step further and create a post automatically for them using wp_insert_post? I dont have time to try this quite yet, but it seems like it ought to work.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to get a plugin to display original content’ is closed to new replies.