Making WordPress.org

Ticket #1526: default-fallback-image.2.diff

File default-fallback-image.2.diff, 2.0 KB (added by jeherve, 9 years ago)

Open Graph: add default image fallback when there is no image in the post, proper diff

  • wordcamp.org/public_html/wp-content/mu-plugins/jetpack-tweaks.php

    diff --git a/wordcamp.org/public_html/wp-content/mu-plugins/jetpack-tweaks.php b/wordcamp.org/public_html/wp-content/mu-plugins/jetpack-tweaks.php
    index fb96ac6..7ada389 100644
    a b function default_og_image() { 
    1212}
    1313add_filter( 'jetpack_open_graph_image_default', __NAMESPACE__ . '\default_og_image' );
    1414
     15/**
     16 * Open Graph Default Image for single posts, when no other image can be found in the post.
     17 *
     18 * 4 levels of fallback:
     19 * - Site Logo -- http://jetpack.me/support/site-logo/
     20 * - Site Icon -- https://make.wordpress.org/core/2015/07/27/site-icon/
     21 * - Header Image -- https://codex.wordpress.org/Custom_Headers
     22 * - Global default OG image -- See `jetpack_open_graph_image_default` filter above.
     23 */
     24function default_single_og_image( $media, $post_id, $args ) {
     25        if ( $media ) {
     26                return $media;
     27        } else {
     28
     29                // First fallback, Site Logo, if the theme supports it and if the site owner uploaded a logo
     30                if ( function_exists( 'jetpack_has_site_logo' ) && jetpack_has_site_logo() ) {
     31                        $img_src = jetpack_get_site_logo( 'url' );
     32                }
     33
     34                // Second fallback, Site Icon
     35                if ( empty( $img_src ) && ( function_exists( 'has_site_icon') && has_site_icon() ) ) {
     36                        $img_src = get_site_icon_url( null, '512' );
     37                }
     38
     39                // Third fallback, Header image
     40                if ( empty( $img_src ) && has_header_image() ) {
     41                        $img_src = get_header_image();
     42                }
     43
     44                // Fourth fallback, default image
     45                if ( empty( $img_src ) ) {
     46                        $img_src = apply_filters( 'jetpack_open_graph_image_default', 'https://s.w.org/images/backgrounds/wordpress-bg-medblue.png' );
     47                }
     48
     49                return array( array(
     50                        'type'  => 'image',
     51                        'from'  => 'custom_fallback',
     52                        'src'   => esc_url( $img_src ),
     53                        'href'  => get_permalink( $post_id ),
     54                ) );
     55        }
     56}
     57add_filter( 'jetpack_images_get_images', 'default_single_og_image', 10, 3 );
     58
     59
    1560/*
    1661 * Add Twitter Card type.
    1762 *