教你如何在WordPress發(fā)布文章時(shí)自定義文章作者名稱
有時(shí)候網(wǎng)站會(huì)收到一些投稿文章,或者也會(huì)轉(zhuǎn)載別人的文章,新創(chuàng)建一個(gè)用戶又有些麻煩,但在作者名稱那里顯示自己的名字,總不是那么和諧。今天倡萌推薦 @西秦公子 的一個(gè)小插件,支持在后臺(tái)自定義當(dāng)前文章的作者名稱,效果如下圖所示:

直接在后臺(tái)插件安裝界面搜索“自定義作者名稱”即可在線安裝,或者到官方下載:https://litepress.cn/plugins/custom-author/
如果轉(zhuǎn)載或投稿文章比較多,倡萌建議單獨(dú)創(chuàng)建一個(gè)專門(mén)用于發(fā)布這類文章的用戶,然后發(fā)布的文章的時(shí)候,自定義一下作者名稱即可。
下面來(lái)看看這個(gè)小插件的代碼:
<?php
/*
Plugin Name: Custom Author
Plugin URI: https://www.ixiqin.com/2018/06/wordpress-custom-author-plugin/
Description: 自定義作者插件
Version: 1.0
Author: Bestony
Author URI: https://www.ixiqin.com/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/* Copyright 2018 Bestony (email : xiqingongzi@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
add_action('post_submitbox_misc_actions', 'cus_author_createCustomField');
add_action('save_post', 'cus_author_saveCustomField');
/** 創(chuàng)建一個(gè)checkBox */
function cus_author_createCustomField() {
$post_id = get_the_ID();
if (get_post_type($post_id) != 'post') {
return;
}
/**
* 提取現(xiàn)有的值
* @var boolean
*/
$value = get_post_meta($post_id, '_custom_author_name', true);
/**
* 添加 nonce 安全處理
*/
wp_nonce_field('custom_author_nonce' , 'custom_author_nonce');
?>
<div class="misc-pub-section misc-pub-section-last dashicons-before dashicons-admin-users">
<label><b>作者:</b><input type="text" value="<?php echo $value ?>" name="_custom_author_name" /></label>
</div>
<?php
}
/**
* 保存配置信息
* @param int $post_id 文章的ID
*/
function cus_author_saveCustomField($post_id) {
/**
* 自動(dòng)保存不處理
*/
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
/**
* nonce 信息不正確不處理
*/
if (
!isset($_POST['custom_author_nonce']) ||
!wp_verify_nonce($_POST['custom_author_nonce'], 'custom_author_nonce')
) {
return;
}
/**
* 用戶無(wú)權(quán)編輯文章不處理
*/
if (!current_user_can('edit_post', $post_id)) {
return;
}
/**
* 存在此項(xiàng)目就更新
*/
if (isset($_POST['_custom_author_name'])) {
update_post_meta($post_id, '_custom_author_name', sanitize_text_field($_POST['_custom_author_name']));
} else {
/**
* 不存在就刪除
*/
delete_post_meta($post_id, '_custom_author_name');
}
}
add_filter('the_author','cus_author_the_author');
function cus_author_the_author($author){
$custom_author = get_post_meta(get_the_ID(), '_custom_author_name');
if ($custom_author) {
return $custom_author[0];
} else {
return $author;
}
}
核心思路就是通過(guò)鉤子 the_author 來(lái)修改了文章作者的顯示名稱。限定了文章類型為 post(文章),見(jiàn)32行。
至此關(guān)于在WordPress發(fā)布文章時(shí)自定義文章作者名稱就結(jié)束了,更多關(guān)于WordPress技巧與插件請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章
編寫(xiě)高質(zhì)量代碼的30條黃金守則(首選隱式類型轉(zhuǎn)換)
這篇文章主要介紹了編寫(xiě)高質(zhì)量代碼的30條黃金守則(首選隱式類型轉(zhuǎn)換),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
解決Git推送錯(cuò)誤non-fast-forward的方法
這篇文章介紹了解決Git推送錯(cuò)誤non-fast-forward的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
關(guān)于postman上傳文件執(zhí)行成功而使用collection runner執(zhí)行失敗的問(wèn)題
這篇文章主要介紹了關(guān)于postman上傳文件執(zhí)行成功而使用collection runner執(zhí)行失敗的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
VSCode使用ssh密鑰免密遠(yuǎn)程登錄服務(wù)器的方法
本文主要介紹了VSCode使用ssh密鑰免密遠(yuǎn)程登錄服務(wù)器的方法,文中通過(guò)圖文代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
綁定/約束 (binding)指兩個(gè)東西之間的關(guān)聯(lián)
綁定/約束 (binding)指兩個(gè)東西之間的關(guān)聯(lián)。如 名字 與它所代表的事物。又如屬性與實(shí)體之間的關(guān)聯(lián),又或者符號(hào)與操作之間的關(guān)聯(lián)。2011-01-01
xmlHttp ie6下不跨域還提示沒(méi)有權(quán)限,ie8下不會(huì)有這錯(cuò)誤
昨天晚上叫我好弄啊,最后發(fā)現(xiàn)原因是我url太長(zhǎng)了,最后發(fā)現(xiàn)URL在2070B左右就不行了2009-04-04
使用MASA?Blazor開(kāi)發(fā)查詢表格頁(yè)
MASA?Blazor?是一個(gè)基于?Material?Design?設(shè)計(jì)語(yǔ)言的?Blazor?組件庫(kù),dotNET開(kāi)發(fā)者只需或者甚至不需要懂得?javascript?就能開(kāi)發(fā)一個(gè)企業(yè)級(jí)中后臺(tái)系統(tǒng),這篇文章主要介紹了使用MASA?Blazor開(kāi)發(fā)一個(gè)標(biāo)準(zhǔn)的查詢表格頁(yè),需要的朋友可以參考下2022-04-04
VSCode程序猿彩虹屁插件rainbow fart體驗(yàn)篇
這篇文章主要介紹了VSCode程序猿彩虹屁插件rainbow fart體驗(yàn),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

