wordpress添加文章固定字段
让wordpress的文章数据表 增加一个字段,使其能在文章编辑页能编辑,并能通过rest api 获取出来。
例:给文章加一个缩略图字段 litpic
首先 通过mysql 给文章表 wp_posts 加一个字段 litpic
然后在主题的function.php 后面添加如下代码:
add_action( 'add_meta_boxes', 'myplugin_add_custom_box');
add_action( 'save_post', 'myplugin_save_postdata');
function myplugin_add_custom_box() {
add_meta_box(
'myplugin_sectionid',
'设置缩略图', // 可自行修改标题文字
'myplugin_inner_custom_box',
'post'
);
}
function myplugin_inner_custom_box( $post ) {
global $wpdb;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
// 获取固定字段litpic的值,用于显示之前保存的值
// 此处wp_posts新添加的字段为litpic,多个用半角逗号隔开
$date = $wpdb->get_row( $wpdb->prepare( "SELECT litpic FROM $wpdb->posts WHERE ID = %d", $post->ID) );
// litpic 字段输入框的HTML代码
echo '<label for="litpic_new_field">图片url </label>';
echo '<input type="text" id="litpic_new_field" name="litpic_new_field" value="'.$date->litpic.'" size="28" />';
// 多个字段依此类推
}
function myplugin_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( ’DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
return;
// 权限验证
if ( 'post' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
// 获取编写文章时填写的固定字段的值,多个字段依此类推
$litpic = $_POST['litpic_new_field'];
global $wpdb;
$wpdb->update( "$wpdb->posts",
// 以下一行代码,多个字段的话参照下面的写法,单引号中是字段名,右边是变量值。半角逗号隔开
array( 'litpic' => $litpic),
array( 'ID' => $post_id ),
// 添加了多少个新字段就写多少个%s,半角逗号隔开
array( '%s'),
array( '%d')
);
}
添加后,文章页会显示litpic字段的输入框,如图:

但此时 rest api还不会把litpic字段输出。
打开 /wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 文件。
添加如下代码:
if ( ! empty( $schema['properties']['litpic'] ) ) {
$data['litpic'] = $post->litpic;
}
'litpic' => array(
'description' => __( 'A litpic to protect access to the content and excerpt.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
),
$post_type_attributes = array(
'title',
'editor',
'author',
'excerpt',
'thumbnail',
'comments',
'revisions',
'page-attributes',
'post-formats',
'custom-fields',
'litpic',
);
$fixed_schemas = array(
'post' => array(
'title',
'editor',
'author',
'excerpt',
'thumbnail',
'comments',
'revisions',
'post-formats',
'custom-fields',
'litpic',
),
case 'litpic':
$schema['properties']['litpic'] = array(
'description' => __( 'The ID for the litpic of the object.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
);
break;
现在,rest api 就可以把litpic 字段输出了。
wordpress添加文章固定字段的更多相关文章
- wordpress添加文章浏览统计(刷新不重复)
wordpress本身不带文章浏览统计,可以用插件wp-postview,但是刷新还是算一个浏览次数. 1.首先在主题下functions.php里增加以下代码,这段代码也是网上可以找到的 //add ...
- wordpress添加文章阅读数量
将下面代码添加到functions.php //取得文章的阅读次数 function post_views($before = '点击 ', $after = ' 次', $echo = 1) { g ...
- WordPress 后台添加额外选项字段到常规设置页面
有时候我们需要添加一些额外的设置选项到常规设置(后台 > 设置 > 常规)页面,下面是一个简单的范例: 直接添加到主题的 functions.php 即可: /*** WordPres ...
- WordPress发布文章/页面时自动添加默认的自定义字段
如果你每篇文章或页面都需要插入同一个自定义字段和值,可以考虑在WordPress发布文章/页面时,自动添加默认的自定义字段.将下面的代码添加到当前主题的 functions.php 即可: 1 2 3 ...
- 为WordPress某个文章添加额外的样式
如需把css直接写在某文章,把下面代码放如function.php /* 为特定文章添加特定css最简单的方式. */ /*添加自定义CSS的meta box*/ add_action('admin_ ...
- 黄聪:在WordPress后台文章编辑器的上方或下方添加提示内容
WordPress 3.5 新增了一对非常有用的挂钩,可以快速在WordPress后台文章编辑器的上方或下方添加提示内容,下面是一个简单的例子,直接将代码添加到主题的 functions.php 文件 ...
- [转]WordPress“添加媒体”文件时只显示上传到当前文章的附件图片
使用WordPress的朋友应该都清楚,特别是喜欢图文并茂的网站,肯定离不开的就是WordPress文章编辑页面的“添加媒体”按钮,每次点击就能弹出一个插入多媒体的界面,然后页面默认就会列举加载所有最 ...
- WordPress主题制作教程10:添加文章类型插件Custom Post Type UI
下载 Custom Post Type UI>> 用Custom Post Type UI添加自定义文章类型对于新手来说最简单不过了,下载安装后,在插件栏启用一下,就可以开始添加文章类型了 ...
- wordpress写文章添加gif图片变成静态图片的解决办法
添加文章时gif只能静态,记得在添加时选择完整尺寸,不要压缩即可
随机推荐
- winfrom
WINFORM(winform) windows窗体应用程序(.NET Framework4,版本太高了不好,选中Visual c#) 客户端应用程序的特点是:所见即所得,就是说,编辑的什么样,启动之 ...
- python 列表、元组、字典的区别
区别: 相互转换:https://www.cnblogs.com/louis-w/p/8391147.html 一.列表 list [1,[2,'AA'],5,'orderl'] 1.任意对象的有序集 ...
- tkinter窗口居中方法
tkinter窗口居中 from tkinter import * class MyFrm(Frame): def __init__(self, master): self.root=master s ...
- javascript正则表达式中 (?=exp)、(?<=exp)、(?!exp)
(?=exp) 百度百科给的解释:非获取匹配,正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串,该匹配不需要获取供以后使用.例如,“Windows(?=95|98|NT|2000) ...
- 详细分析MySQL事务日志(redo log和undo log) 表明了为何mysql不会丢数据
innodb事务日志包括redo log和undo log.redo log是重做日志,提供前滚操作,undo log是回滚日志,提供回滚操作. undo log不是redo log的逆向过程,其实它 ...
- mysql数据库字段内容替换
UPDATE 表名 SET 字段名= replace(字段名, '查找内容', '替换成内容') ; UPDATE car_articles SET article_title = replace(a ...
- elasticsearch 动态增加副本
动态调整副本数 PUT /ptt-new-2018-11/_settings{ "number_of_replicas": 2} 重建索引, 增加节点后要重建索引. 日志报错为网络 ...
- linux系统,服务器与服务器拷贝文件
服务器与服务器拷贝文件命令 scp -P (服务器端口)-r 拷贝文件名称列表 远程服务器用户@远程服务器ip :(文件放置目录) 1.将本地home目录下的apache-tomcat-8.0. ...
- linux环境启动数据库
1.查看数据库监听的状态: 监听状态:lsnrctl status 出现如下列截图所示数据,说明切切换账户有问题:切换账户时要家:-: 如 su - oracle 第一步:打开Oracle监听$ ...
- Codeforces Round #508 (Div. 2)
Codeforces Round #508 (Div. 2) http://codeforces.com/contest/1038 A #include<bits/stdc++.h> us ...