Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 186117
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T17:00:35+00:00 2022-06-10T17:00:35+00:00

php – Why does my function work fine in some places of Elementor Pro but gives a TypeError in other places?

  • 0

[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]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 325 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 295 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 292 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.