简体   繁体   English

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

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

I've been looking for hours trying to find a straightforward answer to this but I just can't seem to find anything out there which tackles this issue. 我一直在寻找解决这个问题的简单方法的几个小时,但似乎找不到解决该问题的任何方法。 There are numerous examples for rewriting standard URLs, but nothing in-terms of rewriting pages from within a custom plugin. 有许多用于重写标准URL的示例,但是从定制插件中重写页面的时间不长。

I've created a custom plugin with a special directory in it called test . 我创建了一个自定义插件,其中有一个名为test的特殊目录。 The test directory has several pages which are blah.php and shmeh.php . 测试目录有几个页面,分别是blah.phpshmeh.php Is it possible to create rewrite rules to these pages ie http://example.com/blah , http://example.com/shmeh 是否有可能创建重写规则,这些页面即http://example.com/blahhttp://example.com/shmeh

Thank you very much for all the help. 非常感谢您提供的所有帮助。

So essentially what you are looking for, is to have Wordpress load a specific page template ( blah.php ) when the user visits http://example.com/blah/ . 因此,基本上,您要寻找的是让Wordpress在用户访问http://example.com/blah/时加载特定的页面模板( blah.php )。 Also, you need it to to be completely generated in your plug-in without actually creating any pages or posts. 另外,您需要在插件中完全生成它,而无需实际创建任何页面或帖子。 You are on the right track. 您走在正确的轨道上。 What you want to do here is create a rewrite rule that utilizes a specific query var to load the page. 您要在此处创建一个重写规则,该规则利用特定的查询变量来加载页面。 Here it is step by step: 这是逐步的:

Step 1: Rewrite rules 步骤1:重写规则

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

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

http://codex.wordpress.org/Rewrite_API/add_rewrite_rule 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' );
}

So in the above code, your just telling Wordpress that example.com/blah/ should be treated as if the user were visiting example.com/index.php?blah=true . 因此,在上面的代码中,您刚刚告诉Wordpress的example.com/blah/应该被视为用户正在访问example.com/index.php?blah=true Now "blah" is a query var that you will set up in the next function below. 现在,“ blah”是一个查询变量,您将在下面的下一个函数中对其进行设置。 Query vars are just what Wordpress calls $_GET variables. 查询变量就是Wordpress所谓的$ _GET变量。 However they must be registered with Wordpress so it can properly store the value (which is "true" in this case). 但是,它们必须在Wordpress中注册,以便它可以正确存储值(在这种情况下为“ true”)。 We will do that below in the next function. 我们将在下面的下一个功能中执行此操作。 It's also worth noting the third setting in the function "top" , which gives this rewrite rule priority over other rewrite rules. 还应注意函数"top"的第三个设置,该设置使此重写规则的优先级高于其他重写规则。 Alternatively, setting it to "bottom" would check that no other rewrite rules match first before using this one. 或者,将其设置为“底部”将在使用该规则之前先检查是否没有其他重写规则匹配。

Step 2: Query Vars 步骤2:查询变数

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

http://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars 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;
}

In the code above, your just registering the query var " blah " with Wordpress. 在上面的代码中,您只需在Wordpress中注册查询变量“ blah ”。 That way, when someone visits example.com/blah/ , which your rewrite rule tells Wordpress to treat it like they are visiting example.com/index.php?blah=true , Wordpress actually bothers to save it's value (which is set to true ). 这样,当有人访问example.com/blah/ (您的重写规则告诉Wordpress将其视为访问example.com/index.php?blah=true ,Wordpress实际上会麻烦保存它的值(设置为true )。 This is so you can use it in the next function, which loads the template you want when users visit your url. 这样,您就可以在下一个函数中使用它,该函数将在用户访问您的URL时加载所需的模板。

Step 3: Template Include 步骤3:包含模板

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

http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include 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/Class_Reference/WP_Query#Interacting_with_WP_Query

http://codex.wordpress.org/Function_Reference/plugin_dir_path 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
}

In the code above, you are simply telling Wordpress to use your blah.php template any time the query var " blah " is present and set to true . 在上面的代码中,您只是在告诉WordPress只要存在查询var“ blah ”并将其设置为true即可使用blah.php模板。 In this case it is present because a user is visiting a url with a rewrite rule that utilizes the " blah " query var. 在这种情况下,它的存在是因为用户正在访问带有重写规则的URL,该重写规则利用了“ blah ”查询变量。 The function checks if the " blah " query var is present, then checks if " blah " is set to true, and if so loads the template. 该函数检查是否存在“ blah ”查询变量,然后检查“ blah ”是否设置为true,如果是,则加载模板。 Also note that the template path is test/blah.php . 另请注意,模板路径为test/blah.php You are using plugin_dir_path(__FILE__) to relatively include the template based on the location of your plugin. 您正在使用plugin_dir_path(__FILE__)根据插件位置相对地包含模板。 That way it's relative as a plug-in should be. 这样,它作为插件应该是相对的。

Step 4: The Actual Template (blah.php) 步骤4:实际模板(blah.php)

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

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

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

Your template file will need to load the header, footer, and sidebar itself. 您的模板文件将需要加载页眉,页脚和侧栏本身。 When using template_include , Wordpress will not automatically load the header and footer as if you were using a page template. 使用template_include ,Wordpress不会像使用页面模板那样自动加载页眉和页脚。 Be sure your template file calls get_header() , get_footer() , and get_sidebar() so that you get your complete layout. 确保模板文件调用get_header()get_footer()get_sidebar()以便获得完整的布局。

Notes 笔记

Hope this helps, I was searching for something similar the other day and had to figure it out the hard way. 希望这会有所帮助,前几天我在寻找类似的东西,不得不设法解决。 It's also worth noting that I have prefixed all of the functions as "yourpluginname_". 还值得注意的是,我已将所有功能添加为“ yourpluginname_”。 You can change this to whatever you want your prefix to be, but be sure to update the function names to match as well. 您可以将其更改为您希望前缀使用的任何名称,但请确保也更新函数名称以使其匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM