1. 程式人生 > 程式設計 >教你如何在WordPress釋出文章時自定義文章作者名稱

教你如何在WordPress釋出文章時自定義文章作者名稱

有時候會收到一些投稿文章,或者也會轉載別人的文章,新建立一個使用者又有些麻煩,但在作者名稱那裡顯示自己的名字,總不是那麼和諧。今天倡萌推薦 @西秦公子 的一個小外掛,支援在後臺自定義當前文章的作者名稱,效果如下圖所示:

教你如何在WordPress釋出文章時自定義文章作者名稱

直接在後臺外掛安裝介面搜尋“自定義作者名稱”即可線上安裝,或者到官方下載:https://litepress.cn/plugins/custom-author/

如果轉載或投稿文章比較多,倡萌建議單獨建立一個專門用於釋出這類文章的使用者,然後釋出的文章的時候,自定義一下作者名稱即可。

下面來看看這個小外掛的程式碼:

<?
/*
Plugin Name: 	Custom Author
Plugin URI: 	https://www.ixiqin.com/2018/06/wordpress-custom-auTZyVkcHRQY
thor-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 : [email protected]) 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 Fo
undation; 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'); /** 建立一個checkBox */ function cus_author_createCustomField() { $po客棧
st_id = get_the_ID(); if (get_post_type($post_id) != 'post') { return; } /** * 提取現有的值 * @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>作者:<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) { /** * 自動儲存不處理 */ 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; } /** * 使用者無權編輯文章不處理 */ if (!current_user_can('edit_post',$post_id)) { return; } /** * 存在此專案就更新 */ if (isset($_POST['_custom_author_name'])) { update_post_meta($post_id,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; } }

核心思路就是通過鉤子 the_authhttp://www.cppcns.comor 來修改了文章作者的顯示名稱。限定了文章型別為 post(文章),見32行。

至此關於在WordPress釋出文章時自定義文章作者名稱就結束了,更多關於WordPress技巧與外掛請檢視下面的相關連結