立即注册 找回密码

QQ登录

只需一步,快速开始

查看: 3212|回复: 0

[Wordpress 通用教程] WordPress自定义文章作者名称的方法

[复制链接]
发表于 2019-10-1 12:30:33 | 显示全部楼层 |阅读模式
道勤网-数据www.daoqin.net

亲注册登录道勤网-可以查看更多帖子内容哦!(包涵精彩图片、文字详情等)请您及时注册登录-www.daoqin.net

您需要 登录 才可以下载或查看,没有账号?立即注册

x
有时候网站会收到一些投稿文章,或者也会转载别人的文章,新创建一个用户又有些麻烦,但在作者名称那里显示自己的名字,总不是那么和谐。今天道勤网小编给大家介绍一个小插件,支持在后台自定义当前文章的作者名称,效果如下图所示:

WordPress自定义文章作者名称的方法

WordPress自定义文章作者名称的方法

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

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

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

  1. <?php
  2. /*
  3. Plugin Name:         Custom Author
  4. Plugin URI:         https://www.ixiqin.com/2018/06/wordpress-custom-author-plugin/
  5. Description:         自定义作者插件
  6. Version:                 1.0
  7. Author:                 Bestony
  8. Author URI:         https://www.ixiqin.com/
  9. License:                 GPL2
  10. License URI:          https://www.gnu.org/licenses/gpl-2.0.html
  11. */
  12. /*  Copyright  2018 Bestony (email : xiqingongzi@gmail.com)
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  24. */


  25. add_action('post_submitbox_misc_actions', 'cus_author_createCustomField');
  26. add_action('save_post', 'cus_author_saveCustomField');
  27. /** 创建一个checkBox */
  28. function cus_author_createCustomField() {
  29.         $post_id = get_the_ID();
  30.         if (get_post_type($post_id) != 'post') {
  31.                 return;
  32.         }
  33.         /**
  34.          * 提取现有的值
  35.          * @var boolean
  36.          */
  37.         $value = get_post_meta($post_id, '_custom_author_name', true);
  38.         /**
  39.          * 添加 nonce 安全处理
  40.          */
  41.         wp_nonce_field('custom_author_nonce' , 'custom_author_nonce');
  42.         ?>
  43.     <div class="misc-pub-section misc-pub-section-last dashicons-before dashicons-admin-users">
  44.         <label><b>作者:</b><input type="text" value="<?php echo $value ?>" name="_custom_author_name" /></label>
  45.     </div>
  46.     <?php   
  47. }
  48. /**
  49. * 保存配置信息
  50. * @param  int $post_id 文章的ID
  51. */
  52. function cus_author_saveCustomField($post_id) {
  53.         /**
  54.          * 自动保存不处理
  55.          */
  56.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  57.                 return;
  58.         }
  59.         /**
  60.          * nonce 信息不正确不处理
  61.          */
  62.         if (
  63.                 !isset($_POST['custom_author_nonce']) ||
  64.                 !wp_verify_nonce($_POST['custom_author_nonce'], 'custom_author_nonce')
  65.         ) {
  66.                 return;
  67.         }
  68.         /**
  69.          * 用户无权编辑文章不处理
  70.          */
  71.         if (!current_user_can('edit_post', $post_id)) {
  72.                 return;
  73.         }
  74.         /**
  75.          * 存在此项目就更新
  76.          */
  77.         if (isset($_POST['_custom_author_name'])) {
  78.                 update_post_meta($post_id, '_custom_author_name', sanitize_text_field($_POST['_custom_author_name']));
  79.         } else {
  80.                 /**
  81.                  * 不存在就删除
  82.                  */
  83.                 delete_post_meta($post_id, '_custom_author_name');
  84.         }
  85. }

  86. add_filter('the_author','cus_author_the_author');
  87. function cus_author_the_author($author){
  88.     $custom_author = get_post_meta(get_the_ID(), '_custom_author_name');
  89.     if ($custom_author) {
  90.                 return $custom_author[0];
  91.         } else {
  92.                 return $author;
  93.         }
  94. }
复制代码
  • 核心思路就是通过钩子 the_author 来修改了文章作者的显示名称。
  • 限定了文章类型为 post(文章),见32行。

道勤主机提供365天*24小时全年全天无休、实时在线、零等待的售后技术支持。竭力为您免费处理您在使用道勤主机过程中所遇到的一切问题! 如果您是道勤主机用户,那么您可以通过QQ【792472177】、售后QQ【59133755】、旺旺【诠释意念】、微信:q792472177免费电话、后台提交工单这些方式联系道勤主机客服! 如果您不是我们的客户也没问题,点击页面最右边的企业QQ在线咨询图标联系我们并购买后,我们为您免费进行无缝搬家服务,让您享受网站零访问延迟的迁移到道勤主机的服务!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

道勤网- 推荐内容!上一条 /2 下一条

!jz_fbzt! !jz_sgzt! !jz_xgzt! 快速回复 !jz_fhlb! !jz_lxwm! !jz_gfqqq!

关于我们|手机版|小黑屋|地图|【道勤网】-www.daoqin.net 软件视频自学教程|免费教程|自学电脑|3D教程|平面教程|影视动画教程|办公教程|机械设计教程|网站设计教程【道勤网】 ( 皖ICP备15000319号-1 )

GMT+8, 2024-3-28 17:37

Powered by DaoQin! X3.4 © 2016-2063 Dao Qin & 道勤科技

快速回复 返回顶部 返回列表