本文目錄1什麼是作者存檔頁鏈接2修改作者存檔頁鏈接3修改作者存檔頁查詢變量4參考資料
之前分享過 將WordPress作者存檔鏈接中的用戶名改為昵稱,不少朋友都在詢問,如果將WordPress作者存檔鏈接中的用戶名改為用戶ID,好吧,一起來看看 前端部落格 的方法吧。
什麼是作者存檔頁鏈接
wordpress的裡的所有註冊用戶都有一個專屬的鏈接,稱之為作者存檔頁鏈接,通常是這樣的:
1 2 3 4 5 6 7 |
// 未url重寫 http://qianduanblog.com/?author=1 // 已url重寫 http://qianduanblog.com/author/admin |
// 未url重寫 http://qianduanblog.com/?author=1 // 已url重寫 http://qianduanblog.com/author/admin
其中未url重寫的參數值是用戶id,而url重寫後的參數值是用戶名。通常,我們都使用瞭url重寫,而作者存檔頁鏈接暴露瞭用戶名,可能對wordpress的安全性有點點傾斜(而作者的想法是作者的的參數值統一為用戶id,如:author/123,用戶頁面鏈接的參數值統一為用戶id,如:user/123,關於用戶頁面鏈接比作者存檔頁鏈接要復雜,後續文章再說),所以我們需要修改參數值為用戶id。
修改作者存檔頁鏈接
首先要做的是,修改存檔頁鏈接,如下示例:
1 2 3 4 5 6 7 |
// 修改之前 http://qianduanblog.com/author/admin // 修改之後 http://qianduanblog.com/author/123 |
// 修改之前 http://qianduanblog.com/author/admin // 修改之後 http://qianduanblog.com/author/123
在wordpress裡內置瞭關於作者存檔頁鏈接的鉤子,原始的作者存檔頁鏈接是這樣獲取的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * Retrieve the URL to the author page for the user with the ID provided. * * @since 2.1.0 * @uses $wp_rewrite WP_Rewrite * @return string The URL to the author's page. */ function get_author_posts_url($author_id, $author_nicename = '') { global $wp_rewrite; $auth_ID = (int) $author_id; $link = $wp_rewrite->get_author_permastruct(); if ( empty($link) ) { $file = home_url( '/' ); $link = $file . '?author=' . $auth_ID; } else { if ( '' == $author_nicename ) { $user = get_userdata($author_id); if ( !empty($user->user_nicename) ) $author_nicename = $user->user_nicename; } $link = str_replace('%author%', $author_nicename, $link); $link = home_url( user_trailingslashit( $link ) ); } $link = apply_filters('author_link', $link, $author_id, $author_nicename); return $link; } |
/**
* Retrieve the URL to the author page for the user with the ID provided.
*
* @since 2.1.0
* @uses $wp_rewrite WP_Rewrite
* @return string The URL to the author’s page.
*/
function get_author_posts_url($author_id, $author_nicename = ”) {
global $wp_rewrite;
$auth_ID = (int) $author_id;
$link = $wp_rewrite->get_author_permastruct();
if ( empty($link) ) {
$file = home_url( ‘/’ );
$link = $file . ‘?author=’ . $auth_ID;
} else {
if ( ” == $author_nicename ) {
$user = get_userdata($author_id);
if ( !empty($user->user_nicename) )
$author_nicename = $user->user_nicename;
}
$link = str_replace(‘%author%’, $author_nicename, $link);
$link = home_url( user_trailingslashit( $link ) );
}
$link = apply_filters(‘author_link’, $link, $author_id, $author_nicename);
return $link;
}
參考源文檔,生成瞭這樣的用戶id的作者存檔頁鏈接:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * 修改url重寫後的作者存檔頁的鏈接變量 * @since yundanran-3 beta 2 * 2013年10月8日23:23:49 */ add_filter( 'author_link', 'yundanran_author_link', 10, 2 ); function yundanran_author_link( $link, $author_id) { global $wp_rewrite; $author_id = (int) $author_id; $link = $wp_rewrite->get_author_permastruct(); if ( empty($link) ) { $file = home_url( '/' ); $link = $file . '?author=' . $author_id; } else { $link = str_replace('%author%', $author_id, $link); $link = home_url( user_trailingslashit( $link ) ); } return $link; } |
/**
* 修改url重寫後的作者存檔頁的鏈接變量
* @since yundanran-3 beta 2
* 2013年10月8日23:23:49
*/
add_filter( ‘author_link’, ‘yundanran_author_link’, 10, 2 );
function yundanran_author_link( $link, $author_id) {
global $wp_rewrite;
$author_id = (int) $author_id;
$link = $wp_rewrite->get_author_permastruct();
if ( empty($link) ) {
$file = home_url( ‘/’ );
$link = $file . ‘?author=’ . $author_id;
} else {
$link = str_replace(‘%author%’, $author_id, $link);
$link = home_url( user_trailingslashit( $link ) );
}
return $link;
}
修改之後,在前臺輸出作者存檔頁的鏈接:
1 2 |
get_author_posts_url(1); // =>http://qianduanblog.com/author/1 |
get_author_posts_url(1);
// =>http://qianduanblog.com/author/1
說明鉤子使用的是正確的,但當我們打開這個鏈接的時候,出現瞭404。這是因為,原始的作者存檔頁鏈接的url重寫規則是根據用戶名來的,而現在修改為瞭用戶id,重寫規則不匹配,出現瞭404。
修改作者存檔頁查詢變量
為瞭避免出現404,我們需要修改作者存檔頁的url重寫規則。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/** * 替換作者的存檔頁的用戶名,防止被其他用途 * 作者存檔頁鏈接有2個查詢變量, * 一個是author(作者用戶id),用於未url重寫 * 另一個是author_name(作者用戶名),用於url重寫 * 此處做的是,在url重寫之後,把author_name替換為author * @version 1.0 * @since yundanran-3 beta 2 * 2013年10月8日23:19:13 * @link https://www.wpdaxue.com/use-nickname-for-author-slug.html */ add_filter( 'request', 'yundanran_author_link_request' ); function yundanran_author_link_request( $query_vars ) { if ( array_key_exists( 'author_name', $query_vars ) ) { global $wpdb; $author_id=$query_vars['author_name']; if ( $author_id ) { $query_vars['author'] = $author_id; unset( $query_vars['author_name'] ); } } return $query_vars; } |
/**
* 替換作者的存檔頁的用戶名,防止被其他用途
* 作者存檔頁鏈接有2個查詢變量,
* 一個是author(作者用戶id),用於未url重寫
* 另一個是author_name(作者用戶名),用於url重寫
* 此處做的是,在url重寫之後,把author_name替換為author
* @version 1.0
* @since yundanran-3 beta 2
* 2013年10月8日23:19:13
* @link https://www.wpdaxue.com/use-nickname-for-author-slug.html
*/
add_filter( ‘request’, ‘yundanran_author_link_request’ );
function yundanran_author_link_request( $query_vars ) {
if ( array_key_exists( ‘author_name’, $query_vars ) ) {
global $wpdb;
$author_id=$query_vars[‘author_name’];
if ( $author_id ) {
$query_vars[‘author’] = $author_id;
unset( $query_vars[‘author_name’] );
}
}
return $query_vars;
}
很巧合的是,作者存檔頁的查詢變量有2個,之前說過,1個是url未重寫的用戶id(author
),另一個是url已重寫的用戶名(author_name
),而現在直接傳遞過來瞭用戶id,隻需要把參數名稱author_name
修改為author
即可。
再次打開剛才的鏈接(http://qianduanblog.com/author/1),出現瞭正確頁面。而如果打開原始的鏈接(http://qianduanblog.com/author/admin)就會出現404頁面瞭,說明我們的修改正確瞭。
參考資料
https://www.wpdaxue.com/use-nickname-for-author-slug.htmlhttp://codex.wordpress.org/Function_Reference/the_author_posts_link