图片无法在 WP 5.3 中发布到 Discourse

I’m guessing this is related to the changes to blocks in the 5.3 update. You can see the post here: In Conversation with Anna Morgan - In Layers: Conversations - Nature Photographers Network

FYI I also have this snippet for CDN images

add_filter( 'wp_discourse_excerpt', 'wpdc_custom_excerpt' );
function wpdc_custom_excerpt( $content ) {
    
    return apply_filters( 'as3cf_filter_post_local_to_s3', $content );
}
3 个赞

I am also getting this error in my dashboard which I’m fairly certain is from the Wordpress plugin.

I came across this post WP Discourse plug in being odd but I am on 1.9.7 and the publishing username is an active account.

I’ll be looking into this today. What I’m seeing on my site is that images published from WordPress are displayed correctly until Discourse attempts to download the remote image. When that is done, I’m ending up with markup like you are getting in the post:

![](upload://kw9YUV5qtquQf5xwatRof6S9RmK.jpeg)

I am not sure if this is related to changes in WordPress 5.3.

If you are getting the We detected an API request using a deprecated authentication method warning, that will not be causing the issue you are having with images. WP Discourse versions 1.9.6 and greater should not be causing that warning. All API requests from the plugin are now using header based authentication.

2 个赞

The problem seems to be that when the Discourse upload markdown is wrapped in HTML tags, the markdown isn’t getting parsed. For example, this is what I’m seeing for a WordPress post after the Discourse PullHotlinkedImages Job is run:

<small>Originally published at:			https://scossar.com/figure-tags-cause-issues/
		</small><br>
<figure class="wp-block-image">![](upload://3hPHOMnM5v5srjlz5QWGmVxY4AL.jpeg)</figure>

Editing the post to remove any html tags that are surrounding the markdown link solves the issue, but a proper solution will need to be found for this.

3 个赞

Sorry to bug, but are you working on this Simon or has someone been assigned to it?

The issue is that when posts with images are published to Discourse, the post’s HTML initially looks something like this:

<figure><img src="https://example.com/wp-content/uploads/your-image.png" /></figure>

If the download remote images to local site setting is enabled on Discourse, the image link will break when Discourse downloads the post from WordPress. The problem will happen any time an image tag with a remote URL is wrapped with HTML tags.

The easiest solutions for this issue are to either disable the download remote images to local site setting, or to not publish full post content from WordPress to Discourse.

In the future, the WP Discourse plugin may remove the option to publish full posts. There are multiple issues that can occurr when publishing full post content from WordPress to Discourse. Most of these issues should be able to be solved by publishing excerpts from WordPress to Discourse and then using the Show Full Post button to display the full post on Discourse. Does this sound like something that could work for your case?

Another possible solution would be to customize the template that is used to publish WordPress posts. Images could be extracted from the posts and then published along with a post excerpt. With a custom template, the image HTML could be structured in a way that doesn’t conflict with the Discourse markdown processor.

1 个赞

Thanks Simon, download remote images to local was the easy solution for now. I am discouraged to hear that publish full posts may go away. The way I currently use it is by publishing a post to wordpress, which then gets published to the Articles section on discourse, which is set to Watching First Post for everyone by default. This way everyone gets an email with the full content of the article, and the best part is they can just reply to the email to add a comment, if they have to take that extra step of visiting the site it’s likely most won’t read the article if there’s just a little snippet, especially if it doesn’t have images which is really important for our site. If publish full posts went away I may consider going away from wordpress and only publish the articles directly in discourse which is not ideal for SEO, etc. but I think the engagement is more important.

1 个赞

我也希望能支持完整的发布功能。
我还不太确定你在这里具体建议了什么……
你会如何提取图片?使用正则表达式吗?
我可以想象用一个正则表达式将图片标签替换为正确的 Markdown 语法。你是这个意思吗?这样做可行吗?

不。如果我没有过去尝试过这类方法,我可能会想这么做。这篇 Stack Overflow 帖子大致解释了这个问题:https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454。

我打算研究一下让插件组装帖子内容的基本 HTML 版本会有多困难。最后的备选方案是使用 DOMDocument 方法来解析内容。Discourse 返回的注释就是用这些方法解析的,而我尚未收到关于此方法存在问题的报告。

我认为发布摘要,然后让用户点击 Discourse 上的“显示完整帖子”按钮来查看完整帖子,可能是解决此问题的最佳方案,但我不太愿意从 WP Discourse 插件中移除“发布完整帖子”选项。

1 个赞

感谢您的解释。
我会让作者直接发布文章并点击“更新”,因为在 WordPress 上先发布再更新文章似乎总能修复 Discourse 上的图片问题。

我宁愿让作者每篇文章多花 1 秒钟,也不愿看到“完整发布”选项消失:wink:

如果您已启用 Discourse 的“将远程图片下载到本地”站点设置,那么在帖子更新后的几分钟内,图片不会再次消失吗?如果不是这种情况,我会进一步调查为何此方法能解决问题。

1 个赞

我确实已启用此设置,且在数天甚至数周后,我所有最新文章中的图片仍显示正常。例如,在这篇 1 个月前的文章 中,我曾遇到过图片显示问题。

此外,我查看了 Discourse 端的图片 URL,发现链接直接指向 WordPress,说明图片并未被下载至 Discourse。这是否是因为网站和论坛共享同一个域名?
(网站:https://monocycle.info,论坛:https://forum.monocycle/info)

4 个赞

当我更新 Discourse 帖子时,仍然显示一个巨大的损坏缩略图,而不是图片。编辑帖子时,我在 < > 中看到以下内容:

img src="http://mysite.com/wp-content/uploads/2020/03/asha2.jpg" class="ss-hidden-pin-image" alt="Blog" data-pin-url="" data-pin-media="http://mysite.com/wp-content/uploads/2020/03/asha2.jpg" data-pin-description=""/

您的 WordPress 站点是 http 还是 https?

目前使用的是 HTTP,不过我正在研究 HTTPS。

如果页面是 HTTPS,浏览器将不会加载来自不安全来源的内容。

如果您依赖 HTML 元素来加载图像,那么直到您的主站使用 HTTPS 之前,这都不会生效。

2 个赞

我刚刚将 WP Discourse 版本 2.0.2 推送到 WordPress 插件仓库。此次更新应能解决在使用区块编辑器发布帖子到 Discourse 时出现的图片损坏问题。

现在,帖子中的图片、图片画廊,以及 YouTube 和 Vimeo 视频都会被提取并以 Discourse 能够处理的方式进行格式化。如果您在更新后遇到任何问题,请随时告知。如果仍有某些 WordPress 区块在 Discourse 上无法正确渲染,也请告诉我——现在可以按名称解析区块,因此应该能够解决任何相关问题。

下周我将添加一个过滤器,可以挂钩用于解析那些过于生僻、插件本身无法处理的区块。

3 个赞

更新后这里有些问题。

在 WordPress 中,我使用的是“经典编辑器”(TinyMCE)。

我有一些旧文章,其中的视频 URL 使用了 [video] 短代码;而最近的文章(不到 3 年)则使用了 ARVE 视频嵌入插件,它将视频 URL 放在 [arve] 短代码中。
因此,我通过以下方式将 WordPress 的文章过滤到 Discourse:

    $excerpt = preg_replace('/\[arve .*url="(.*?)" .*\/\]/is',"\n$1\n", $excerpt);
    $excerpt = preg_replace('/\[video .*mp4="(.*)"\]\[\/video\]/is',"\n$1\n", $excerpt);

之前一切正常,只有视频 URL 被传递到 Discourse,因此它们在 Discourse 上正确显示为嵌入视频。

但自从 WP-Discourse 更新后,视频无法在 Discourse 上显示了。
我还尝试直接在 TinyMCE 中粘贴 YouTube URL,不使用短代码(我了解到 WordPress 似乎不需要短代码就能嵌入 YouTube 视频……或者这可能是因为我的其他插件或主题?:thinking: 但我想这并不重要),并移除了我的 preg_replace 函数,但视频仍然无法在 Discourse 上显示。

以下是我在 WordPress 上的文本(纯文本模式,非可视化编辑标签):

https://www.youtube.com/watch?v=e6MCkspqtxo

[arve url="https://www.youtube.com/watch?v=e6MCkspqtxo" /]

在 WordPress 上的显示效果:

Discourse 上的帖子:

Discourse 帖子的 HTML 代码:

<p>Test vidéooo:</p>
<div data-mode="normal" data-provider="youtube">
<div></div>
</div>
<div data-mode="normal" data-provider="youtube">
<div></div>
</div>

编辑:另外,我注意到当文章在 WordPress 上设为私密时,编辑文章后不会同步到 Discourse。这在进行私密测试时有点烦人。

1 个赞

这很奇怪。我进行这次更改的初衷是希望它不会影响通过经典编辑器发布的帖子。我会尝试复现该问题。如果您在编辑器的“文本”标签中打开该帖子,能否分享一下您看到的标记?

1 个赞

使用 WP 插件 ARVE

<p>测试视频ooo:</p>
<div class="arve-wrapper aligncenter" data-mode="normal" data-provider="youtube" id="arve-e6MCkspqtxo-3" style="max-width:800px;" itemscope itemtype="http://schema.org/VideoObject">
<div class="arve-embed-container" style="padding-bottom:56.250000%"><iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen class="arve-iframe fitvidsignore" frameborder="0" name sandbox="allow-scripts allow-same-origin allow-presentation allow-popups allow-popups-to-escape-sandbox" scrolling="no" src="https://www.youtube-nocookie.com/embed/e6MCkspqtxo?iv_load_policy=3&#038;modestbranding=1&#038;rel=0&#038;autohide=1&#038;playsinline=1&#038;autoplay=0" width="480" height="270"></iframe></div>
</div>
<div class="arve-wrapper aligncenter" data-mode="normal" data-provider="youtube" id="arve-e6MCkspqtxo-4" style="max-width:800px;" itemscope itemtype="http://schema.org/VideoObject"><div class="arve-embed-container" style="padding-bottom:56.250000%"><iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen class="arve-iframe fitvidsignore" frameborder="0" name sandbox="allow-scripts allow-same-origin allow-presentation allow-popups allow-popups-to-escape-sandbox" scrolling="no" src="https://www.youtube-nocookie.com/embed/e6MCkspqtxo?iv_load_policy=3&#038;modestbranding=1&#038;rel=0&#038;autohide=1&#038;playsinline=1&#038;autoplay=0" width="480" height="270"></iframe></div></div>

不使用插件,如果我只在 WP 中写入:

https://www.youtube.com/watch?v=e6MCkspqtxo

Discourse 编辑器显示的结果如下:

<p>测试视频ooo:</p>
<div class="fitvids-video"><iframe title="Volkor X - Enclave" width="800" height="450" src="https://www.youtube.com/embed/e6MCkspqtxo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>

看起来 WP 仍在使用另一个非原生库,也许来自我的主题?:thinking:
我想问题更多出在我这边,而不是你们那边,不过更新之前一切正常……:sweat_smile:

2 个赞