1

I've been working with a child theme and functions.php file to add an Image to the posts that have a specific Tag (Videos in the code). I was trying to create the function but I have some issues, one is that in the site, when I load a Post with that tag, it crashes, looks like it stays in a loop, and after that I have to manually delete all the repeated images (I just want one) from the posts in the database (PhpMyAdmin)

Could you help me to solve the issue and have working code?

Important Update: I am using a RSS feed, and I require that Image to be in the feed too (I mean in the posts table database)

Here is my code in functions.php:

/**
 * Prepend image to post if it has a specific tag
 *
 * @param String $content - WP Post Content for display
 *
 * @return String $content - WP Post Content for display
*/ 
function theme_videos_append_image( $content ) {

    global $post;

    // It would be easier if you got this URL from Attachment ID
    $upload_dir_arr     = wp_upload_dir();  // Get upload directory array ( https://developer.wordpress.org/reference/functions/wp_upload_dir/#user-contributed-notes )
    $static_image_url   = $upload_dir_arr['baseurl'] . '/2017/11/upliftingscroll.jpg';

    // Ensure we are viewing a Post, and it has the Videos tag
    if( has_tag( 'Videos', $post->ID ) ) {

        $has_image = get_post_meta( $post->ID, '_video_image_added', true );    // Check if our postmeta exists

        // If is does not have our postmeta - add it
        if( empty( $has_image ) ) {

            $image      = sprintf( '<p><img src="%1$s" alt="" class="img-responsive" /></p>', $static_image_url );  // Create image
            $content    = $image . $content;    // prepend image

            // Update post so we don't need to add the image again
            $success = wp_update_post( array(
                'ID'            => $post->ID,
                'post_content'  => $image . $post->post_content,
            ) );

            // If the post updated, create postmeta letting us know later it has the image
            if( false !== $success && !is_wp_error( $success ) ) {
                update_post_meta( $post->ID, '_video_image_added', true );
            }

        }

    }

    return $content;

}
add_filter( 'the_content', 'theme_videos_append_image' );
  • Why do you update the post content? The function runs and adds the image so no need for updating the post? – GDY Nov 7 '17 at 14:48
  • Sorry that i coudn't help on that topic. Would you mind changing the username in your comment (removing my full name)? – GDY Jan 21 at 11:45
1

Heres a more simple version of your function:

<?php

    add_filter( 'the_content', 'theme_videos_append_image' );

    function theme_videos_append_image( $content ) {

        global $post;

        $upload_dir_arr     = wp_upload_dir();
        $static_image_url   = $upload_dir_arr['baseurl'] . '/2017/11/upliftingscroll.jpg';
        $tag = '<p><img src="' . $static_image_url . '" alt="" class="img-responsive" /></p>';

        return has_tag( 'Videos', $post->ID ) ? $tag . $content : $content;

    }

?>

Check if that is the most elegant solution for getting the image URL. Seems a bit odd to me.

The loop you've mentioned probably exists because in wp_update_post the the_content filter also may be applied. So if you want to stick to your solution try adding the post meta first and then updating the post content ... but i don't think this is necessary.

  • This way is saved in the database? – ricardorios Nov 7 '17 at 15:01
  • No it isn't. But i don't see why this is necessary? – GDY Nov 7 '17 at 15:05
  • Because I am using a RSS feed, and I require that Image to be in the feed too – ricardorios Nov 7 '17 at 15:07
  • Is the the_content filter not applied in this feed? – GDY Nov 7 '17 at 15:08
  • No, is not, so that is because I need the image be placed inside the database posts – ricardorios Nov 7 '17 at 15:11
0

Extending @GDY solution. You have to also hook to the_excerpt_rss filter by doing:

add_filter('the_excerpt_rss', 'theme_videos_append_image');

add_filter( 'the_content', 'theme_videos_append_image' );

function theme_videos_append_image( $content ) {

        global $post;

        $upload_dir_arr     = wp_upload_dir();
        $static_image_url   = $upload_dir_arr['baseurl'] . '/2017/11/upliftingscroll.jpg';
        $tag = '<p><img src="' . $static_image_url . '" alt="" class="img-responsive" /></p>';

        return has_tag( 'Videos', $post->ID ) ? $tag . $content : $content;

}
  • would you mind removing my full name. thanks in advance – GDY Jan 20 at 15:19
  • @GDY I have update it. – Skatox Jan 20 at 17:09
  • I appreciate that very much. Thank you. – GDY Jan 21 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.