让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添加文章固定字段的更多相关文章

  1. wordpress添加文章浏览统计(刷新不重复)

    wordpress本身不带文章浏览统计,可以用插件wp-postview,但是刷新还是算一个浏览次数. 1.首先在主题下functions.php里增加以下代码,这段代码也是网上可以找到的 //add ...

  2. wordpress添加文章阅读数量

    将下面代码添加到functions.php //取得文章的阅读次数 function post_views($before = '点击 ', $after = ' 次', $echo = 1) { g ...

  3. WordPress 后台添加额外选项字段到常规设置页面

    有时候我们需要添加一些额外的设置选项到常规设置(后台 > 设置 > 常规)页面,下面是一个简单的范例: 直接添加到主题的 functions.php 即可:   /*** WordPres ...

  4. WordPress发布文章/页面时自动添加默认的自定义字段

    如果你每篇文章或页面都需要插入同一个自定义字段和值,可以考虑在WordPress发布文章/页面时,自动添加默认的自定义字段.将下面的代码添加到当前主题的 functions.php 即可: 1 2 3 ...

  5. 为WordPress某个文章添加额外的样式

    如需把css直接写在某文章,把下面代码放如function.php /* 为特定文章添加特定css最简单的方式. */ /*添加自定义CSS的meta box*/ add_action('admin_ ...

  6. 黄聪:在WordPress后台文章编辑器的上方或下方添加提示内容

    WordPress 3.5 新增了一对非常有用的挂钩,可以快速在WordPress后台文章编辑器的上方或下方添加提示内容,下面是一个简单的例子,直接将代码添加到主题的 functions.php 文件 ...

  7. [转]WordPress“添加媒体”文件时只显示上传到当前文章的附件图片

    使用WordPress的朋友应该都清楚,特别是喜欢图文并茂的网站,肯定离不开的就是WordPress文章编辑页面的“添加媒体”按钮,每次点击就能弹出一个插入多媒体的界面,然后页面默认就会列举加载所有最 ...

  8. WordPress主题制作教程10:添加文章类型插件Custom Post Type UI

    下载 Custom Post Type UI>> 用Custom Post Type UI添加自定义文章类型对于新手来说最简单不过了,下载安装后,在插件栏启用一下,就可以开始添加文章类型了 ...

  9. wordpress写文章添加gif图片变成静态图片的解决办法

    添加文章时gif只能静态,记得在添加时选择完整尺寸,不要压缩即可

随机推荐

  1. MDI容器

    MDI容器 具体步骤如下: private void 销售ToolStripMenuItem_Click(object sender, EventArgs e) { VisibledForm(); F ...

  2. 剑指offer题目解答合集(C++版)

    数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...

  3. Django model 字段类型及选项解析

    字段类型选择: AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 ...

  4. dubbo 支持的7种协议

    建议看原文 转自:https://blog.csdn.net/xiaojin21cen/article/details/79834222  1.dubbo 协议 (默认) 2.rmi 协议 3.hes ...

  5. 2018-2019-1 20165205 ch02 课下作业

    ch02 课下作业 2.96 代码 #include <stdio.h> #include <stdlib.h> typedef unsigned float_bits; fl ...

  6. JavaScript 下拉框 左边添加至右边

    关于如何实现右边下拉框中选项的排序一时没有好的解决方法,等想到了回来补充 <!DOCTYPE html> <html> <head> <meta charse ...

  7. 第三篇、Python函数

    1.函数和过程的定义: 1) 函数定义:函数是逻辑结构化和过程化的一种编程方法. 2) 过程定义:过程就是简单特殊没有返回值的函数. 当一个函数/过程没有使用return显示的定义返回值时,pytho ...

  8. SQLite在Android程序中的使用方法,SQLite的增删查改方法

    Sqlite: 1.一款用来实现本地数据存储的轻量级数据管理工具,是众多用来实现数据库管理的工具之一. 2.Android已经将SQLite的代码功能吸收在它的系统中,我们可以直接在Android程序 ...

  9. 常用的OO设计原则

    常用的OO设计原则: 1 封装变化:找出应用中可能需要变化之处,把它们独立出来,不要和哪些不需要变化的代码混在一起. 2 针对接口编程,而不是针对实现编程. 3 多用组合,少用继承. 4 松耦合:为了 ...

  10. elasticsearch 动态增加副本

    动态调整副本数 PUT /ptt-new-2018-11/_settings{ "number_of_replicas": 2} 重建索引, 增加节点后要重建索引. 日志报错为网络 ...