如何在WordPress发布文章时自定义文章作者名称

发布时间:2021-09-24 15:03:13 作者:柒染
来源:亿速云 阅读:130

本篇文章给大家分享的是有关如何在WordPress发布文章时自定义文章作者名称,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

有时候网站会收到一些投稿文章,或者也会转载别人的文章,新创建一个用户又有些麻烦,但在作者名称那里显示自己的名字,总不是那么和谐。效果如下图所示:

如何在WordPress发布文章时自定义文章作者名称

直接在后台插件安装界面搜索“自定义作者名称”即可在线安装,或者到官方下载:https://litepress.cn/plugins/custom-author/

如果转载或投稿文章比较多,倡萌建议单独创建一个专门用于发布这类文章的用户,然后发布的文章的时候,自定义一下作者名称即可。

下面来看看这个小插件的代码:

<?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');
/** 创建一个checkBox */
function cus_author_createCustomField() {
	$post_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><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) {
	/**
	 * 自动保存不处理
	 */
	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, '_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;
	}
}

核心思路就是通过钩子 the_author 来修改了文章作者的显示名称。

以上就是如何在WordPress发布文章时自定义文章作者名称,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. wordpress 文章选择不同展示模版
  2. 操作WordPress数据库发布文章

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

wordpress

上一篇:在WordPress的页眉和页脚添加代码方法是怎样的

下一篇:python中数据类型的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》