WP Discourse - 模板自定义

使用场景:我的 WordPress 帖子中包含 YouTube 链接。我希望在发布 WordPress 帖子后,自动将对应的 YouTube 视频显示在 Discourse 帖子中。

问题:似乎无法正常工作,因为同步后,Discourse 帖子的 HTML 为:

<p>https://www.youtube.com/watch?v=alJEZwwtQ3U</p>

只有当我移除 <p></p> 标签时,YouTube 视频才能成功显示。

那么我该如何移除 <p></p> 标签? 我不希望手动移除这些标签,因为我可能有成千上万篇帖子。

不确定是否可以直接在 functions.php 文件中移除 <p></p> 标签。

我按照您的自定义教程操作。这是 functions.php 中的代码:

// 将已发布到 Discourse 的帖子的特色图片添加进去。
function my_namespace_publish_format( $input ) {
    ob_start();
    ?>
    {excerpt} **<-----我可以在这里移除 p 标签吗?**
    <?php
    $output = ob_get_clean();
    
    // 注意:原始函数中的 apply_filters() 调用已被移除。
    return $output; 
}

add_filter( 'discourse_publish_format_html', 'my_namespace_publish_format' );

根据您发布视频的方式,您可能可以使用类似下面的代码:

add_filter( 'discourse_publish_format_html', 'my_namespace_publish_format', 10, 2 );
function my_namespace_publish_format( $input, $post_id ) {
	$post_content = apply_filters( 'the_content', get_post( $post_id )->post_content );
	$videos = get_media_embedded_in_content( $post_content );
	$video_string = '';
	foreach( $videos as $video ) {
	    $video_string .= $video . '<br>';
    }
    ob_start();

    echo '<small>Originally published at {blogurl}</small><br><br>';
    echo $video_string;
    echo '{excerpt}';
    $output = ob_get_clean();

    return $output;
}

如果您的视频是嵌入在 WordPress 站点中的,这段代码应该可以工作。可能需要一些尝试和调整才能获得正确的格式。我遇到的主要问题是,如果 Youtube iframe 标签前面有前导空格,Discourse 会将其解释为代码。

如果这种方法在您的站点上不起作用,请告诉我。

我希望实现的是,当用户在 Discourse 上点击“显示完整帖子”按钮以展开 WordPress 帖子时,能够显示 Youtube 视频。不过,我还不确定这是否可行。

嗨,Simon

感谢你的回复。非常感激你的帮助。

但似乎不起作用。

输出结果是:

<small>Originally published at http://staging.a1.sg/matrix/</small><br><br><iframe title="GCE O-Level 
A-Maths: Matrix &amp; its Inverse" width="525" height="394" 
src="https://www.youtube.com/embed/alJEZwwtQ3U?feature=oembed" frameborder="0" 
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>. 
</iframe><br><p><iframe title="GCE O-Level A-Maths: Matrix &amp; its Inverse" width="525" 
height="394" src="https://www.youtube.com/embed/alJEZwwtQ3U?feature=oembed" frameborder="0" 
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>. 
</iframe></p>

在尝试了上述提出的解决方案后,我还在 functions.php 文件中添加了一段额外的代码,以确保 YouTube 链接保持为字符串形式:

remove_shortcode( 'embed' );
remove_filter( 'the_content', [ $GLOBALS['wp_embed'], 'autoembed' ], 8 );
remove_filter( 'the_content', [ $GLOBALS['wp_embed'], 'run_shortcode' ], 8 );
remove_action( 'edit_form_advanced', [ $GLOBALS['wp_embed'], 'maybe_run_ajax_cache' ] );

但输出结果(如下所示)仍然包含了

标签。如前所述,Discourse 帖子中的

标签正是导致问题的原因。

<small>Originally published at http://staging.a1.sg/matrix/</small><br><br>. 
<p>https://www.youtube.com/watch?v=alJEZwwtQ3U</p>

也就是说,如果我移除

标签,链接会在 Discourse 上自动嵌入。但这仍然是一个手动过程。

或者有没有办法在 Discourse 上批量创建帖子?使用来自 Google Sheets 的数据。

这样我就可以弃用 WordPress 了。

WordPress 中的对应工具是 wpallimport.com

您第一个代码示例的输出看起来接近我的预期。我认为问题在于您需要在 Discourse 的“允许的 iframe

效果非常棒!!!非常感谢!!!

但是出现了两个嵌入内容,而不是一个。

输出如下。你之前给我的代码有什么问题吗?

<small>Originally published at http://staging.a1.sg/beautiful/</small><br><br><iframe src="https://www.youtube.com/embed/alJEZwwtQ3U" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe><br><p><iframe src="https://www.youtube.com/embed/alJEZwwtQ3U" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>

add_filter( 'discourse_publish_format_html', 'my_namespace_publish_format', 10, 2 );
function my_namespace_publish_format( $input, $post_id ) {
	$post_content = apply_filters( 'the_content', get_post( $post_id )->post_content );
	$videos = get_media_embedded_in_content( $post_content );
	$video_string = '';
	foreach( $videos as $video ) {
	    $video_string .= $video . '<br>';
    }
    ob_start();

    echo '<small>Originally published at {blogurl}</small><br><br>';
    echo $video_string;
    echo '{excerpt}';
    $output = ob_get_clean();

    return $output;

你现在可以尝试不使用自定义模板发布帖子吗?也许只需要在你的 Discourse 站点设置中的“允许的 iframe 来源”里添加 https://www.youtube.com/embed 即可。

你在 WordPress 网站上使用的是旧版经典编辑器,还是新版区块编辑器?另外,你是将整篇帖子发布到 Discourse,还是只发布摘要?

可以通过编写一个小脚本,从 CSV 文件发布帖子来实现这一功能。

  1. 它起作用了。谢谢 Simon。有没有办法移除指向 WordPress 的链接?如下图所示。

  1. 能否请您指点一下,或者提供一份教程,说明应该编写哪种类型的脚本?

我想最快的方法是用 CSS 把它隐藏起来。

若要防止您的 WordPress 域名链接出现在主题标题下方,请在 WP Discourse 发布设置页面中取消选中“添加特色链接”选项。若要移除已发布帖子中的特色链接,可以通过 CSS 隐藏该链接,或从 Discourse 站点的数据库中将其删除。我接下来几天不在工作岗位,但可以在周三回到工作岗位后为您处理此事。

如果您能等到周三,我可以协助您完成此事。如果您需要在此之前完成,可以尝试在我们的 Support 类别中创建一个新主题,询问如何通过 API 从 CSV 文件创建主题。