Viewing 2 replies - 1 through 2 (of 2 total)
  • This error is occurring because the esc_url() function is receiving a WP_Error object instead of a string. This can happen when there is an issue retrieving a URL, and instead of returning a string, WordPress returns a WP_Error object to signal a problem.

    Here’s a potential solution to handle this error in your theme’s functions.php file on line 159, where the esc_url() function is being called:

    1. Check for WP_Error: Wrap the URL in a conditional check to ensure that it’s a valid string before passing it to esc_url(). This avoids passing WP_Error objects into functions expecting strings.Update line 159 in your functions.php file to something like this:
      $url = get_some_url_function(); // Replace with the actual function or variable that gets the URL.
      if (!is_wp_error($url)) {
      $url = esc_url($url);
      } else {
      $url = ''; // Or handle the error as needed, like setting a default URL.
      }
    2. Debug the URL Source: Find the function or code returning this URL and verify why it’s returning a WP_Error instead of a URL string. This can happen if the URL retrieval logic encounters an error (e.g., the ACF field is missing or misconfigured).
    Thread Starter InventiveWebDesign

    (@inventivewebdesign)

    Thank you, it looks like I am already doing this but it may have gotten me closer. I have a few functions in there that are used to create shortcodes so I can list the field data with their links in the correct place on the product page.

    The code for [acf_accessories] (which doesn’t always work) looks like this:

    function acf_accessory() {
        //if ( is_product() ) {
            //global $post;
            $prod_accessories = get_field('accessories' );
    
            if ( ! empty( $prod_accessories ) && ! is_wp_error( $prod_accessories ) ) {
                $accessory_links = array();
    
                foreach ( (array) $prod_accessories as $prod_accessory ) { // LINE 158
                    $accessory_links[] = '<a href="' . esc_url(get_term_link(  $prod_accessory )) .  // LINE 159
                    '">' . esc_html( $prod_accessory->name ) . '</a>';
                }
    
                return implode( ', ', $accessory_links );
            }
        //}
    
        return '';
    }
    
    add_shortcode( 'acf_accessories', 'acf_accessory' );

    The code for [acf_prod_mod_nums] (which works) looks like this:

        function acf_prod_mod_num() {
            //if ( is_product() ) {
                //global $post;
                $prod_mod_nums = get_field('model_numbers' );
    
                if ( ! empty( $prod_mod_nums ) && ! is_wp_error( $prod_mod_nums ) ) {
                    $mod_num_links = array();
    
                    foreach ( $prod_mod_nums as $prod_mod_num ) {  // LINE 205
                        $mod_num_links[] = '<a href="' . esc_url(get_term_link( $prod_mod_num )) .  // LINE 206
                        '">' . esc_html( $prod_mod_num->name ) . '</a>';
                    }
    
                    return implode( ', ', $mod_num_links );
                }
            //}
    
            return '';
        }
    
        add_shortcode( 'acf_prod_mod_nums', 'acf_prod_mod_num' );

    I have commented inline where the line numbers in question are in my code. You can see the only difference (other than variables) for each code is that in Line 158 in the Accessories code it has (array) after the foreach (. I thought, if these functions are essentially doing the same thing shouldn’t removing (array) work? Well a lot of the errors disappeared but now the Accessory doesn’t show and I get this error:

    Warning: foreach() argument must be of type array|object, string given in?/home/customer/www/staging2.mcdanielmetals.com/public_html/wp-content/themes/astra-child/functions.php?on line?158

    I am unsure how this code is working and why one needs to be different than the other but maybe someone who know SCF (acf) better could help me out?

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.