【问题标题】:Wordpress plugin generating virtual pages and using theme templateWordpress 插件生成虚拟页面并使用主题模板
【发布时间】:2013-07-31 09:41:58
【问题描述】:

我需要能够基于http://www.mycinema.com/wpcinema/movie/MOVIEID 之类的 URL 生成虚假/虚拟/动态页面,以便能够显示带有电影信息和实时会话提要信息的电影院电影。

花了很多时间研究之后,似乎没有太多关于如何在WordPress中做虚拟页面的东西,所以我会在解决这个问题后写下我的经验!

到目前为止,目前的计划是使用两个过滤器——template_redirect 将模板设置为当前插件的page.php 模板,以及the_content 插入内容。这个想法是使用主题的模板,以便页面主题与网站很好地融合在一起。

(我从this excellent 2012 page from Xavi Esteve得到这种方法)。

我有两个问题:

  1. 什么是最好、最防弹的方法?我使用了错误的方法吗?我的想法是,使用当前主题的模板可能会提供最适合当前网站样式的样式。

  2. TwentyTwelve 似乎没有在我使用的上下文中调用 the_content 过滤器。我怀疑我做错了什么,但找不到问题。这可能与问题 1 密切相关。TwentyTwelve 肯定会为普通页面调用 the_content,甚至在我的代码中也不会触发早期的 add_filter()。

我昨天发现了 get_template_part() 并想知道是否应该使用它而不是手动查找子文件夹然后父文件夹并运行包含。

我不会问,但我在谷歌上广泛搜索,可能是错误的搜索词。

我考虑过自定义帖子类型,但是这方面存在各种复杂性(包括可能每隔几分钟更改一次的内容),这意味着动态生成的页面效果更好。

这是我为进一步解释问题而编写的代码的摘录:

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)";
}

这将在 WordPress 中变得越来越普遍 - 任何人都可以提供建议或帮助吗?任何事情都非常感谢。

【问题讨论】:

  • 嗨,Brian,我想知道我是否可以将虚拟页面应用于我的问题 wordpress.stackexchange.com/questions/110118/… 你觉得如何?
  • Radek - 虽然我没有测试过,但我相信你可以,如果你可以将主页设置为现有页面之外的其他内容。或者,您可以使用我的代码来覆盖它。

标签: wordpress templates plugins themes virtual


【解决方案1】:

(在文章底部更新,包括指向改进的工作代码要点的链接)

我想发布一个答案,因为这里关于 WordPress 虚拟页面的查询似乎都没有答案!在获得这个问题的答案、对其进行测试并确保它运行良好的过程中,需要付出很多努力。希望这能拯救其他一些人我所经历的痛苦......

事实证明,在 2013 年使用 WordPress 3.5.2+(一周前现在是 3.6)时,上面提到的 Xavi Esteve 的解决方案不再有效,因为 WordPress 已经发展,dangit。

单独使用上面的 template_redirect 方法,问题是就 WordPress 而言,没有页面/帖子内容,因此许多主题不会调用 the_content(),因此我在 the_content 过滤器上的代码永远不会被调用.

现在最好的解决方案似乎是挂钩到“the_posts”过滤器并返回一个伪页面,但它本身并没有附加主题。

缺少主题的解决方案是将其与 Xavi Esteve 的部分方法混合,以允许我更改用于生成页面的模板。

这种方法应该立即适用于大多数(如果不是全部)WordPress 主题,这是我的目标,并且它与我迄今为止测试过的主题效果非常好。

我使用了 Dave Jesch 在此页面上记录的方法(还有其他版本,但只有 Dave 仔细解释了它,谢谢 Dave!):http://davejesch.com/wordpress/wordpress-tech/creating-virtual-pages-in-wordpress/

在某些主题中,Wordpress cmets 部分出现在页面底部,我也经历了很多痛苦。解决方案将出现在上面链接的文件中,并且可能超出此特定解决方案的范围。

另外,为了防止 WordPress 3.5.2+ 出现警告,我还必须添加一个帖子成员:

 $post->ancestors = array();

这个解决方案用于 wp-cinema WordPress 插件(文件views.php,如果你想获取一些工作代码,应该在接下来的几周内签入)。如果该方法存在问题,我将保持该文件最新,因为它是更大项目的一部分。

完整的工作解决方案如下。这是从一段更长的代码中摘录的,它也阻止了 cmets 的出现等(参见上面提供的链接)。代码:

add_action('parse_request', 'vm_parse_request');


// Check page requests for Virtual movie pages
// If we have one, generate 'movie details' Virtual page.
// ...
//
function vm_parse_request(&$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', 'vm_template_redir');

    $this->vm_body = "page body text";

    add_filter('the_posts', 'vm_createdummypost');

    // now that we know it's my page,
    // prevent shortcode content from having spurious <p> and <br> added
    remove_filter('the_content', 'wpautop');
}


// Setup a dummy post/page 
// From the WP view, a post == a page
//
function vm_createdummypost($posts)
{
    // have to create a dummy post as otherwise many templates
    // don't call the_content filter
    global $wp, $wp_query;

    //create a fake post intance
    $p = new stdClass;
    // fill $p with everything a page in the database would have
    $p->ID = -1;
    $p->post_author = 1;
    $p->post_date = current_time('mysql');
    $p->post_date_gmt =  current_time('mysql', $gmt = 1);
    $p->post_content = $this->vm_body;
    $p->post_title = $this->vm_title;
    $p->post_excerpt = '';
    $p->post_status = 'publish';
    $p->ping_status = 'closed';
    $p->post_password = '';
    $p->post_name = 'movie_details'; // slug
    $p->to_ping = '';
    $p->pinged = '';
    $p->modified = $p->post_date;
    $p->modified_gmt = $p->post_date_gmt;
    $p->post_content_filtered = '';
    $p->post_parent = 0;
    $p->guid = get_home_url('/' . $p->post_name); // use url instead?
    $p->menu_order = 0;
    $p->post_type = 'page';
    $p->post_mime_type = '';
    $p->comment_status = 'closed';
    $p->comment_count = 0;
    $p->filter = 'raw';
    $p->ancestors = array(); // 3.6

    // reset wp_query properties to simulate a found page
    $wp_query->is_page = TRUE;
    $wp_query->is_singular = TRUE;
    $wp_query->is_home = FALSE;
    $wp_query->is_archive = FALSE;
    $wp_query->is_category = FALSE;
    unset($wp_query->query['error']);
    $wp->query = array();
    $wp_query->query_vars['error'] = '';
    $wp_query->is_404 = FALSE;

    $wp_query->current_post = $p->ID;
    $wp_query->found_posts = 1;
    $wp_query->post_count = 1;
    $wp_query->comment_count = 0;
    // -1 for current_comment displays comment if not logged in!
    $wp_query->current_comment = null;
    $wp_query->is_singular = 1;

    $wp_query->post = $p;
    $wp_query->posts = array($p);
    $wp_query->queried_object = $p;
    $wp_query->queried_object_id = $p->ID;
    $wp_query->current_post = $p->ID;
    $wp_query->post_count = 1;

    return array($p);
}


// Virtual Movie page - tell wordpress we are using the page.php
// template if it exists (it normally will).
//
// We use the theme page.php if we possibly can; if not, we do our best.
// The get_template_part() call will use child theme template if it exists.
// This gets called before any output to browser
//
function vm_template_redir()
{
    // Display movie template using WordPress' internal precedence
    //  ie: child > parent; page-movie.php > page.php
    //  this call includes the template which outputs the content
    get_template_part('page', 'movie');

    exit;
}

顺便说一句,重要的是要说我觉得这几乎是一种 hack,并且很想知道如何才能做得更好。此外,我希望看到 WordPress 能够达到标准并提供用于生成虚假页面的 API。 (我怀疑他们有意识形态上的原因为什么他们不会这样做,但很高兴看到他们对此的解决方案,即使是替代方案,也能得到深入解释);我个人觉得有些情况下我不想仅仅为了生成页面而干预用户的网站。

2014 年 2 月更新:我已将其抽象为一个类,该类应该为大多数应用程序提供足够的灵活性:https://gist.github.com/brianoz/9105004

【讨论】:

  • 我已经把它变成了一个类来管理多个虚拟页面。当匹配正则表达式时,该类调用提供的内容函数。当我对此感到满意时,我会将上面的链接替换为指向类文件的链接(其中包括一个示例)。如果您需要它,请戳我,我已经忘记了! :)
  • 这是一个有趣的方法。一段时间以来,我一直在尝试做类似的事情来适度成功。你有指向你提供的类文件的链接吗?
  • 对不起,是的;我忘了我已经做过了。要点在这里(也更新了文章):gist.github.com/brianoz/9105004 这应该足够灵活,可以与大多数应用程序一起使用,感谢反馈。
  • Gist 上链接的类非常适合我的用例(根据 url 路径的第一部分创建“虚拟”页面)。值得阅读 Gist 页面上的 cmets - 您需要修改三行代码才能使其正常运行。
猜你喜欢
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多