0

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

?>
4

2 回答 2

0

所以我发现我的 meta_key 被添加了,但是 meta_value 总是空的。

尝试一种不同的方法,我将附加到“克隆”文本区域内的另一个项目以进行测试。克隆部分可以正常工作,因为我可以看到它在添加它们时使用 Firefox 检查,但它们显示在 Firefox 中的文本区域内,显示为灰色。我也看不到前端文本框中实际显示的值。

是否需要对带有 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。有人有想法么?

于 2013-10-20T00:24:06.483 回答
0

你需要一个用于 save_post 的动作钩子

add_action('save_post', function() {});

然后在回调函数内部,您需要查找发布的数据,验证并保存。

于 2013-12-05T17:07:01.573 回答