如何删除在 Discord 预览中可见的作者标签?

问题描述 投票:0回答:4

将帖子分享到 Discord 时,Discord 生成的预览会显示作者姓名和 URL。我们删除了有关作者的所有信息,但这并没有阻止作者标签的显示。

Image here

wordpress woocommerce chat discord
4个回答
16
投票

这是通过 oEmbed 完成的。在你的functions.php文件中添加以下代码

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;
}

**disorc 可能已将响应存储在缓存中,因此创建新的帖子或页面并测试 **


3
投票

@hrak 的想法是正确的,但对于我们这些不习惯处理 PHP 的人来说,他的答案缺乏上下文。

我最终做的是检查

/wp-includes/default-filters.php
中是否已经有'oembed_response_data'的过滤器。我的看起来像这样:

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

如果由于某种原因,上一行尚不存在,则将其添加到指定文件中。

之后,我在 wp-includes/embed.php 中检查了

get_oembed_response_data_rich
函数,它看起来像这样:

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;
}

我刚刚添加了 @hrak 在他的答案中引入的两行代码,以在返回之前从

$data
中删除作者标签(名称和 URL):

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;
}

与之前一样,如果

get_oembed_response_data_rich
函数尚不存在,请添加它。大约 5-10 分钟后,Discord 嵌入链接停止显示作者标签。

来源:


0
投票

我通过清空 posts_by 函数来完成此操作,如本文所示,在“使用代码删除作者姓名”部分中

https://wpdatatables.com/how-to-hide-the-author-in-wordpress/

基本上找到

posted_by
函数并清空它

function twentynineteen_posted_by() {

}

endif;

0
投票

我想重申 hrak 的答案是正确的:

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;
}

但是您不必触及任何后端文件,甚至是functions.php。安装代码片段插件(默认情况下您应该在正在运行的任何 WordPress 网站上添加该插件)并将代码作为片段添加到其中。这可以确保您不必担心以后对 WordPress 或您的主题进行更新,从而破坏您的代码,并且还确保即使您没有对安装后端的 FTP 访问权限,您也可以做到这一点。当您通过代码片段执行此操作时,您可以选择在整个站点运行它,或者仅在某些帖子类型上运行它,因此这是一个明显的优势。我一直在绞尽脑汁地试图找到这个解决方案,所以我对此非常满意。

再次强调,代码全部归功于 hrak。我只是想为那些对挖掘后端文件持怀疑态度或无权访问它们的人添加一种更简单的部署方法。

© www.soinside.com 2019 - 2024. All rights reserved.