How to remove Author Tag from being visible in Discord's previews?

谁说胖子不能爱 提交于 2020-01-24 01:37:10

问题


When sharing a post to Discord, the preview Discord generates shows the author name and URL. We removed all information about the author but it didn't stop the author tag from showing.


回答1:


add_filter( 'oembed_response_data', 'disable_embeds_filter_oembed_response_data_' );
function disable_embeds_filter_oembed_response_data_( $data ) {
    unset($data['author_url']);
    unset($data['author_name']);
    return $data;
}



回答2:


@hrak has the right idea but his answer lacks context for those of us not used to dealing with PHP.

What I ended up doing was check if there was already a filter for 'oembed_response_data' in /wp-includes/default-filters.php. Mine looked like this:

add_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 );

Add the previous line to the specified file if, for whatever reason, it isn't there already.

Afterward, I checked in wp-includes/embed.php for the get_oembed_response_data_rich function, which looked like this:

function get_oembed_response_data_rich( $data, $post, $width, $height ) {
    $data['width']  = absint( $width );
    $data['height'] = absint( $height );
    $data['type']   = 'rich';
    $data['html']   = get_post_embed_html( $width, $height, $post );

    // Add post thumbnail to response if available.
    $thumbnail_id = false;

    if ( has_post_thumbnail( $post->ID ) ) {
        $thumbnail_id = get_post_thumbnail_id( $post->ID );
    }

    if ( 'attachment' === get_post_type( $post ) ) {
        if ( wp_attachment_is_image( $post ) ) {
            $thumbnail_id = $post->ID;
        } elseif ( wp_attachment_is( 'video', $post ) ) {
            $thumbnail_id = get_post_thumbnail_id( $post );
            $data['type'] = 'video';
        }
    }

    if ( $thumbnail_id ) {
        list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) );
        $data['thumbnail_url']                                      = $thumbnail_url;
        $data['thumbnail_width']                                    = $thumbnail_width;
        $data['thumbnail_height']                                   = $thumbnail_height;
    }

    return $data;
}

I just added the two lines of code that @hrak introduced in his answer to remove the author tag (name and URL) from $data before it was returned:

function get_oembed_response_data_rich( $data, $post, $width, $height ) {

    (...)

    if ( $thumbnail_id ) {
        list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = wp_get_attachment_image_src( $thumbnail_id, array( $width, 99999 ) );
        $data['thumbnail_url']                                      = $thumbnail_url;
        $data['thumbnail_width']                                    = $thumbnail_width;
        $data['thumbnail_height']                                   = $thumbnail_height;
    }

    unset($data['author_url']);
    unset($data['author_name']);

    return $data;
}

As before, add the get_oembed_response_data_rich function if it does not already exist. After about 5-10 minutes, Discord link embeds stopped showing the author tag.

Source:

  • http://hookr.io/filters/oembed_response_data/
  • https://developer.wordpress.org/reference/functions/get_oembed_response_data_rich/


来源:https://stackoverflow.com/questions/55018396/how-to-remove-author-tag-from-being-visible-in-discords-previews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!