Mic*_*ael 4 wordpress custom-wordpress-pages
我想根据 URL 提供不同的内容。我从通过自定义模板设置自定义页面开始,但我对其他东西持开放态度。
主要目标是拥有一个 PHP 页面,该页面可以基于 URL 以编程方式提供不同的内容。
例如:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc...
Run Code Online (Sandbox Code Playgroud)
我一直在看add_rewrite_rule
,add_rewrite_tag
,flush_rewrite_rules
和WP_Rewrite API
,但我弄不清哪一个我应该使用?
我在这里找到了一个例子,但我无法让它工作,我得到 404,知道为什么吗?:
/*
Plugin Name: Products Plugin
Plugin URI: http://clivern.com/
Description: Register URL rules for our products
Version: 1.0
Author: Clivern
Author URI: http://clivern.com
License: MIT
*/
function products_plugin_activate() {
products_plugin_rules();
flush_rewrite_rules();
}
function products_plugin_deactivate() {
flush_rewrite_rules();
}
function products_plugin_rules() {
add_rewrite_rule('products/?([^/]*)', 'index.php?pagename=products&product_id=$matches[1]', 'top');
}
function products_plugin_query_vars($vars) {
$vars[] = 'product_id';
return $vars;
}
function products_plugin_display() {
$products_page = get_query_var('pagename');
$product_id = get_query_var('product_id');
if ('products' == $products_page && '' == $product_id):
//show all products
exit;
elseif ('products' == $products_page && '' != $product_id):
//show product page
exit;
endif;
}
//register activation function
register_activation_hook(__FILE__, 'products_plugin_activate');
//register deactivation function
register_deactivation_hook(__FILE__, 'products_plugin_deactivate');
//add rewrite rules in case another plugin flushes rules
add_action('init', 'products_plugin_rules');
//add plugin query vars (product_id) to wordpress
add_filter('query_vars', 'products_plugin_query_vars');
//register plugin custom pages display
add_filter('template_redirect', 'products_plugin_display');
Run Code Online (Sandbox Code Playgroud)
首先,确保启用了漂亮的永久链接,在这种情况下,应取消选择“设置”中的“普通”选项 - 永久链接: 选择以下选项之一以启用漂亮的永久链接
您可以检查代码中是否启用了漂亮的永久链接,如下所示:
function is_enabled_pretty_permalinks() {
return !empty( get_option( 'permalink_structure' ) );
}
if ( is_enabled_pretty_permalinks() ) {
echo 'Pretty permalinks enabled';
}
Run Code Online (Sandbox Code Playgroud)
接下来添加一个新的重写规则:
function add_my_rewrite_rule() {
$page_slug = 'products'; // slug of the page you want to be shown to
$param = 'do'; // param name you want to handle on the page
add_rewrite_rule('my-plugin/?([^/]*)', 'index.php?pagename=' . $page_slug . '&' . $param . '=$matches[1]', 'top');
}
add_action('init', 'add_my_rewrite_rule');
Run Code Online (Sandbox Code Playgroud)
将您的参数添加到查询变量中,以便您能够在页面上处理它:
function add_my_query_vars($vars) {
$vars[] = 'do'; // param name you want to handle on the page
return $vars;
}
add_filter('query_vars', 'add_my_query_vars');
Run Code Online (Sandbox Code Playgroud)
然后您可以do
在页面模板或短代码中访问您的查询变量,例如:
function my_plugin_shortcode_handler( $atts ){
$do = get_query_var( 'do' );
if ( $do === 'this' ) {
return 'do this';
} else {
return 'do that';
}
}
add_shortcode( 'myshortcode', 'my_plugin_shortcode_handler' );
Run Code Online (Sandbox Code Playgroud)
查看链接:
https://some-url.com/my-plugin/this/ - outputs "do this"
https://some-url.com/my-plugin/that/ - outputs "do that".
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
363 次 |
最近记录: |