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

时间:2014-08-14 14:30:39

标签: php wordpress wordpress-plugin

我一直在寻找几个小时试图找到一个直截了当的答案,但我似乎无法找到解决这个问题的任何东西。有许多重写标准URL的例子,但没有任何关于从自定义插件中重写页面的内容。

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

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:7)

基本上,您正在寻找的是让Wordpress在用户访问blah.php时加载特定页面模板(http://example.com/blah/)。此外,您需要在插件中完全生成它而不实际创建任何页面或帖子。你走在正确的轨道上。您要在此处执行的操作是创建一个重写规则,该规则使用特定的查询var来加载页面。这是一步一步:

第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。现在"等等#34;是一个查询var,您将在下面的下一个函数中设置。查询变量就是Wordpress调用$ _GET变量的内容。但是,它们必须在Wordpress中注册才能正确存储该值(在这种情况下为" true")。我们将在下一个函数中执行以下操作。它还值得注意函数"top"中的第三个设置,它使该重写规则优先于其他重写规则。或者,将其设置为" bottom"在使用这个规则之前,会检查没有其他重写规则匹配。

第2步:查询Vars

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实际上很难保存它的值(设置为{{1} }})。这样您就可以在下一个函数中使用它,它会在用户访问您的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

true

在上面的代码中,您只是告诉Wordpress每次查询var" /* 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 } "时都会使用您的blah.php模板。存在并设置为blah。在这种情况下,它存在是因为用户正在访问具有重写规则的URL,该重写规则使用" true"查询变量该函数检查" blah"查询var存在,然后检查" blah"设置为true,如果是,则加载模板。另请注意,模板路径为blah。您正在使用test/blah.php根据插件的位置相对包含模板。这样它作为一个插件应该是相对的。

第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

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

备注

希望这会有所帮助,我前几天正在寻找类似的东西,并且必须以艰难的方式解决这个问题。同样值得注意的是,我已将所有函数作为" yourpluginname _"的前缀。您可以将此更改为您想要的前缀,但请务必更新函数名称以匹配。