【问题标题】:Wordpress rewrite url for my pluginWordpress 为我的插件重写 url
【发布时间】:2015-12-04 21:57:18
【问题描述】:

如何为我的第一个 wordpress 插件创建自定义 url。我不知道如何运行我的代码。当我输入显示 404 页面的“/hom-nay”时出现问题。

add_action('init', function () {
    add_rewrite_rule('hom-nay', "today.php", 'top');
    flush_rewrite_rules();
});

【问题讨论】:

    标签: wordpress plugins routing


    【解决方案1】:

    你可以试试类似的东西

    add_action('init', function () {
        add_rewrite_rule('^hom-nay$', "today.php", 'top');
    });
    

    或者将它设置为某个函数或在 index.php 中的某个页面?我认为它试图在基本 URL 中找到“today.php”,所以它可能是问题所在,而不是在你的插件目录中找到。

    这是一个很好的参考,可能是您需要的解决方案: http://www.ringabell.org/en/appeller-un-fichier-de-template-custom-avec-une-rewrite-rule-dans-wordpress/

    【讨论】:

    • 谢谢你,我现在试试 [code] add_action('init', function () { //add_rewrite_rule('^hom-nay$', "today.php", 'top'); add_rewrite_rule ('hom-nay/([^/]+)/?', 'index.php?plugins/hexagrams=$matches[1]', 'top'); // flush_rewrite_rules(); }); echo'123';exit();[/code] url 工作得很好,但仍然显示 404 页面。
    • 是根目录下的“today.php”吗?还是在插件目录中?
    • 插件目录中的“today.php”
    • 这就是为什么,您会看到大多数示例:重写是针对“index.php?plugins/hexagrams=$matches[1]”或类似的东西。您需要引用 index.php 来加载 today.php。 add_rewrite_rule('^hom-nay$', "index.php?p=1", 'top'); 这将引用 URL 到 post id 1,同样你需要引用 index.php
    • 是的,我明白了。重写不会按照您想要的方式工作,它会查找对根目录的引用,而不是插件目录。您必须为您的插件创建一个 index.php 引用并使用该引用添加重写。
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 2010-12-06
    • 2013-06-25
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多