F2P Filter: Fix Encoding Conversion Error when "mbstring" or "iconv" are Already Present

This filter is to be applied if you see an encoding conversion error in the error log, but mbstring or iconv is already present. The error text will be as follows:

The data could not be converted to UTF-8. You MUST have either the iconv or mbstring extension installed.

The underlying SimplePie library used for parsing the feed XML uses a class called <a href="https://github.com/simplepie/simplepie/blob/master/idn/idna_convert.class.php">idna_convert</a>, if it is available, to standardize the request URL according to the IDNA standard. If your environment includes this class, the request URL will be appended with “?” (question mark), which in the case of a URL with an empty query is redundant, but allowed. If the feed’s server issues a redirect as a result, the returned content will be empty, despite SimplePie’s ability to follow redirects. Your available encoding conversion function will run, and will correctly return an empty string, which due to a bug in SimplePie will incorrectly be interpreted by it as failure to convert, and will result in that completely irrelevant error message.

To counter this, please use the below filter, which will remove the redundant question mark from the URL.

// Fix for encoding conversion error
if ( !function_exists('my_wprss_before_curl_exec') ) {
    function my_wprss_before_curl_exec( $fp, $url ) {
        $expression = '(https?://[^\ \?]*)(\?\#?)$';
        $url = curl_getinfo( $fp, CURLINFO_EFFECTIVE_URL );
        $orig_url = $url;
        $url = preg_replace( sprintf( '`%1$s`', $expression ), '$1', $url );
        if ( $url !== $orig_url ) {
            curl_setopt( $fp, CURLOPT_URL, $url );
            wprss_log_obj( sprintf( 'Request URL altered by %1$s', __FUNCTION__ ), sprintf( '%1$s -> %2$s', $orig_url, $url ), null, WPRSS_LOG_LEVEL_SYSTEM );
        }
    }
}
add_action( 'wprss_before_curl_exec', 'my_wprss_before_curl_exec', 10, 2 );

How do I add this to my site?

Follow the step-by-step instructions below to add this filter to your WordPress site.

  1. Copy the code you need from above.
  2. Go to your WordPress site's dashboard.
  3. Go to Plugins > Add New.
  4. Search for Code Snippets, then install and activate the plugin.
  5. Once installed and activated, go to Snippets in your dashboard menu.
  6. Click on Add New.
  7. Add a Title, which could be the title of this article.
  8. Paste the code you copied in step 1 to the Code section.
  9. Add a Description or Tags if you wish to do so. It is not required.
  10. Click on Save Changes and Activate to save the filter and activate it.
    1. Or click on Save Changes to save the filter and activate it later.
  11. Your action or filter is now stored and active on your site.

Still need help? Contact Us Contact Us