如何在自定义插件中为文件创建重写规则


WordPress: How to create a rewrite rule for a file in a custom plugin

我已经找了几个小时试图找到一个直截了当的答案,但我似乎找不到任何解决这个问题的方法。有很多重写标准url的例子,但是没有一个是从自定义插件中重写页面的例子。

我创建了一个自定义插件,其中有一个名为test的特殊目录。测试目录有blah.phpshmeh.php几个页面。是否可以为这些页面创建重写规则,例如http://example.com/blah, http://example.com/shmeh

非常感谢您的帮助

所以基本上你正在寻找的,是让Wordpress加载一个特定的页面模板(blah.php)当用户访问http://example.com/blah/。此外,您需要在插件中完全生成它,而无需实际创建任何页面或帖子。你在正确的轨道上。这里要做的是创建一个重写规则,该规则利用一个特定的查询变量来加载页面。下面是一步一步的:

步骤1:重写规则

http://codex.wordpress.org/Function_Reference/add_action

http://codex.wordpress.org/Plugin_API/Action_Reference/init

http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

/* Rewrite Rules */
add_action('init', 'yourpluginname_rewrite_rules');
function yourpluginname_rewrite_rules() {
    add_rewrite_rule( 'blah/?$', 'index.php?blah=true', 'top' );
}

所以在上面的代码中,你只是告诉Wordpress, example.com/blah/应该被视为如果用户访问example.com/index.php?blah=true。现在,"blah"是一个查询变量,您将在下面的函数中设置它。查询变量就是Wordpress所说的$_GET变量。然而,它们必须在Wordpress上注册,这样它才能正确地存储值(在这种情况下是"true")。我们将在下面的函数中做这个。还值得注意的是函数"top"中的第三个设置,它使此重写规则优先于其他重写规则。或者,将其设置为"bottom"将在使用此规则之前先检查没有其他重写规则匹配。

步骤2:查询变量

http://codex.wordpress.org/Function_Reference/add_filter

http://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars

/* Query Vars */
add_filter( 'query_vars', 'yourpluginname_register_query_var' );
function yourpluginname_register_query_var( $vars ) {
    $vars[] = 'blah';
    return $vars;
}

在上面的代码中,你只是注册查询var " blah "与Wordpress。这样,当有人访问example.com/blah/时,你的重写规则告诉Wordpress像访问example.com/index.php?blah=true一样对待它,Wordpress实际上会保存它的值(设置为true)。这样你就可以在下一个函数中使用它,当用户访问你的url时,它会加载你想要的模板。

步骤3:模板包含

http://codex.wordpress.org/Function_Reference/add_filter

http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include

http://codex.wordpress.org/Class_Reference/WP_Query Interacting_with_WP_Query

http://codex.wordpress.org/Function_Reference/plugin_dir_path

/* Template Include */
add_filter('template_include', 'yourpluginname_blah_template_include', 1, 1); 
function yourpluginname_blah_template_include($template)
{
    global $wp_query; //Load $wp_query object
    $page_value = $wp_query->query_vars['blah']; //Check for query var "blah"
    if ($page_value && $page_value == "true") { //Verify "blah" exists and value is "true".
        return plugin_dir_path(__FILE__).'test/blah.php'; //Load your template or file
    }
    return $template; //Load normal template when $page_value != "true" as a fallback
}

在上面的代码中,你只是告诉Wordpress在查询变量"blah"存在并设置为true时使用blah.php模板。在这种情况下,它是存在的,因为用户正在访问一个url的重写规则,利用"blah"查询变量,该函数检查"blah"查询变量是否存在,然后检查"blah"是否被设置为true,如果是,加载模板。还要注意模板路径是test/blah.php。您正在使用plugin_dir_path(__FILE__)相对包括模板基于您的插件的位置。这样它就像插件一样是相对的。

第4步:实际模板(blah.php)

http://codex.wordpress.org/Function_Reference/get_header

http://codex.wordpress.org/Function_Reference/get_footer

http://codex.wordpress.org/Function_Reference/get_sidebar

模板文件需要加载页眉、页脚和侧边栏本身。当使用template_include时,Wordpress不会像使用页面模板一样自动加载页眉和页脚。确保您的模板文件调用get_header()get_footer()get_sidebar(),以便您获得完整的布局。

指出

希望这有帮助,前几天我在寻找类似的东西,不得不找出它的艰难的方式。同样值得注意的是,我把所有的函数都前缀为"yourpluginname_"。您可以将其更改为您想要的前缀,但一定要更新函数名以匹配。