WP-discourse: how to deal with shortcodes in post

I noticed that what WP-discourse is publishing to Discourse are the raw texts from the WordPress posts, not the rendered html. And I guess that makes some sense, given how complex wordpress pages can be. But it’s also a problem since it means you can’t use any shortcodes (or you can’t use discourse with the posts where you do)…

Is there any workaround for this?

Are there any plans of maybe providing an option to use the rendered html as the basis for the discourse post?

Take a look at the ‘Dealing with WordPress shortcodes’ section of this topic: WP Discourse Plugin Tips and Tricks
The do_shortcode option that’s given in that topic generally doesn’t work, but I think some changes could be made to the plugin to get it working.

I solved this by circumventing the content of the post and merely exporting the post url via the plugin while making sure that it will be oneboxed, like this:

For posts where you need the shortcode content to be published to Discourse, it’s possible to publish the post in an iframe. This works best if the WordPress posts uses a custom post-type. That way you can decide which posts are to be published in an iframe. It takes a bit of work on the WordPress end so that posts are published in a format that’s suitable for putting in an iframe. You also need to be able to whitelist the iframe on Discourse for this to work.

I’ve been testing this with both an image gallery shortcode, and a groups sign-up form. It seems to be working quite well.

function your_namespace_publish_format_html( $output ) {
    global $post;

    if ( 'my_iframe_post_type' === $post->post_type) {
	ob_start();

	?>
    <iframe width="690" height="600" src="<?php echo esc_url( the_permalink() ); ?>" frameborder="0"></iframe>
	<?php
	$output = ob_get_clean();

	// Return an iframe for this post type.
	return $output;
    }

    // Return the default output, or do something else with it here.
    return $output;
}
add_filter( 'discourse_publish_format_html', 'your_namespace_publish_format_html' );

@simon

我使用了你的模板来自定义 discourse_publish_format_html,以便在 iframe 中显示所有帖子。但是,生成的 iframe 的 src 参数是空的。请参见此处:

https://forum.cannabisanbauen.net/t/ist-meine-pflanze-maennlich-was-tun/7196

有什么原因吗? 下面是我的代码:

// 在 iframe 中显示帖子
function your_namespace_publish_format_html( $output ) {
    global $post;
    ?>
    <iframe width="690" height="2000" src="<?php echo esc_url( the_permalink() ); ?>" frameborder="0"></iframe>
    <?php
    $output = ob_get_clean();

    // 返回默认输出,或在此处对其进行其他处理。
    return $output;
}
add_filter( 'discourse_publish_format_html', 'your_namespace_publish_format_html' );

您可能需要使用 post_id 来获取永久链接。您能检查一下类似这样的代码是否可行吗?

add_filter( 'discourse_publish_format_html', 'wpdc_custom_discourse_publish_format_html', 10, 2 );
function wpdc_custom_discourse_publish_format_html( $output, $post_id ) {
    $permalink = get_the_permalink( $post_id ) . '?embed=true';
    ob_start();
    ?>
    <iframe width="690" height="600" src="<?php echo esc_url( $permalink ); ?>" frameborder="0"></iframe>
    <?php

    return ob_get_clean();
}

效果很好!再次感谢您的支持

@simon

现在,我想将帖子文本隐藏在该主题中,以便搜索引擎功能能够找到它,并被搜索引擎爬虫正确抓取。

我需要对下面的当前代码进行哪些更改,才能将包含帖子内容的 <div style="display:none;">...</div> 添加进去?

// 移除所有看起来像短代码的内容。这将移除帖子中
// 位于方括号 [...] 内的所有内容。
add_filter( 'wp_discourse_excerpt', 'testeleven_remove_shortcodes' );
function testeleven_remove_shortcodes( $excerpt ) {
    $excerpt = preg_replace( '/\[.*\]/', '', $excerpt );

    return $excerpt;
}
// 显示通过 iframe 嵌入到 Discourse 中的 WordPress 帖子
// https://meta.discourse.org/t/wp-discourse-how-to-deal-with-shortcodes-in-post/58838/5
add_filter( 'discourse_publish_format_html', 'wpdc_custom_discourse_publish_format_html', 10, 2 );
function wpdc_custom_discourse_publish_format_html( $output, $post_id ) {
    $permalink = get_the_permalink( $post_id );
    ob_start();
	?>
    <iframe width="690" height="2000" src="<?php echo esc_url( $permalink ); ?>" frameborder="0"></iframe>
	<?php

	return ob_get_clean();
}

在 WordPress 上,您可以通过将帖子摘要包裹在一个带有 data 属性的 div 中来隐藏帖子正文,例如设置 data-hide="true"

add_filter( 'discourse_publish_format_html', 'wpdc_custom_discourse_publish_format_html', 10, 2 );
function wpdc_custom_discourse_publish_format_html( $output, $post_id ) {
	$permalink = get_the_permalink( $post_id ) . '?embed=true';
	ob_start();
	?>
    <div data-hide="true">{excerpt}</div>
    <iframe width="690" height="600" src="<?php echo esc_url( $permalink ); ?>" frameborder="0"></iframe>
	<?php

	return ob_get_clean();
}

在 Discourse 上,将类似以下内容添加到您的主题中:

[data-hide="true"] {
    display: none;
}

我不确定这对 SEO 的效果如何,但使用 data 属性是从 WordPress 发布的帖子添加样式的好方法。

这个模板难道不是只添加摘要吗?还是说 WP Discourse 的“显示完整帖子”设置会为 {excerpt} 占位符显示完整帖子?

excerpt 占位符始终会被使用。如果您选择了“显示完整帖子”设置,完整帖子将替换该占位符。