这是我试图保存的元框代码。我有其他一切工作我想要的但我无法弄清楚如何保存数据从插入到函数clone_to()
我在该部分添加了几行评论,以显示我想要保存的内容。从那里我希望我的保存元数据功能是有道理的。
希望有人会注意到我做错了什么。提前致谢。
<?php
// enqueue scripts and styles, but only if is_admin
if(is_admin()) {
wp_enqueue_script('custom-js', get_template_directory_uri().'/js/custom-js.js');
wp_enqueue_script('jquery.confirm.min', get_template_directory_uri().'/js/jquery.confirm.min.js');
wp_enqueue_script('bootstrap.min', get_template_directory_uri().'/js/bootstrap/js/bootstrap.min.js');
wp_enqueue_script('edit-box', get_template_directory_uri().'/js/edit-box.js');
wp_enqueue_style('jquery-ui-custom', get_template_directory_uri().'/css/jquery-ui-custom.css');
wp_enqueue_style('page-builder', get_template_directory_uri().'/css/page-builder.css');
wp_enqueue_style('bootstrap', get_template_directory_uri().'/js/bootstrap/css/bootstrap.css');
}
// Add the Meta Box
function add_custom_meta_box() {
add_meta_box(
'custom_meta_box', // $id
'Custom Meta Box', // $title
'show_custom_meta_box', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box');
$custom_meta_fields = array(
'Divider' =>array(
'text'=>array(
'title'=> 'BACK TO TOP TEXT',
'name'=> 'page-option-item-divider-text',
'type'=> 'inputtext'
)
),
'Column'=>array(
'column-text'=>array(
'title' => 'Column Text',
'name' => 'page-option-item-column-text',
'type' => 'textarea'
)
),
'Quote'=>array(
'quote-text'=>array(
'title' => 'Quote Text',
'name' => 'page-option-item-quote-text',
'type' => 'textarea'
)
)
);
?>
<?php
// The Callback
function show_custom_meta_box() {
global $custom_meta_fields, $post;
echo '<div id="overlay-wrapper">';
default_items();
echo '<div class="page-element-lists" id="page-element-lists">';
page_items();
echo '</div>';
clone_to();
echo '</div>';
// Use nonce for verification
wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
}
// Select Add options
function default_items(){
global $custom_meta_fields, $post;
?>
<div class="page-select-element-list-wrapper combobox">
<select id="page-select-element-list">
<option>Select an item</option>
<?php
foreach ($custom_meta_fields as $page_meta_box => $value) {
echo '<option>' .$page_meta_box. '</option>';
}
?>
</select>
</div>
<input type="button" value="Add item" class="page-add-item-button" id="page-add-item-button">
<?php
}
// Elements to clone
function page_items(){
global $custom_meta_fields, $post;
foreach( $custom_meta_fields as $page_meta_box => $value ){
if($page_meta_box == 'Column'){
$size = 'element1-4';
$defaultsize = '1/4';
}
elseif($page_meta_box == 'Divider'){
$size = 'element1-1';
$defaultsize = '1/1';
}
?>
<div id="page-element" class="page-element <?php echo $size; ?>" rel="<?php echo $page_meta_box; ?>">
<div class="page-element-item">
<div class="left-side">
<input type="button" class="increase-size" value="+"/>
<input type="button" class="decrease-size" value="-"/>
<span class="element-description"> <?php echo $page_meta_box; ?> </span>
</div>
<input type="hidden" name="" value="Tab" class="page-option-item-type" id="page-option-item-type">
<input type="hidden" name="" value="element1-4" class="page-option-item-size" id="page-option-item-size">
<div class="right-side">
<span class="element-size-text" id="element-size-text"><?php echo $defaultsize; ?></span>
<div class="change-element-property">
<a title="Edit"><div class="edit-element" id="page-element-edit-box" rel="edit-box"></div></a>
<a title="Delete"><div id="delete-element" class="remove-element"></div></a>
</div>
</div>
</div>
<div class="page-element-edit" id="my_modal">
<?php
if($page_meta_box == 'Column'){
echo 'Title: <input type="text"/>';
echo 'Content: <textarea></textarea>';
}
elseif($page_meta_box == 'Divider'){
echo '<hr>';
}
elseif($page_meta_box == 'Quote'){
echo 'Content: <textarea></textarea>';
}
?>
</div>
</div>
<?php
}
}
// Area for added elements
function clone_to(){
?>
<div class="page-methodology">
<div class="page-selected-elements-wrapper">
<div id="page-selected-elements" name="page-selected-elements" class="page-selected-elements page-no-sidebar ui-sortable">
<!-- Trying to save all data that gets inserted into here from the meta box -->
<!-- Then once saved display the saved data here -->
<?php
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo $value;
?>
</div>
</div>
</div>
<?php
}
function remove_taxonomy_boxes() {
remove_meta_box('categorydiv', 'post', 'side');
}
add_action( 'admin_menu' , 'remove_taxonomy_boxes' );
// Save the Data THIS IS FROM THE EXAMPLE NEED TO FIGURE OUT HOW TO SAVE NEW DATA
function myplugin_save_postdata( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) )
return $post_id;
$nonce = $_POST['myplugin_inner_custom_box_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) )
return $post_id;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
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;
}
/* OK, its safe for us to save the data now. */
// Sanitize user input.
$mydata = ( $_POST['page-selected-elements'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $mydata );
print_r($_POST);
}
add_action( 'save_post', 'myplugin_save_postdata' );
?>
答案 0 :(得分:0)
所以我发现我的meta_key正在添加,但meta_value总是空的。
尝试不同的方法我追加“克隆”textarea内的另一个项目进行测试。克隆部分工作,因为我可以看到它与Firefox一起检查,因为它们被添加,但它们显示在firefox中的textarea里面变灰了。我也没有看到实际显示在前端文本框中的值。
对于带有html内容的update_post_meta,是否需要做一些特别的事情?
我再次尝试使用动态添加的div内容保存元框。
另一个简单的例子:
<div class="wrapper">
<div class="page_selected_elements">
<!--
jQuery is cloning some divs and appends them into here.
<?php
$values = get_post_custom( $post->ID );
$selected = isset( $values['addddData'] ) ? $values['addddData'][0] : '';
echo $selected;
?>
So after clicking my add button I end up with something like this
<div class="added-item"></div>
-->
</div>
</div>
我的保存部分不是整个功能
$cloned = $_POST['page_selected_elements'];
update_post_meta( $post_id, 'addddData', $cloned );
作为测试,我使用phpmyadmin手动更新meta_value,使用jQuery添加div内容,刷新wordpress并显示内容,然后当我更新帖子时,它正在删除meta_data。有人有什么想法吗?
答案 1 :(得分:0)
你需要一个save_post的动作钩子
add_action('save_post',function(){});
然后在回调函数中,您需要查找已发布的数据,验证并保存它。