2017-02-03 38 views
2

默認情況下,wordpress會將寫入user_nicename列的內容劃分爲wp_users表。它將刪除空格,一些特殊字符並將大寫更改爲小寫。是否有可能更新user_nicename列而不進行消毒處理?在wordpress中定製wp_users表中的user_nicename

+0

嗨:)什麼是規範,你想做什麼。 –

+0

我想unsanitized nice_name。 – Stanly

回答

0

有可能使用pre_user_nicename過濾器。您可以在這裏閱讀https://developer.wordpress.org/reference/hooks/pre_user_nicename/過濾器在nicename消毒後立即應用,但我們仍然可以訪問unsanitized數據。這是WP-包括/ user.php的

/* 
* If a nicename is provided, remove unsafe user characters before using it. 
* Otherwise build a nicename from the user_login. 
*/ 
if (! empty($userdata['user_nicename'])) { 
    $user_nicename = sanitize_user($userdata['user_nicename'], true); 
    if (mb_strlen($user_nicename) > 50) { 
     return new WP_Error('user_nicename_too_long', __('Nicename may not be longer than 50 characters.')); 
    } 
} else { 
    $user_nicename = mb_substr($user_login, 0, 50); 
} 
$user_nicename = sanitize_title($user_nicename); 
// Store values to save in user meta. 
$meta = array(); 
/** 
* Filters a user's nicename before the user is created or updated. 
* 
* @since 2.0.3 
* 
* @param string $user_nicename The user's nicename. 
*/ 
$user_nicename = apply_filters('pre_user_nicename', $user_nicename); 
$raw_user_url = empty($userdata['user_url']) ? '' : $userdata['user_url']; 

您仍然可以通過訪問與用戶數據$ [「user_nicename」]變量用戶輸入的未修改nicename。因此,一個示例過濾器會是這樣的:

add_filter('pre_user_nicename', 'my_nicename_modification'); 
function my_nicename_modification($userdata) { 
    /*do anything you want with $userdata['user_nicename'] here 
    or leave blank if you want it saved just as the user typed it in */ 
    return $userdata['user_nicename']; 
} 

這將停止$user_nicename = sanitize_title($user_nicename);運行上,您可以瞭解更多關於此https://codex.wordpress.org/Function_Reference/sanitize_title。正如你在你的問題中解釋的那樣,這就是修改nicename。希望這可以幫助

+0

函數my_nicename_modification($ userdata ['user_nicename']){ }正在拋出一個錯誤。我在這裏沒有得到unsanitized nicename。 – Stanly

+0

它仍然無法正常工作,堅持 –

+0

我們正在將過濾的nicenameinside過濾掉,而不是$ userdata變量。 – Stanly

相關問題