是否可以将数组或类保存到$ _POST?

时间:2016-03-02 08:41:49

标签: php wordpress post

我有一个meta box插件,我正在使用wordpress。我有一个按钮,您可以使用该按钮添加输入,可以是文本区域或文本输入。当用户更新他们的帖子时,文本框和输入中的任何内容都会保存到post meta,但是我想循环输入并为每个帖子添加主观数据。

例如,目前我只是将所有输入的值保存到我的'BBPlugin-resources'键中,一个接一个地保存,如下所示:

 'a:2:{
        i:0;s:6:"some words";
        i:1;s:14:"some more words";
    }'

我想我最接近的事情是json,其中存在以下类型的脚本,但我不知道是否可以使用post meta。无论如何,我希望能够得到这样的东西:

 'a:2:{
        "title": "a metabox full of inputs";
        "input boxes"{
            "input 1"{
                "text": "some words",
                "type:"textarea"
            };
            "input 2"{
                "text": "some more words",
                "type:"input"
            };
        };
    }'

这样的事情可能吗?

1 个答案:

答案 0 :(得分:2)

从此

创建数组

我想您在$_POST['input_type']

中的$_POST['input_text']实际输入中获得了输入类型
$input_data = array(
                    'title'         => "a metabox full of inputs",
                    'input_boxes'   => array(
                        'text'  => $_POST['input_text'],
                        'type'  => $_POST['input_type'],
                        ),
                );

//for getting multiple entries under input_boxes run a foreach loop of all text boxes and text area and get them in single array
// for eg : $inpur_arr and put it in the key 'input_boxes' =>  $inpur_arr
/* else 'input_boxes'   => array(
                        'text'  => $_POST['input_text'],
                        'type'  => $_POST['input_type'],
                        ),*/



//will save the json encoded data
update_post_meta($post_ID, 'input_json', json_encode($input_data));

//wil save the serialized data as shown in your example above
update_post_meta($post_ID, 'input_json', $input_data);