1

我正在开发一个部分排序器插件,为此我使用 Redux 设置了一个只有排序器字段的简单选项页面。问题是,我不能以编程方式放置启用的值,我尝试了几种方法,但似乎没有任何效果。

我正在使用 AJAX 获取主页中所有部分标签的 id,然后,我将部分的 id 发送到选项文件中的函数(Redux 选项文件,sample-config.php,或者在我的情况下为 sort-sections-options .php ,像这样:

这是我的 JS/JQ/AJAX 代码:

jQuery(document).ready(function($){
var sections_ids = {action: 'save_sections_ids'};
$.get(window.location.protocol + "//" + window.location.host + "/futbol-americano", function(data) {
var sections_elements = $(data).find("section");
sections_elements.each(function(){
var section_id = $(this).attr("id");

sections_ids[section_id] = section_id; 
console.log(section_id);

}); 
pass_ids_to_server(sections_ids);

});

function pass_ids_to_server(sections_ids){
    $.ajax({
        url : sorting_sections.ajax_url,
        type : 'post',
        data : sections_ids,
        success : function( response ) {             
            alert(response);
        }
    }); 
} 
});

这是从 ajax 接收数据的函数:

这是目前的形式,但我尝试循环 $_POST 变量并返回一个数组以放置在 'enabled' => $array 和其他几种方式中,所以我虽然异步调用可能正在执行该字段加载在函数之前并决定将数据存储在用户元中。

add_action( 'wp_ajax_save_sections_ids', 'save_sections_ids_sorting_sections' );

function save_sections_ids_sorting_sections() {

$user_id = get_current_user_id();
update_user_meta($user_id, "set-sections", $_POST);

}

AJAX 调用有效,接收数据的函数也有效。数据存储在数据库的 user_meta 表中。但是当我这样做时:

$user_id = get_current_user_id();
$get_sections_array = get_user_meta($user_id, "set-sections", true);
//Returns false but if var_dumped inside the save_sections_ids_sorting_sections() function, it shows the array correctly in an alert box (from succes on ajax call).

$sorter_field = array(
'section_id' => 'basic',
'id' => 'opt-homepage-layout-3',
'type' => 'sorter',
'title' => 'Este es el titulo',
'desc' => 'this is the description',
'compiler' => 'true',
'options' => array(
'disabled' => array(
),
'enabled' => $get_sections_array;
));

Redux::setField($opt_name, $sorter_field );
//The Redux::setField($opt_name, $sorter_field ); works if I define $sorter_field manually before the instance.

我在选项页面中收到以下错误消息:

警告:array_merge(): Argument #2 is not an array in C:\xampp\htdocs\futbol-americano\wp-content\plugins\sorting-sections\ReduxFramework\ReduxCore\inc\fields\sorter\field_sorter.php 在线81

警告:在第 103 行为 C:\xampp\htdocs\futbol-americano\wp-content\plugins\sorting-sections\ReduxFramework\ReduxCore\inc\fields\sorter\field_sorter.php 中的 foreach() 提供的参数无效

我希望有人可以帮助解决这个问题。

提前致谢。

4

0 回答 0