Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    What is the functions.php code you use to add images to your feed?

    For whatever reason, the URL is passed through htmlentities() twice. The real cure involves not doing that. If it is happening consistently, you could pass added content through html_entity_decode() to undo one of them. You should verify it has been double encoded before doing so. Otherwise there’s a risk of undoing a single encoding.

    Thread Starter fscbmwcca

    (@fscbmwcca)

    Here is the code that used to work (and I currently have on there)

    //Add images to RSS for MailChimp in description
    function featuredtoRSS($content) {
       global $post;
       if ( has_post_thumbnail( $post->ID ) ){
       $content = '' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:left; margin:0 0 0 0;' ) ) . '' . $content;
    }
    return $content;
    }
    
    add_filter('the_excerpt_rss', 'featuredtoRSS');
    add_filter('the_content_feed', 'featuredtoRSS');

    My apologies I am not super-versed in php or that technical, so I wouldn’t know how you determined that htmlentities() is twice ??

    • This reply was modified 5 years, 3 months ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    Double application of html entities (or equivalent, end result is the same) is the only explanation for getting the &_amp;amp; (without the ‘_’) in URL query strings. Once is good, twice is bad, as you have discovered. Tracing back to the source of the extra application will be difficult, especially if you are not well versed in PHP.

    It’ll be a lot easier to deal with the symptom instead of the cause, though it’s not an ideal solution. Replace the 3 line if (){...} structure with this:

    if ( has_post_thumbnail( $post->ID ) ){
       $img = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:left; margin:0 0 0 0;' ) );
       // Remove the '_' in '&_amp;amp;' below before using
       if ( false !== strpos( $img, '&_amp;amp;')) {
          $img = html_entity_decode( $img );
       }
       $content = '' . $img . '' . $content;
    }

    Note the // comment about removing the underscore. The actual entity gets rendered in forum posts so it’s difficult to have the proper code display correctly, so I utilized a “fake” entity for appearances sake. The actual 5 char entity needs to be in the actual code though. You can also remove the comment line as well to avoid future confusion.

    If you are not going to wrap the img tag in other HTML you can remove the empty strings in the $content assignment:
    $content = $img . $content;

    BTW, when you post code in these forums, please demarcate with backticks or use the code button. Failure to do so corrupts the code and makes it difficult to test or create modified examples. I fixed the code in your last reply so that I could more easily create my solution ??

    Thread Starter fscbmwcca

    (@fscbmwcca)

    This didn’t work ??

    I don’t understand why you are trying to remove
    _’? only the ‘amp;’ should be removed in the three instances (I bolded them)

    https://i-cdn.embed.ly/1/display/crop?height=300&key=fd92ebbc52fc43fb98f69e50e7893c13&url=https%3A%2F%2Fcdn.motor1.com%2Fimages%2Fmgl%2FYnQo0%2Fs3%2F2019-bmw-z4-m40i.jpg&width=636

    Should be
    https://i-cdn.embed.ly/1/display/crop?height=300&key=fd92ebbc52fc43fb98f69e50e7893c13&url=https%3A%2F%2Fcdn.motor1.com%2Fimages%2Fmgl%2FYnQo0%2Fs3%2F2019-bmw-z4-m40i.jpg&width=636

    PS thanks for the tip on posting code. ??

    I tried the following code on https://sandbox.onlinephpfunctions.com/

    <?php
    $img = "https://i-cdn.embed.ly/1/display/crop?height=300&key=fd92ebbc52fc43fb98f69e50e7893c13&url=https%3A%2F%2Fcdn.motor1.com%2Fimages%2Fmgl%2FYnQo0%2Fs3%2F2019-bmw-z4-m40i.jpg&width=636";
    echo html_entity_decode( $img );
    ?>

    and it returned the correct string url: https://i-cdn.embed.ly/1/display/crop?height=300&key=fd92ebbc52fc43fb98f69e50e7893c13&url=https%3A%2F%2Fcdn.motor1.com%2Fimages%2Fmgl%2FYnQo0%2Fs3%2F2019-bmw-z4-m40i.jpg&width=636

    Moderator bcworkz

    (@bcworkz)

    The strpos() simply identifies double encoded ‘&’ entities. It’s not removing anything specific. That’s left for html_entity_decode(). We don’t want to decode anything that is single encoded. It’s meant to be encoded once and only once. You do need to remove the “_” from the actual matching string code for it to correctly match double encoding. I just placed it there so your browser doesn’t just display an “&” in its place. For functioning code it cannot be there.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘RSS feed featured image’ is closed to new replies.