如何正确使用 function.php 中的 custom_rewrite_rule() 函数?

huangapple go评论65阅读模式
英文:

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(&#39;^profile/([^/]+)/(\d+)/?&#39;, &#39;index.php?page_id=104&amp;name=$matches[1]&amp;age=$matches[2]&#39;, &#39;top&#39;);
    flush_rewrite_rules(); // Flush rewrite rules to test the changes immediately
}
add_action(&#39;init&#39;, &#39;custom_rewrite_rule&#39;);

function display_profile_content() {
    $name = sanitize_text_field(get_query_var(&#39;name&#39;));
    $age = intval(get_query_var(&#39;age&#39;));

    echo &#39;Name: &#39; . $name . &#39;&lt;br&gt;&#39;;
    echo &#39;Age: &#39; . $age . &#39;&lt;br&gt;&#39;;
}
add_shortcode(&#39;profile_content&#39;, &#39;display_profile_content&#39;);

&lt;/code&gt;

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(&#39;^profile\/([^/]+)\/(\d+)\/?&#39;, &#39;index.php?name=$matches[1]&amp;age=$matches[2]&#39;, &#39;top&#39;);
	flush_rewrite_rules(); // Flush rewrite rules to test the changes immediately
}
add_action(&#39;init&#39;, &#39;custom_rewrite_rule&#39;);

add_filter( &#39;query_vars&#39;, function( $query_vars ) {
	$query_vars[] = &#39;age&#39;;
	return $query_vars;
} );
function display_profile_content() {
	global $wp_query;
	$name = sanitize_text_field(get_query_var(&#39;name&#39;));
	$age = intval(get_query_var(&#39;age&#39;));
	echo &#39;&lt;pre&gt;&#39;;
	var_dump( $wp_query );
	echo &#39;&lt;/pre&gt;&#39;;

	echo &#39;Name: &#39; . $name . &#39;&lt;br&gt;&#39;;
	echo &#39;Age: &#39; . $age . &#39;&lt;br&gt;&#39;;
}
add_shortcode(&#39;profile_content&#39;, &#39;display_profile_content&#39;);

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(&#39;[profile_content]&#39;);

huangapple
  • 本文由 发表于 2023年5月30日 00:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定