Change WordPress fixed link to date author form

Source: Internet
Author: User
Tags php file

For example,/post/author/frustigor,/post/date/2014/04. This is very strange. It is not easy to read or necessary. The expected result 3 is in the format of/author/name and/date/year/month. How can we fix it? This article tells you the answer.

1. What you need to know & uarr;

We need to first understand these facts: 1. you must write wordpress-related php programs to solve this problem. 2. you must understand that this problem is related to two things. One is a link. For example, the link in the sidebar "March 2014" must be changed to a URL that does not contain post, and the other is a rewrite, you need to let wordpress know that this URL is used to call the archived topic of the month.

These two problems are difficult to understand. If you have no idea about wordpress development, you can drag them down directly. If you are interested in the implementation principle, you can take a look.

2. How to modify the link so that click the link to enter the URL we want & uarr;

This is actually not that difficult, very simple. Wordpress has some hooks to achieve this effect directly, mainly year_link, month_link, day_link, and author_link. How to use it?

The code is as follows: Copy code
Add_filter ('author _ link', 'archive _ rewrite_link ', 99, 2 );
Add_filter ('Day _ link', 'archive _ rewrite_link ', 99, 2 );
Add_filter ('month _ link', 'archive _ rewrite_link ', 99, 2 );
Add_filter ('Year _ link', 'archive _ rewrite_link ', 99, 2 );
Function archive_rewrite_link ($ link ){
Global $ wp_rewrite; www.111cn.net
$ Front = ltrim ($ wp_rewrite-> front ,'/');
$ Link = str_replace_first ($ front, '', $ link );
Return $ link;
}

The str_replace_first above is a function written by myself. Drag it down and you can see it in all the code. A very concise piece of code. Refresh it and see if these links have been modified. But don't be happy too early. The link is modified, but don't be happy too early. Do you see 404? Yes, wordpress doesn't even know you have modified the link.

3. Add rewrite rules for wordpress to make the modified link accessible & uarr;

There is a global class named rewrtie in wordpress. This class is so powerful that it determines what content the current URL will display. If you look at the articles written by others on this aspect, it will scare you, and the logic in it is very complicated. However, don't worry. Here we can implement it with an extremely simple code.

The code is as follows: Copy code
Add_filter ('author _ rewrite_rules ', 'Archives _ rewrite_rules', 99 );
Add_filter ('date _ rewrite_rules ', 'Archives _ rewrite_rules', 99 );
Function archive_rewrite_rules ($ rules ){
Global $ wp_rewrite;
$ Front = ltrim ($ wp_rewrite-> front ,'/');
Foreach ($ rules as $ key => $ value ){
$ Newrules [str_replace_first ($ front, '', $ key)] = $ value;
    }
Return $ newrules;
}

Was it found that it was shorter than the previous code. In fact, the reason is also very simple. Before the URL is parsed, we can achieve our goal by modifying the global class $ wp_rewrite variable. (This method is very opportunistic, because for wordpress itself, the rewrite rules in the database have not changed. But what does it do? We can achieve our own results .)

4. Complete code

If you are a newbie, copy the following code to the functions. Php file in your topic directory. Note that the code must be placed in <? Php?> Inside.

Old rules, reply to read the complete code.

The code is as follows: Copy code

// Replace the substring that appears for the first time in the string
If (! Function_exists ('Str _ replace_first ')):
Function str_replace_first ($ find, $ replace, $ string ){
$ Position = strpos ($ string, $ find );
If ($ position! = False ){
$ Length = strlen ($ find );
$ String = substr_replace ($ string, $ replace, $ position, $ length );
Return $ string;
} Else {
Return false;
 }
} Www.111cn.net
Endif;

// First, modify the link in the webpage
Add_filter ('author _ link', 'archive _ rewrite_link ', 99, 2 );
Add_filter ('Day _ link', 'archive _ rewrite_link ', 99, 2 );
Add_filter ('month _ link', 'archive _ rewrite_link ', 99, 2 );
Add_filter ('Year _ link', 'archive _ rewrite_link ', 99, 2 );
Function archive_rewrite_link ($ link ){
Global $ wp_rewrite;
$ Front = ltrim ($ wp_rewrite-> front ,'/');
$ Link = str_replace_first ($ front, '', $ link );
Return $ link;
}

// Modify the rewrite rule
Add_filter ('author _ rewrite_rules ', 'Archives _ rewrite_rules', 99 );
Add_filter ('date _ rewrite_rules ', 'Archives _ rewrite_rules', 99 );
Function archive_rewrite_rules ($ rules ){
Global $ wp_rewrite;
$ Front = ltrim ($ wp_rewrite-> front ,'/');
Foreach ($ rules as $ key => $ value ){
$ Newrules [str_replace_first ($ front, '', $ key)] = $ value;
    }
Return $ newrules;
}

There are also a lot of URL-related ideas here. For example, you can quickly modify the prefix of the author page by modifying $ wp_rewrite in the future, for example, it is cool to change your author page to the/user/user_id format.

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.