有时候网站会收到一些投稿文章,或者也会转载别人的文章,新创建一个用户又有些麻烦,但在作者名称那里显示自己的名字,总不是那么和谐。今天倡萌推荐 @西秦公子 的一个小插件,支持在后台自定义当前文章的作者名称,效果如下图所示:

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

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

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

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?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 来修改了文章作者的显示名称。
  • 限定了文章类型为 post(文章),见32行

WordPress自定义文章作者名称相关推荐

  1. php详情页模板怎么做,WordPress自定义文章详情页模板

    想要让某个特定分类文章页面样式区别于其他分类,我们可以自定义模板来实现.今天我们分享一下WordPress自定义文章详情页模板,首先在所用WordPress主题根目录新建一个名称 single-wor ...

  2. WORDPRESS自定义文章列表显示

    最近还在捣鼓wordpress主题,在露兜博客 里看到了query_posts用法汇总,收藏过来备用- 基本可以通过query_posts函数可以实现随机阅读.评论最多和标题排序就等功能吧~ 基本用法 ...

  3. Wordpress 自定义文章类型添加 Categoried、Tags

    默认情况下 ,自定义文章类型没有分类和标签属性,需要通过 register_taxonomy_for_object_type 手动注册文章分类和标签,可以通过在 functions.php 或插件中添 ...

  4. Wordpress 自定义文章类型的显示以及分页问题

    前提:自己开发了一套主题,并且全局使用了自定义了文章类型. 首页调用了四个分类来显示,并且每个分类显示8个文章,使用查询方式如下: $args = array('post_type'=>'vid ...

  5. WordPress 修改自定义文章类型的固定链接结构

    关于自定义文章类型和固定链接结构,大家可以想回顾一下: WordPress 自定义文章类型 介绍及实例解说(上) WordPress 自定义文章类型 介绍及实例解说(下) WordPress快速添加多 ...

  6. php 修改 wordpress,修改WordPress中文章编辑器的样式的方法详解

    这篇文章主要介绍了修改WordPress中文章编辑器的样式的方法详解,同时文中也推荐了两款取代默认文章编辑器的插件,需要的朋友可以参考下 自定义文章编辑器的样式每一个 WordPress 主题的文章样 ...

  7. 一文说透WordPress的自定义文章类型

    转自丘壑博客 从2004年的1.0版本算起,WordPress在14年间已经迭代开发到了5.x版.如果说这中间哪个版本是一个质的提升的话,那应该算是2010年发布的代号为Thelonious 的 3. ...

  8. html5置顶按钮如何添加,WordPress如何自定义文章开启置顶按钮?

    WordPerss想制作置顶的功能,结果竟然发现自定义文章类型没有置顶的功能选项,查阅资料后发现WP只是没有显示置顶的选项,功能和文章类型一样.那么WordPress如何自定义文章开启置顶按钮? 添加 ...

  9. 转:绝对干货--WordPress自定义查询wp_query所有参数详细注释

    1 <?php 2 /** 3 * WordPress 查询综合参考 4 * 编译:luetkemj - luetkemj.com 5 * 6 * 官方文档: http://codex.word ...

最新文章

  1. PrestaShop 网站漏洞修复如何修复
  2. 27岁清华留美博士疑似跳机自杀!:常年被抑郁症困扰
  3. Exchange 2003允许部分用户发送邮件到部分外网服务器
  4. jquery全选/取消全选(反选)/单选操作
  5. CSS+DIV-公司网站
  6. linux同时链接多个库,通过Shell脚本同时监控多个数据库负载
  7. CSS实现图片居中且缩放不影响图片纵横比
  8. 计算机网络 tcp 阻塞,读书笔记:计算机网络第7章:阻塞控制
  9. 极简易版专家聊天程序--JAVA练手
  10. 从网页(WEB)登录SAP
  11. 运维前线:一线运维专家的运维方法、技巧与实践导读
  12. JAVA多线程与并发学习总结
  13. 春节后赚钱风口在哪?
  14. Win10 x64 VS2015 MFC打开主对话框报错:“未在此计算机上注册activex控件{648A5600-2C6E-101B-82B6-000000000014}”
  15. swagger注释HTML,Swagger注解生成Rest Api文档
  16. 直通串口线和交叉串口线
  17. 软件项目招投标中的“标的额”是什么意思?
  18. POJ 3264 线段树
  19. 朴素贝叶斯-后验概率最大化
  20. Vue3.0源码解读 - 响应式系统

热门文章

  1. Waifu2x 算法黑科技二次元图片无损放大
  2. 教你3步拯救word型PPT
  3. 帆软、永洪 BI、瓴羊 Quick BI 等工具,都有哪些特点呢?
  4. 如何使用sublime3愉快的编写vue项目(踩坑总结)
  5. 晶体塑性有限元 Abaqus 三维泰森多边形(voronoi模型)插件 V6.0
  6. 初识RFID识别技术
  7. 自然科学六大基础学科:数学,物理学,化学,生物学,地球科学,天文学
  8. Oracle概念及常用语句(一)
  9. 今天带软测2班学员做面试前的试题(每天几道面试题分析)
  10. Guacamole搭建教程