Search code examples
wordpressnginxhttpsmixed-content

Wordpress Mixed content requested an insecure XMLHttpRequest endpoint


OK I checked all other similar answers, but thanks to people that downvote for no reason there is no actual response.

I am using Wordpress and I have a Mixed Content with a website https://oujdaportail.net/

It is generating this error:

Mixed Content: The page at 'https://oujdaportail.net/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://oujdaportail.net/'. This request has been blocked; the content must be served over HTTPS.

Chrome debug console has been completely useless!! It detects the error at the first line of source code where there is nothing to look up.

I am not sure how I should resolve this... I need help to capture the source of this issue.


Solution

  • Finally! I would be someone's hero...

    After hours of struggle, it turned out that wp_head and wp_footer where responsible for generating unknown HTTP requests. And to fix it all I had to do is create a custom wp_head and wp_footer function just like this:

    /**
     * Wrapper for wp_head() which manages SSL
     *
     * @uses    wp_head()
     * @param   bool    $ssl
     * @return  void
     */
    function custom_wp_head() {
      // Capture wp_head output with buffering
      ob_start();
      wp_head();
      $wp_head = ob_get_contents();
      ob_end_clean();
    
      // Replace plain protocols
      $wp_head = preg_replace( '/(["\'])http:\/\//', '\1https://', $wp_head );
    
      // Output
      echo $wp_head;
    }
    
    function custom_wp_footer() {
      // Capture wp_head output with buffering
      ob_start();
      wp_footer();
      $wp_footer = ob_get_contents();
      ob_end_clean();
    
      // Replace plain protocols
      $wp_footer = preg_replace( '/(["\'])http:\/\//', '\1https://', $wp_footer );
    
      // Output
      echo $wp_footer;
    }