<span style="color: #000000; font-weight: bold;"><?php</span>
<span style="color: #666666; font-style: italic;">/*
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
*/</span>
<span style="color: #666666; font-style: italic;">/* 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
*/</span>
add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'post_submitbox_misc_actions'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cus_author_createCustomField'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
add_action<span style="color: #009900;">(</span><span style="color: #0000ff;">'save_post'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cus_author_saveCustomField'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/** 创建一个checkBox */</span>
<span style="color: #000000; font-weight: bold;">function</span> cus_author_createCustomField<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #000088;">$post_id</span> <span style="color: #339933;">=</span> get_the_ID<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span>get_post_type<span style="color: #009900;">(</span><span style="color: #000088;">$post_id</span><span style="color: #009900;">)</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'post'</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #009933; font-style: italic;">/**
* 提取现有的值
* @var boolean
*/</span>
<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">(</span><span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_custom_author_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/**
* 添加 nonce 安全处理
*/</span>
wp_nonce_field<span style="color: #009900;">(</span><span style="color: #0000ff;">'custom_author_nonce'</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'custom_author_nonce'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?></span>
<div class="misc-pub-section misc-pub-section-last dashicons-before dashicons-admin-users">
<label><b>作者:</b><input type="text" value="<span style="color: #000000; font-weight: bold;"><?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$value</span> <span style="color: #000000; font-weight: bold;">?></span>" name="_custom_author_name" /></label>
</div>
<span style="color: #000000; font-weight: bold;"><?php</span>
<span style="color: #009900;">}</span>
<span style="color: #009933; font-style: italic;">/**
* 保存配置信息
* @param int $post_id 文章的ID
*/</span>
<span style="color: #000000; font-weight: bold;">function</span> cus_author_saveCustomField<span style="color: #009900;">(</span><span style="color: #000088;">$post_id</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #009933; font-style: italic;">/**
* 自动保存不处理
*/</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #990000;">defined</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'DOING_AUTOSAVE'</span><span style="color: #009900;">)</span> <span style="color: #339933;">&&</span> DOING_AUTOSAVE<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #009933; font-style: italic;">/**
* nonce 信息不正确不处理
*/</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span>
<span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">(</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'custom_author_nonce'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span> <span style="color: #339933;">||</span>
<span style="color: #339933;">!</span>wp_verify_nonce<span style="color: #009900;">(</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'custom_author_nonce'</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'custom_author_nonce'</span><span style="color: #009900;">)</span>
<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #009933; font-style: italic;">/**
* 用户无权编辑文章不处理
*/</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #339933;">!</span>current_user_can<span style="color: #009900;">(</span><span style="color: #0000ff;">'edit_post'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post_id</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #009933; font-style: italic;">/**
* 存在此项目就更新
*/</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #990000;">isset</span><span style="color: #009900;">(</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'_custom_author_name'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
update_post_meta<span style="color: #009900;">(</span><span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_custom_author_name'</span><span style="color: #339933;">,</span> sanitize_text_field<span style="color: #009900;">(</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'_custom_author_name'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">{</span>
<span style="color: #009933; font-style: italic;">/**
* 不存在就删除
*/</span>
delete_post_meta<span style="color: #009900;">(</span><span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_custom_author_name'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span>
add_filter<span style="color: #009900;">(</span><span style="color: #0000ff;">'the_author'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'cus_author_the_author'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">function</span> cus_author_the_author<span style="color: #009900;">(</span><span style="color: #000088;">$author</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
<span style="color: #000088;">$custom_author</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">(</span>get_the_ID<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_custom_author_name'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$custom_author</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$custom_author</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">{</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$author</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span>
请先
!