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' );

Hey @simon

I used your template to customize the discourse_publish_format_html to display all posts in an iframe. However, the src parameter of the iframe output is empty. See here:

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

Any idea why? Below is my code:

// show post in 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 the default output, or do something else with it here.
    return $output;
}
add_filter( 'discourse_publish_format_html', 'your_namespace_publish_format_html' );

You might need to use the post_id to get the permalink. Can you check if something like this works?

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>를 추가하려면 어떤 변경 사항이 필요한가요?

// shortcode처럼 보이는 모든 것을 제거합니다. 이는 게시글에서
// 괄호 `[...]` 안에 있는 모든 내용을 제거합니다.
add_filter( 'wp_discourse_excerpt', 'testeleven_remove_shortcodes' );
function testeleven_remove_shortcodes( $excerpt ) {
    $excerpt = preg_replace( '/\[.*\]/', '', $excerpt );

    return $excerpt;
}
// discourse에서 iframe을 통해 wp 게시글을 표시
// 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();
}

Discourse 사이트 테마에서 data 속성을 대상으로 지정하여 CSS로 게시물 텍스트를 숨길 수 있습니다. WordPress에서는 게시물 발췌문을 data-hide="true"와 같은 값으로 설정된 data 속성을 가진 div로 감싸야 합니다:

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에 얼마나 효과적인지는 확실하지 않지만, WordPress에서 게시된 게시물에 스타일을 추가하는 좋은 방법은 data 속성을 사용하는 것입니다.

이 템플릿은 단순히 발췌만 추가하는 건가요? 아니면 WP Discourse의 “전체 게시글 표시” 설정이 {excerpt} 플레이스홀더에 대해 전체 게시글을 표시하나요?

excerpt 플레이스홀더는 항상 사용됩니다. ‘전체 게시물 표시’ 설정을 선택하면 플레이스홀더 대신 전체 게시물이 표시됩니다.