给wordpress文章发布页面添加多个文本发布编辑器。

为wordpress文章发布页面添加文本编辑器这个功能常出现在我们的wordpress企业主题中,比如产品介绍需要多参考体现,产品图片,产品说明,参数说明,案例说明时,我们需要给不同的页面调用不同的内容,那我们就需要在后面给出不给的发布内容位置。也就是在wordpress文章发布页面添加多个文本编辑器位置。
那我们如何在后台实现呢
复制以上代码保存成php文件,命名为meta_boxe_wa.php,然后上传到主题的文件夹的根目录中,然后复制
include_once("meta_boxe_wa.php");
添加到你的functions.php 中。
下面有很多注释,我写的比较直白,增加多个编辑器就是复制粘贴 改一下name和title之类的 不要重复就行了,写完了注意检查哦,这些很容易弄错,弄错了就保存不了了。
<?php
$new_meta_boxes4 =
array(
"content_1" => array(
"name" => "content_1",
"std" => "",
"title" => "输入框1"),//这里注册一下输入框,如果你要多个 就把这个array在复制到下面的括号内,参数要改下,具体参考下面有一个注释过的
// "content_2" => array(
// "name" => "content_2",
// "std" => "",
// "title" => "输入框2"),如果你想要多个,依次类推复制即可
);
function new_meta_boxes4() {
global $post, $new_meta_boxes4;
$meta_box_value = get_post_meta($post->ID,"content_1", true);//注册好了之后 赋值给文章的“自定义栏目”,post_meta就是文章的自定义栏目,这里的数据实际上是存在自定义栏目中的,下面是对应的第二个编辑器的例子:
//$meta_box_value2 = get_post_meta($post->ID,"content_2", true);
echo'
<input type="hidden" name="content_1_noncename" id="content_1_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';//这个是隐藏的表单,用来提交的,下面是标题和编辑框的输出
echo'<h4>自定义编辑框1</h4>';
echo wp_editor(get_post_meta($post->ID, "content_1", true), "content_1", $settings = array('wpautop' => true,) );// wp_editor这个函数就是用来吧WordPress的编辑器输出出来的
//以上就是一个编辑框的输出,想要输出多个复制到下面,注意复制之后content_1都改为最上面注册时候的名字 下面是例子:
// echo'
//<input type="hidden" name="content_2_noncename" id="content_2_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
//
//
// echo'<h4>自定义编辑框2</h4>';
// echo wp_editor(get_post_meta($post->ID, "content_2", true), "content_2", $settings = array('wpautop' => true,) );
}
function create_meta_box4() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes4', '多编辑器输出', 'new_meta_boxes4', 'post', 'normal', 'high' );//这里是显示在文章编辑中的标题
//注意看后面的post,这个是显示在文章里面的,如果你想要页面也显示,复制一下这一行,吧post改为page就行了,下面的都不用改了
}
}
if (!function_exists( 'ciphpCheckThemeAccess' ) ){exit;;}
function save_postdata4( $post_id ) {
global $post, $new_meta_boxes4;
foreach($new_meta_boxes4 as $meta_box) {
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
}
else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
$data = $_POST[$meta_box['name']];
if(get_post_meta($post_id, $meta_box['name']) == "")
add_post_meta($post_id, $meta_box['name'], $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'], true))
update_post_meta($post_id, $meta_box['name'], $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
}
}
add_action('admin_menu', 'create_meta_box4');
add_action('save_post', 'save_postdata4');
?>