dimanche 18 octobre 2015

Set Redux sorter field option values programatically

I'm working on a section sorter plugin, for that I used Redux to set a simple options page with only the sorter field. The thing is, I can't put the enabled values programatically, I have tried several ways and nothing seems to work.

I am using AJAX to get the ids of all section tags in homepage, then, I send the sections' ids to a function in the options file (Redux options file, sample-config.php, or in my case sorting-sections-options.php , like this:

This is my JS/JQ/AJAX code:

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);
        }
    }); 
} 
});

This is the function that receives the data from ajax:

This is the form is has currently, but I have tried looping the $_POST variable and returning an array to place inside the 'enabled' => $array and several other ways, so I though the asinchronous call could be doing that the field load before the function and decided to store the data in user meta.

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);

}

The AJAX call works, and the function that receives the data works as well. The data stores in the user_meta table in database. But when I do this:

$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.

I get the following error message in the options page:

Warning: 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 on line 81

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\futbol-americano\wp-content\plugins\sorting-sections\ReduxFramework\ReduxCore\inc\fields\sorter\field_sorter.php on line 103

I hope someone can help with this.

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire