Custom routing and URL parameters in WORDPRESS

Source: Internet
Author: User

Wordpress can use the rewrite API to make the url more beautiful. Two related rewite functions are required: add_rewrite_tag and add_rewrite_rule. The following describes how to customize wordpress routing rules and url parameters.

Add_action ('init ', 'My _ rr_url ');
Function my_rr_url ()
{     
// Ensure the $ wp_rewrite global is loaded
Global $ wp_rewrite;
Add_rewrite_tag ('% myname %', '([^ &] + )');
Add_rewrite_rule ('Haha/([A-Za-z0-9] {1 ,})/? $ ', 'Index. php? Page_id = 8 & myname = $ matches [1] ', 'top ');
 
// Call flush_rules () as a method of the $ wp_rewrite object
$ Wp_rewrite-> flush_rules ();
}
Add_rewrite_tag custom url parameters

You can use the add_rewrite_tag function to add custom url parameters. For example, & myname = in the preceding example tells wordpress that the myname parameter is valid. Therefore, use the add_rewrite_tag function to register this url parameter.

* The add_rewrite_tag function is not required to beautify wordpress URL routing rules. It is used only when you add custom parameters. For example, a parameter myname is added in this example.

Add_rewrite_rule custom route

To beautify the wordpress url, you must use the add_rewrite_rule function to customize url access rules and tell wordpress how to correctly parse the url. In the example above:

 

Add_rewrite_rule ('Haha/([A-Za-z0-9] {1 ,})/? $ ', 'Index. php? Page_id = 8 & myname = $ matches [1] ', 'top ');

The first matched parameter is $ matches [1], the second parameter is $ matches [2], and so on.

Familiar with the two functions: add_rewrite_tag and add_rewrite_rule, you can customize the routing rules of the wordpress website as you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.