这是我要保存的元框代码。我有其他一切按我想要的方式工作,但我不知道如何保存插入到函数 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' );
?>