让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. 表单enctype不对导致action中无法接受数据

    表单enctype不对导致action中无法接受数据 描述:在用ssh开发项目的时候,可能会遇到一个问题, 那就是明明我的表单字段和JavaBean类中的字段都是一一对应的,而且action也实现了模 ...

  2. 1816647 - Error "Data file of SAP Note is incomplete" uploading a note in SNOTE

    ymptom When uploading an SAP Note in transaction SNOTE you receive the error "Data file of SAP ...

  3. centos下安装djangobb

    曾经在freenas虚拟环境下安装过djangobb,因为要安装的依赖文件太多,最后没有安装成功. 今晚在centos6.9 下,先创建了虚拟环境,然后照着官方网站的快速安装指南,安装后也运行不了,后 ...

  4. Sql Server数据库之约束

    一.约束的分类 实体约束:关于行的约束,比如某一行出现的值就不允许别的行出现,如主键 域约束:关于列的约束,对表中所有行的某些列进行约束,如check约束 参照完整性约束:如果某列的值必须与其他列的值 ...

  5. .do的消除

    其实就是在web.xml中去掉.do即可  那里有拦截器作用,什么样的文件可以进入前端控制器1

  6. Jquery中的 Deferred分析

    参考:https://www.idaima.com/a/1627.html http://www.cnblogs.com/aaronjs/p/3356505.html 未完!

  7. type的解释

    在jquery-19.1.1源码中,type,检查对象的类型是:Boolean/Number/String/Function/Array/Date/RegExp/Object/Error中的一种,返回 ...

  8. IDEA 工具从Json自动生成JavaBean

    1.先安装GsonFormat插件:File-->Setting-->Plugins-->GsonFormat-->OK 2.new 一个新的Class空文件,然后 Alt+I ...

  9. 《java与模式》阅读笔记02

    java语言的接口 在之前的编程作业中,我或多或少都用到了java的接口,但是接口的具体意思是什么,又该如何更好的使用呢?这个确实一知半解,带着这个问题我读了关于这些内容的章节. 所谓接口(inter ...

  10. 小强学渲染之OpenGL渲染管线详析

    什么是OpenGL? OpenGL是一套图形硬件的软件API接口库,它直接和GPU交互,将3D场景渲染绘制到2D屏幕上.总结说,OpenGL的功能是将程序中定义的各种2D或3D模型绘制到帧缓存中,或者 ...