0

I am using a plugin called The Events Calendar. I don't always set the featured image for each individual event. Each event has an organizer ID (tribe_get_organizer_id).

I want to add a filter that looks if an event from a custom post type (tribe_events) has a featured image attached. If not, than the filter should call up the organizer and assign the organizer's featured image as the event's featured image.

I'm not even sure with what hook to start. Something like this?

    function set_featured_event_image () {
    if ( get_post_type() == 'tribe_events') {
    if ( has_post_thumbnail() ) {
        the_post_thumbnail();
        }
        else {
GRAB THE IMAGE FROM THE RELATED ORGANIZER
        }
    }
}

    add_filter( '**WHAT HOOK?**', 'set_featured_event_image');

I might be terribly off with any of this. Any help in the right direction is appreciated.

0

I took a quick look at the Modern Tribe's source code, particularly the single-event.php view template. MT is using an internal function, tribe_event_featured_image(), which includes a filter: tribe_event_featured_image.

You could use this filter to see if MT has already found an image, and, if not, look up your own and return its value.

add_filter( 'tribe_event_featured_image', 'wpse174412', 10, 4 );
function wpse174412( $featured_image, $post_id, $size, $image_src ) {
  if ( ! $featured_image ) {
    // get image from related organizer
  }
  return $featured_image;
}
  • Thank you very much for your quick response. I think where I got stuck specifically is how to find the related organizer's featured image. I know you have to probably find the ID (tribe_get_organizer_id) and then find the image from there, but I'm stuck on how to do that. – Marc van Bree Jan 9 '15 at 20:27
  • I just installed the plugin on a test site and don't see an option to assign a featured image to organizers – karpstrucking Jan 9 '15 at 20:29
  • The event is one custom post type, the organizer is another custom post type. In each post you can set the featured image. Wouldn't you be able to find the thumbnail ID from the post ID of the organizer post? So basically, for an event without a featured image set, you'd be looking to see what the organizer post ID's thumbnail ID is and use that? I don't know if I'm still making sense. – Marc van Bree Jan 9 '15 at 20:42
  • what you're saying makes sense. but I'm looking at a fresh install of the plugin and don't see an option to add a featured image to an Organizer. – karpstrucking Jan 9 '15 at 21:21
  • The organizer view comes with the PRO account. But it uses the same tribe_event_featured_image function. In your function, would I simply have to say that $post_id = the tribe organizer id? Like this? (This doesn't seem to work though): add_filter( "tribe_event_featured_image", "wpse174412", 10, 4 ); function wpse174412( $featured_image, $post_id, $size, $image_src ) { if ( ! $featured_image ) { $post_id = tribe_get_organizer_id ( $postId ); } return $featured_image; } – Marc van Bree Jan 9 '15 at 22:45
0

I found a solution, but it seems rather cumbersome. I would think a cleaner solution would be possible using WP’s native thumbnail functions, but I can’t figure it out.

When I use the “tribe_event_featured_image” function, it replaces the images well wherever that function is loaded in the templates. However, I also use for example the Genesis Framework and their Responsive Slider plugin. The solution with the tribe function didn’t populate the slider with the featured image, as that was called with the “genesis_image” function (which uses and filters WP’s thumbnail function).

That’s why I think a function that deals with the native capabilities would be better too. But here’s the code that made it possible for me to display the organizer’s featured image as the event’s featured image if no featured image was set.

    /** Define a default post thumbnail Genesis */
function default_event_image_fallback($output, $args) {
    global $post;
    if( $output || $args['size'] == 'full' && get_post_type() == 'tribe-events' )
        return $output;

    $post_id = tribe_get_organizer_id();
    $image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
    $thumbnail = $image_src[0];

    switch($args['format']) {

        case 'html' :
            return '<img src="'.$thumbnail.'" class="attachment-'. $args['size'] .'" alt="'. get_the_title($post_id) .'" />';
            break;
        case 'url' :
            return $thumbnail;
            break;
       default :
           return $output;
            break;
    }
}
add_filter('genesis_get_image', 'default_event_image_fallback', 10, 2);

/** Define a default post thumbnail Events Calendar */
function set_default_event_featured_img( $featured_image, $post_id, $size, $image_src ) {
    if ( ! $featured_image ) {
        $post_id = tribe_get_organizer_id();
        $image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), $size );
        $featured_image = '';

        //if link is not specifically excluded, then include <a>
        if ( ! empty( $image_src ) && $link ) {
            $featured_image .= '<div class="tribe-events-event-image"><a href="' . tribe_get_event_link() . '" title="' . get_the_title( $post_id ) . '"><img src="' . $image_src[0] . '" title="' . get_the_title( $post_id ) . '" /></a></div>';
        } elseif ( ! empty( $image_src ) ) {
            $featured_image .= '<div class="tribe-events-event-image"><img src="' . $image_src[0] . '" title="' . get_the_title( $post_id ) . '" /></div>';
        }

        return apply_filters( 'tribe_event_featured_image', $featured_image, $post_id, $size, $image_src );
    } 
    else {
        return $featured_image;
    }
}
add_filter( 'tribe_event_featured_image', 'set_default_event_featured_img', 10, 4 );

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.