Quantcast
Channel: Topic Tag: plugin | WordPress.org
Viewing all articles
Browse latest Browse all 26892

brianoz on "Plugin generating virtual pages, issue with the_content not running"

$
0
0

I have a need to be able to generate fake/virtual/dynamic pages based on a url like http://www.mycinema.com/wpcinema/movie/MOVIEID to be able to display movies for cinemas with info on the movie and live session feed information.

After spending many hours researching, there doesn't seem to be much stuff written on how to do virtual pages in WordPress, so I will be writing up my experiences after getting this resolved!

So far, the current plan is to use the two filters - template_redirect to set the template to the current plugin's page.php template, and the_content to insert the content. The idea is to use the theme's template so the pages theme in well with the site.

(I got this approach from this excellent 2012 page from Xavi Esteve).

I have two problems:

  1. What is the best, most bullet proof, way to do this? Am I using the wrong approach? My thinking was that using the current theme's template was likely to provide the best current fit for the style of the website.
  2. TwentyTwelve does not appear to be calling the the_content filter in the context I'm using it. I suspect I'm doing something wrong, but cannot find the problem. This is probably closely related to question 1. TwentyTwelve definitely calls the_content for a normal page, and even an early add_filter() doesn't trigger in my code.

I discovered get_template_part() yesterday and wondered if I should be using that instead of manually looking in the child folder then the parent and running an include (not shown in the code below).

I wouldn't be asking, but I'm at my wit's end having googled extensively, possibly for the wrong search terms.

I've considered custom post types, but there are various complexities around this (including content that may change every few minutes) which means a dynamically generated page works much better.

This is an excerpt from the code I've written to explain the problem further:

add_action('parse_request', array(&$this, 'vm_parse_request'));

function vm_parse_request( &$wp )
{
    global $wp;
    if (empty($wp->query_vars['pagename']))
        return; // page isn't permalink

    $p = $wp->query_vars['pagename'];

    if (! preg_match("#wp-cinema/movie/([^/]+)#", $p, $m))
        return;

    // setup hooks and filters to generate virtual movie page
    add_action('template_redirect', array(&$this, 'vm_template_redir'));
    add_filter('the_content', array(&$this, 'vm_the_content'));
}

function vm_template_redir()
{
    // Reset currrently set 404 flag as this is a plugin-generated page
    global $wp_query;
    $wp_query->is_404 = false;

    $template = 'page.php';

    include(STYLESHEETPATH."/page.php"); // child
    // parent case left out for brevity

    exit;
}

function vm_the_content($content)
{
    return "my new content (actually generated dynamically)";
}

This is going to be an increasingly common thing in WordPress - can anyone offer suggestions or help? Anything is much appreciated.


Viewing all articles
Browse latest Browse all 26892

Trending Articles