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只能静态,记得在添加时选择完整尺寸,不要压缩即可
随机推荐
- REST framwork之分页器,路由器,响应器
一 REST framwork分页器: from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination ...
- Suricata之outputs(输出选项)
outputs stats.log(统计数据包信息的日志) 配置文件:默认开启,数据包统计时间间隔默认为8S 日志文件:主要统计规定时间间隔内数据包的总量,各种不同类型数据包的数量以及数据包的大小等信 ...
- mysql 多个字段合并
group_concat 函数默认“,”合并 select p.id as patent_id,p.application_no,p.name,p.inventer,group_concat(pi.` ...
- Hash和HashCode深入理解
目录介绍1.Hash的作用介绍1.1 Hash的定义1.2 Hash函数特性1.3 Hash的使用场景2.如何判断两个对象相等2.1 判断两个字符串2.2 判断两个int数值2.3 其他基本类型3.H ...
- python大法好——递归、内置函数、函数进阶
1.递归(自己干自己) def fun(n): n=n/2 print(n) if(n>2) fun(n) #函数调用 fun(10) 结果是5 2 1 0 递归的执行过程:递归执行到最里面一层 ...
- 预浸料(Prepreg,PreimpregnatedMaterials)
预浸料(Prepreg,PreimpregnatedMaterials),是把基体(Matrix)浸渍在强化纤维(Reinforced Fiber)中制成的预浸片材产品,是复合材料的中间材料.
- 尚硅谷springboot学习27-使用外置servlet容器
嵌入式Servlet容器:应用打成可执行的jar 优点:简单.便携: 缺点:默认不支持JSP.优化定制比较复杂(使用定制器[ServerProperties.自定义EmbeddedServle ...
- 制造业期刊-ZT
小虫一名英国博后,前阵发书,认识了很多机械制造领域的伙伴.得知我录用了多篇顶刊后,很多人私聊我求经验. 哎,哪里那么容易.回想过去5年,制造领域的期刊基本都被拒过一圈.当年自己投稿时就发现,制造顶刊的 ...
- properties文件读写工具类
java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...
- 3. Go语言基本类型
Go语言基本类型如下: bool string 数值类型 (int8, int16, int32, int64, int, uint8, uint16, uint32, uint64, uint, f ...