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?

1 Like

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.

2 Likes

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:

2 Likes

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

Hey @simon

I used your template to customize the discourse_publish_format_html to display all posts in an iframe. Yet 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();
}
1 Like

It worked well! thx again for the support

@simon

Now I want to add the post text hidden in the topic so that it will be found by the search function and properly scraped by search engine crawlers.

What changes to I have to make to my current code below to put a <div style="display:none;">...</div> with the post content?

// Removes anything that looks like a shortcode. This will remove anything in the post that
// occurs inside of brackets `[...]`.
add_filter( 'wp_discourse_excerpt', 'testeleven_remove_shortcodes' );
function testeleven_remove_shortcodes( $excerpt ) {
    $excerpt = preg_replace( '/\[.*\]/', '', $excerpt );

    return $excerpt;
}
// show wp posts embedded in discourse via iframe
// 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();
}

You can hide the post’s text with CSS by targeting a data attribute in your Discourse site’s theme. On WordPress, wrap the post excerpt in a div with a data attribute set to something like 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();
}

On Discourse, add something like this to your theme:

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

I’m not sure how well this will work for SEO, but using a data attribute is a good way to add styles to posts that are published from WordPress.

1 Like

Doesn’t this template just add the excerpt instead? Or does the WP Discourse “display full post” setting display the full post for {excerpt} placeholder?

The excerpt placeholder is always used. If you select the ‘display full post’ setting, the full post will be substituted for the placeholder.

1 Like