doufangmu9087 2019-06-17 07:11
浏览 80

如何在WordPress中存储多个输入的多维数组元框中的数据?

In my custom post-type, I am building an multi dimensional array meta-box with multiple inputs, with below code:

<?php
    $services = get_post_meta($post->ID, 'services', true);

    foreach ((array) $services as $service) {
        echo '<div class="inside">
        <div>
            <label>Title</label>
            <input type="text" name="service[][title]" value="' . $service['title'] . '">
        </div>
        <div>
            <label>Type</label>
         <input type="text" name="service[][type]" value="' . $service['type'] . '">
        </div>
        <div>
            <label>Content</label>
            <textarea name="service[][text]">' . $service['text'] . '</textarea>
        </div>';
    }
    exit;
    ?>

And, saving the data with below function:

function service_save_meta_box_data($post_id) {
// verify taxonomies meta box nonce
if (!isset($_POST['service_meta_box_nonce']) || !wp_verify_nonce($_POST['service_meta_box_nonce'], basename(__FILE__))) {
    return;
}

// return if autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return;
}

// Check the user's permissions.
if (!current_user_can('edit_post', $post_id)) {
    return;
}

// store custom fields values
// Feature Title
if (isset($_REQUEST['services'])) {
    update_post_meta($post_id, 'services', sanitize_text_field($_POST['service']));
}
}

add_action('save_post_service', 'service_save_meta_box_data');

However, the data is not being stored and retrieved in the inputs. Where am I doing the mistake?

  • 写回答

1条回答 默认 最新

  • dongling2545 2019-06-17 10:15
    关注

    You can use this code

    Template

    <?php /*
     Template Name: Customform
     */
    
     get_header()
     ?>
     <form id="Customform">
        <div class="inside">
            <div>
                <label>Title</label>
                <input type="text" name="service[][title]" >
            </div>
            <div>
                <label>Type</label>
             <input type="text" name="service[][type]">
            </div>
            <div>
                <label>Content</label>
                <input type="hidden" name="action" value="get_data">
                <textarea name="service[][text]"></textarea>
            </div>
            <div>
    
                <input type="button" id="test" value="send">
            </div>
     </form>
     <script type="text/javascript">
        jQuery(document).ready(function()
        {
    
            jQuery("#test").click(function(){
                console.log("test");    
    
    
                jQuery.ajax({
                         type : "POST",
                         dataType : "json",
                         url : "<?php echo admin_url('admin-ajax.php'); ?>",
                         data : jQuery("#Customform").serialize(),
                         success: function(response) {
    
                               alert("Your vote could not be added");
                               alert(response);
                            }
                    });   
    
            });
        })
     </script>
     <?php get_footer();?>
    

    Functions.php

        function  savemyformdata() {
    
            update_post_meta(34, 'services', $_POST['service']);
    
    }
    add_action( 'wp_ajax_nopriv_get_data', 'get_data' );
    add_action( 'wp_ajax_get_data', 'savemyformdata' );
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法