从我的插件中充分覆盖 $post

发布于 2024-12-27 11:15:32 字数 1586 浏览 3 评论 0原文

我正在为 WordPress 开发我的第一个真正的大型插件,到目前为止几乎一切都进展顺利。我唯一遇到的问题是很难欺骗当前的帖子,以便用户的布局继续正常工作。让我解释一下。

我正在使用《Wrox Professional Wordpress Plugin Development》一书的永久链接结构策略。为此,我执行以下操作:

//Used to detect when the plugin gets actuvated
public function pluginActivatedAction(){

    //Send commands to install the rewrite rules
    add_rewrite_tag('%sgmpage%', '([^/]+)');
    add_permastruct('sgmpage', 'sgm/%sgmpage%');
    flush_rewrite_rules();

}

因此,我的插件响应所有 /sgm/** url。这很好,因为我希望能够进行一些古怪的网址重写,以获得更好的结果。问题是,使用这种技术,wordpress 无法说出它在哪个页面下,这会扰乱集成器完成的布局,例如标题图像、活动菜单项、侧边栏。

我尝试挂钩几个钩子,但没有成功,例如:

  1. wp (全局 $post 并覆盖它)
  2. pre_post_selection (并更改查询)
  3. template_redirect (全局 $post 并覆盖它)

没有任何变化...模板使用 The_Id() 作为一个检索当前帖子 id 的函数,该函数有效地使用 $post->id (我认为,现在关闭了源代码),但要么在我更改它之后有东西覆盖了 $post,要么我的技术无法正常工作。

所以我的问题是,您可以使用“add_rewrite_tag”技术以任何方式挂钩或覆盖当前帖子,网址匹配...我想做这样的事情:

global $post;
$post_id = 440; //Or get_option() later obviously
$post = get_post($post_id);

但它不起作用。

感谢您的帮助...


更新

//Used to detect when the plugin gets actuvated
public function pluginActivatedAction(){

    //Send commands to install the rewrite rules
    add_rewrite_rule('sgm(/(([a-z0-9]+)(/([a-z]+)/?)?)?)', 'index.php?p=440&sgmevent=$matches[3]&sgmpage=$matches[5]');
    flush_rewrite_rules();

}

我删除了其他所有内容,只是添加了一个重写规则以使 p=440 继续运行,除了 P 查询变量似乎不存在之外,一切仍然工作正常,有些东西告诉我重写不起作用。我在重写器和激活功能中粘贴了同样的内容,然后停用/重新激活了我的插件。

I'm developping my first real big pluggin for wordpress and so far almost everything is going smootly. The only thing i'm having, is difficulty spoofing the current post so that the layout of the user continues working fine. Let me explain.

I'm using the Wrox Profesionnal Wordpress Plugin Development book's permalink structure strategy. To this end, i do the following:

//Used to detect when the plugin gets actuvated
public function pluginActivatedAction(){

    //Send commands to install the rewrite rules
    add_rewrite_tag('%sgmpage%', '([^/]+)');
    add_permastruct('sgmpage', 'sgm/%sgmpage%');
    flush_rewrite_rules();

}

And thus, my plugin responds to all /sgm/** urls. Which is fine cause i want to be able to do some whacky url rewritting to get something nice going. Problem is, with that technique, wordpress can't say under which page it is and this messes up the layout done by the integrator such as the header image, the active menu item, the sidebar.

I tried hooking to several hooks without success such as:

  1. wp (global $post and override it)
  2. pre_post_selection (And change the query)
  3. template_redirect (global $post and override it)

And nothing changes... The template is using The_Id() as a function to retrieve the id of the current post which effectively uses $post->id (i think, closed the source now) but either there is something overriding the $post AFTER i change it, or my technique is not working right.

So my question is, can you hook or override in anyway the current post the url matches using the "add_rewrite_tag" technique... I'd want to do something like that:

global $post;
$post_id = 440; //Or get_option() later obviously
$post = get_post($post_id);

But it doesn't work.

Thanks for your help...


UPDATE

//Used to detect when the plugin gets actuvated
public function pluginActivatedAction(){

    //Send commands to install the rewrite rules
    add_rewrite_rule('sgm(/(([a-z0-9]+)(/([a-z]+)/?)?)?)', 'index.php?p=440&sgmevent=$matches[3]&sgmpage=$matches[5]');
    flush_rewrite_rules();

}

I removed everything else and just added a rewrite rule to get that p=440 going, everything is still working fine except the P query var doesn't seem to be there, something tells me the rewrite is not working. I pasted that same thing in both my rewriter and in the activation function and i deactivated/reactivated my pluggin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯叶蝶 2025-01-03 11:15:32

如果该功能仅在激活时运行,那将是一个主要问题。 add_rewrite_tagadd_permastruct 都需要在每次页面加载时运行。 flush_rewrite_rules 应该仅在激活时运行,就像您在此处所看到的那样。

我不确定这是否可以解决 404 问题,因为 WordPress 不知道如何处理 'sgmpage' 查询变量。您可以做的一件事是,不要使用 add_rewrite_tag,而是添加如下内容:

function your_own_add_rewrite_tag_function(){
    global $wp_rewrite, $wp;
    $wp->add_query_var('sgmpage');
    $wp_rewrite->add_rewrite_tag('%sgmpage%', '([^/]+)', 'p=440&sgmpage=');
}

这告诉 WordPress 确切地使用 add_rewrite_tag 做了什么,除了它告诉 WP 也解释所有 sgmpage 请求的帖子 ID 为 440。WordPress 不需要做额外的工作,不需要额外的过滤、操作等,同时仍然保留 sgmpage 查询变量。

这有帮助吗?

编辑

要阻止它重定向到帖子,您需要挂钩'redirect_canonical'。我完全忘记了规范......它总是戴立克......呃......规范重定向!

添加此代码片段应该会有所帮助:

add_filter( 'redirect_canonical', 'redirect_canonical_8886079', 10, 2 );

function redirect_canonical_8886079( $redirect_url, $requested_url ){
    global $wp;
    if(!empty($wp->query_vars['sgmpage']))
        return false;
    return $redirect_url;
}

redirect_canonical 查找请求的对象并确定我们是否位于该对象的规范页面上。如果没有,它会 301 将用户重定向到该数据的规范 URL。搜索引擎喜欢这些东西。

就像蜜獾一样,redirect_canonical 并不关心并做它想做的事。与蜜獾不同,您可以通过过滤重定向值并返回 false$requested_url 来告诉它停止,本质上是:要么规范不适用于此处,要么canonical 是错误的,所请求的 URL 实际上是本例中的规范 url。

If that function is run only on activation, that's going to be one major problem. add_rewrite_tag and add_permastruct both need to be run on every page load. flush_rewrite_rules should only be run on activation, as you have it here.

I'm not sure if that will fix the 404 problem, since WordPress doesn't know what to do with the 'sgmpage' query variable. One thing you might do is, rather than using add_rewrite_tag, add something like this:

function your_own_add_rewrite_tag_function(){
    global $wp_rewrite, $wp;
    $wp->add_query_var('sgmpage');
    $wp_rewrite->add_rewrite_tag('%sgmpage%', '([^/]+)', 'p=440&sgmpage=');
}

That tells WordPress exactly what using add_rewrite_tag does, except it tells WP to also interpret all sgmpage requests as having a post id of 440. No extra work for WordPress to do, no extra filtering, actions, etc., while still preserving the sgmpage query vars.

Does that help?

EDIT

To stop it from redirecting to the post, you will need to hook into 'redirect_canonical'. I totally forgot about canonical... It's always the Daleks ... er ... canonical redirect!

Adding this snippet should help:

add_filter( 'redirect_canonical', 'redirect_canonical_8886079', 10, 2 );

function redirect_canonical_8886079( $redirect_url, $requested_url ){
    global $wp;
    if(!empty($wp->query_vars['sgmpage']))
        return false;
    return $redirect_url;
}

redirect_canonical looks for requested objects and figures out if we're on the canonical page for that object. If not, it 301 redirects the user to the canonical url for that data. The search engines love that stuff.

Like the honey badger, redirect_canonical don't care and does what it wants. Unlike the honey badger, you can tell it to stop by filtering the redirect value and either returning false or $requested_url, saying, essentially: either canonical doesn't apply here or canonical is wrong and the requested URL is, in fact, the canonical url in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文