英文:
How to properly use the function.php custom_rewrite_rule()
问题
I'm using WordPress and I have added this function to the functions.php in my theme (the basic twentyone):
function custom_rewrite_rule() {
add_rewrite_rule('^profile/([^/]+)/(\d+)/?', 'index.php?page_id=104&name=$matches[1]&age=$matches[2]', 'top');
flush_rewrite_rules(); // Flush rewrite rules to test the changes immediately
}
add_action('init', 'custom_rewrite_rule');
function display_profile_content() {
$name = sanitize_text_field(get_query_var('name'));
$age = intval(get_query_var('age'));
echo 'Name: ' . $name . '<br>';
echo 'Age: ' . $age . '<br>';
}
add_shortcode('profile_content', 'display_profile_content');
In the specific page I added correctly the shortcode and my intent would be that for example if go to
Website.com/page/Poul/21
It should show:
Name: Poul
Age: 21
but whatever I put in the URL it always display:
Name: profile
Age: 0
Troubleshooting so far:
I have saved the permalink page to flush the cache.
Disabled all plugins
changed theme to this basic one,
but nothing seems to work.
How can I do?
英文:
I'm using WordPress and I have added this function to the functions.php in my theme (the basic twentyone):
function custom_rewrite_rule() {
add_rewrite_rule('^profile/([^/]+)/(\d+)/?', 'index.php?page_id=104&name=$matches[1]&age=$matches[2]', 'top');
flush_rewrite_rules(); // Flush rewrite rules to test the changes immediately
}
add_action('init', 'custom_rewrite_rule');
function display_profile_content() {
$name = sanitize_text_field(get_query_var('name'));
$age = intval(get_query_var('age'));
echo 'Name: ' . $name . '<br>';
echo 'Age: ' . $age . '<br>';
}
add_shortcode('profile_content', 'display_profile_content');
</code>
In the specific page I added correctly the shortcode and my intent would be that for example if go to
Website.com/page/Poul/21
It should show:
> Name: Poul
Age: 21
but whatever I put in the URL it always display:
> Name: profile
Age:0
Troubleshooting so far:
I have saved the permalink page to flush the cache.
Disabled all plugins
changed theme to this basic one,
but nothing seems to work.
How can I do?
答案1
得分: 2
The WP_Query 不允许任何 URL 参数,它有一系列经过白名单验证的参数,您可以使用钩子 query_vars
来更改这些参数。
链接到 query_vars
现在,为了使它工作如下:
function custom_rewrite_rule() {
add_rewrite_rule('^profile/([^/]+)/(\d+)/?', 'index.php?name=$matches[1]&age=$matches[2]', 'top');
flush_rewrite_rules(); // 立即刷新重写规则以测试更改
}
add_action('init', 'custom_rewrite_rule');
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'age';
return $query_vars;
} );
function display_profile_content() {
global $wp_query;
$name = sanitize_text_field(get_query_var('name'));
$age = intval(get_query_var('age'));
echo '<pre>';
var_dump( $wp_query );
echo '</pre>';
echo 'Name: ' . $name . '<br>';
echo 'Age: ' . $age . '<br>';
}
add_shortcode('profile_content', 'display_profile_content');
此外,您可能误解了您的正则表达式,所以 rewrite_rules
接受 profiles/{name}/{age}
,因此有效的 URL 是 Website.com/profile/Poul/21
。您可以在您主题的 index.php 中通过添加以下代码来测试这个:
do_shortcode('[profile_content]');
英文:
The WP_Query does not allow any url parameters, it has list of whitelisted parameters, you can change those parameters using the hook query_vars
.
Link to query_vars
Now in order to make it Work like this.
function custom_rewrite_rule() {
add_rewrite_rule('^profile\/([^/]+)\/(\d+)\/?', 'index.php?name=$matches[1]&age=$matches[2]', 'top');
flush_rewrite_rules(); // Flush rewrite rules to test the changes immediately
}
add_action('init', 'custom_rewrite_rule');
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'age';
return $query_vars;
} );
function display_profile_content() {
global $wp_query;
$name = sanitize_text_field(get_query_var('name'));
$age = intval(get_query_var('age'));
echo '<pre>';
var_dump( $wp_query );
echo '</pre>';
echo 'Name: ' . $name . '<br>';
echo 'Age: ' . $age . '<br>';
}
add_shortcode('profile_content', 'display_profile_content');
Also you might have misunderstood your regex, so rewrite_rules takes the profiles/{name}/{age}
so your URL, that works would be Website.com/profile/Poul/21
You can test this out in the index.php of your theme by adding this code.
do_shortcode('[profile_content]');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论