[ad_1]
I’m creating a widget that’s compatible with Elementor Pro but I’m getting a Fatal Error: Uncaught TypeError: Elementor\Controls_Stack::sanitize_settings(): Argument #1 ($settings) must be of type array, null given. I’m trying to create a select field and the array I’m returning are the options for the select field. I have other select fields that work just fine with a returned array but I can’t figure out why this one doesn’t work. I’ve done a var_dump of the variable to verify it is an array and to see the contents of the array. Everything looks fine. I also did a test by manually creating a simple array to return and that worked. Here is my function:
/**
* Get a list of the custom field names from the selected post type
*/
protected function get_post_fields(){
/***** This code works fine but isn't what I need***************
$post_fields = [
'default' => esc_html__( 'Default', 'OWSM-table-widget' ),
'yes' => esc_html__( 'Yes', 'OWSM-table-widget' ),
'no' => esc_html__( 'No', 'OWSM-table-widget' ),
];
return $post_fields;
***************************************************************/
//Get the selected post type
$my_settings = $this->get_settings_for_display();
$selected_post_type = $my_settings['data_source'];
//Array to store field names
$field_names = array();
//Check to make sure there is a selected post type
if (!$selected_post_type) {
return;
}
//Get the selected post type, loop through and get custom post field names
$wanted_posts = new WP_Query(array('post_type' => $selected_post_type));
if($wanted_posts->have_posts()) :
while($wanted_posts->have_posts()) : $wanted_posts->the_post();
// Get all the custom data
$getPostCustom = get_post_custom();
//Store the names of the custom fields in an array
foreach($getPostCustom as $name=>$value) {
if (strpos($name, "_") !== 0){
$field_names[$name] = esc_html__($name, 'OWSM-table-widget');
}
}
endwhile;
endif;
// Return to the current page's main query
wp_reset_query();
//Return the array of custom fields for the specified post type
return $field_names;
}
Here’s the var_dump of the manually created array $post_fields:
array(3) { [“default”]=> string(7) “Default” [“yes”]=> string(3) “Yes” [“no”]=> string(2) “No” }
And here’s the var_dump of the $field_names array created with the loop:
array(5) { [“city”]=> string(4) “city” [“nights_of_play”]=> string(14) “nights_of_play” [“contact”]=> string(7) “contact” [“email”]=> string(5) “email” [“phone”]=> string(5) “phone” }
Both arrays have the exact same structure but the $field_names array gives me the error. Any suggestions?
[ad_2]