• Resolved moonstruck

    (@moonstruck)


    Hi, this is a PHP-code question.
    When I search for the best up-to-date code to add featured image in my RSS feed, set to excerpts, (for Feedly and Mailchimp) I see there are some minor differences in the code, but I don′t understand why…

    This seems to be the “original” code from way back.
    Is this still the best code for blog posts with excerpts and featured images? Or should I delete “the_content” and replace with “the_excerpt_rss”? Does this code exclude comments feed?
    (And why float right??):

    <?php
    add_filter( 'the_content', 'featured_image_in_feed' );
    function featured_image_in_feed( $content ) {
        global $post;
        if( is_feed() ) {
            if ( has_post_thumbnail( $post->ID ) ){
                $output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
                $content = $output . $content;
            }
        }
        return $content;
    }
    ?>
    

    But I also found code modified with P, why/when to use P??

    
    add_filter('the_excerpt_rss', 'rss_post_thumbnail');
    add_filter('the_content_feed', 'rss_post_thumbnail');
    function rss_post_thumbnail($content) {
        global $post;
        $content ='';
    	if(has_post_thumbnail($post->ID)) {
    	$content = '<p>' . get_the_post_thumbnail($post->ID , 'full') . '</p>' . get_the_excerpt();
        }
        return $content;
    }
    

    I also found it with DIV, why/when to use DIV??

    
    add_filter('the_excerpt_rss', 'featuredtoRSS');
    add_filter('the_content_feed', 'featuredtoRSS');
    function featuredtoRSS($content) {
        global $post;
        if ( has_post_thumbnail( $post->ID ) ){
            $content = '<div>' . get_the_post_thumbnail( $post->ID, 'large', array( 'style' => 'margin-bottom: 15px;' ) ) . '</div>' . $content;
        }
        return $content;
    }
    

    All three are also different just before

    
    }
      return $content;
    

    Are they all correct?
    Trying to learn…

    Bonus question,
    is big images in the feed alright, or is there a max size for Feedly and Mailchimp? Thumbnail must be to small these days, but is large okey? Or is maybe 600 max??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The content of a post contains HTML, which is output in the feed within CDATA wrappers. The feed itself is XML.

    You might have to search again, for up-to-date information.
    Adding images to RSS feeds was started in the early 2000s. The <enclosure> mechanism was used.

    RSS enclosures are a way of attaching multimedia content to RSS feeds by providing the URL of a file associated with an entry, such as an MP3 file to a music recommendation or a photo to a diary entry. Unlike e-mail attachments, enclosures are merely hyperlinks to files. The actual file data is not embedded into the feed (unless a data URL is used). Support and implementation among aggregators varies

    If you are simply adding a link to the HTML content, it shouldn’t matter whether it is in a <p> tag or a <div> tag, or no tag at all.
    In WordPress, excerpts can be manually written, and contain HTML. Excerpts that are generated from content have the HTML tags stripped out, so the link would have to be added after the stripping happens.

    By 2009, it was expanded to Media RSS Spec, which handles more types of files.

    Media RSS is a new RSS module that supplements the <enclosure> capabilities of RSS 2.0. RSS enclosures are already being used to syndicate audio files and images. Media RSS extends enclosures to handle other media types, such as short films or TV, as well as provide additional metadata with the media.

    I suggest you use a plugin for adding the images.

    Thread Starter moonstruck

    (@moonstruck)

    Thank you for an interesting answer!
    Hmm… I have googled a lot and everybody so far says it so easy, just add this code (with small variations). I know how to use PHP snippet plugin, so I was hoping to not have to add an extra plugin.

    Just wanted to be sure I was using an up to date code, modified for Gutenberg and a responsive world 2021.

    But as you say, this goes many years back and I find it hard to know if it is still relevant today, or if things have changed but people are lazy or non tech (like me) and still repeat the same old code…

    Right, the snippets you mentioned weren’t using <enclosure>. They just put the link in with the other HTML.
    I suggested a plugin because the author would have researched it, tested it, and maintains it, so you don’t have to know all that stuff. Or you can find a plugin and just read the code so you know the answer to your question.

    When you add code to WordPress, you have to add it somewhere. A plugin is the best place to add code. When you use a snippet plugin, you are storing code in the database, which is not as good an idea as storing it in code files in the plugin folder. There are protections in place for the code files, but rogue plugins or hacks can compromise your database.

    Thread Starter moonstruck

    (@moonstruck)

    Thank you, good information about snippets and database! And I think you have convinced me to find a light plugin instead…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Why P or DIV in RSS/feed code for featured image?’ is closed to new replies.