如何从RSS提要中的帖子中保留HTML

时间:2021-10-04 作者:paneerakbari

我已经用几个不同的HTML标记建立了一个示例帖子。这是从块编辑器复制的标记:

<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>This is paragraph 1.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In a new paragraph, we begin an unordered list:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>list item 1</li><li>list item 2</li><li>list item 3</li></ul>
<!-- /wp:list -->

<!-- wp:image {"id":418,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://domain.tld/wp-content/uploads/2018/11/logo.png" alt="" class="wp-image-418"/><figcaption>organization logo</figcaption></figure>
<!-- /wp:image -->

<!-- wp:html -->
Begin custom HTML
<br><hr><a href="#">link trial</a><br>
<div><p>This is a paragraph in a div</div>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>Each</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>of</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>these</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>words</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>is</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>a</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>paragraph</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
查看我的feed,它显示descriptioncontent:encoded 以下元素:

<description><![CDATA[This is an excerpt]]></description>
<content:encoded><![CDATA[<p>This is paragraph 1. In a new paragraph, we begin an unordered list: list item 1 list item 2 list item 3 Begin custom HTML link trial This is a paragraph in a div Each of these words is a paragraph...</p>
<p><a href="https://domain.tld/sample/" rel="nofollow">Source</a></p>]]></content:encoded>
显然,某种机制是剥离所有HTML并将帖子的纯文本包装在一个<p> - 但我正在努力找到它是什么。

我放置了一个print_debug_backtrace() 在顶部feed-rss2.php 并得到以下结果:

#0  require_once() called at [/home1/foo/public_html/wp-includes/template.php:770]
#1  load_template(/home1/foo/public_html/wp-includes/feed-rss2.php) called at [/home1/foo/public_html/wp-includes/functions.php:1623]
#2  do_feed_rss2() called at [/home1/foo/public_html/wp-includes/class-wp-hook.php:305]
#3  WP_Hook->apply_filters(, Array ([0] => ,[1] => rss2)) called at [/home1/foo/public_html/wp-includes/class-wp-hook.php:327]
#4  WP_Hook->do_action(Array ([0] => ,[1] => rss2)) called at [/home1/foo/public_html/wp-includes/plugin.php:470]
#5  do_action(do_feed_rss2, , rss2) called at [/home1/foo/public_html/wp-includes/functions.php:1585]
#6  do_feed() called at [/home1/foo/public_html/wp-includes/template-loader.php:48]
#7  require_once(/home1/foo/public_html/wp-includes/template-loader.php) called at [/home1/foo/public_html/wp-blog-header.php:19]
#8  require(/home1/foo/public_html/wp-blog-header.php) called at [/home1/foo/public_html/index.php:17]
我还设置了一个自定义调试页面,并逐步完成了我观察到的<content:encoded> 要素这些测试都返回了所需的HTML,没有任何WP内容注释。

谢谢你们能帮我的忙

附录:这里的最终目标是使MailChimp的RSS合并能够收集这些帖子并以定期电子邮件的形式集体发送,而无需重复工作。我知道HTML在电子邮件中的局限性,所有帖子都将是非常简单的标记,但我希望至少有段落分隔符。我很确定我的用例避免了使用API。将标记包装在CDATA 我不希望与RSS验证发生冲突。

1 个回复
最合适的回答,由SO网友:paneerakbari 整理而成

事实证明,这种行为是由插件导致的,插件的默认设置是将所有帖子转换为摘录,从而剥离标记。

我通过建立一个没有任何自定义项或主题的WordPress实例,复制已安装的插件(只是它们的存在,而不是它们的设置),并在激活每个插件后检查提要来确定这一点。

这个故事的寓意是,您将错过许多行为,浪费大量时间修补代码,而不是专注于复制行为。