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

Hola @simon

He utilizado tu plantilla para personalizar discourse_publish_format_html y mostrar todas las publicaciones en un iframe. Sin embargo, el parámetro src del iframe de salida está vacío. Mira esto:

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

¿Alguna idea de por qué? A continuación está mi código:

// mostrar la publicación en un 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();

    // Devuelve la salida predeterminada, o haz algo más con ella aquí.
    return $output;
}
add_filter( 'discourse_publish_format_html', 'your_namespace_publish_format_html' );

Es posible que necesites usar el post_id para obtener el enlace permanente. ¿Puedes comprobar si algo como esto funciona?

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();
}

¡Funcionó bien! Gracias de nuevo por el apoyo.

@simon

Ahora quiero agregar el texto del post oculto en el tema para que sea encontrado por la función de búsqueda y correctamente rastreado por los motores de búsqueda.

¿Qué cambios debo hacer en mi código actual de abajo para incluir un <div style="display:none;">...</div> con el contenido del post?

// Elimina cualquier cosa que parezca un shortcode. Esto eliminará todo lo que esté
// dentro de corchetes `[...]` en el post.
add_filter( 'wp_discourse_excerpt', 'testeleven_remove_shortcodes' );
function testeleven_remove_shortcodes( $excerpt ) {
    $excerpt = preg_replace( '/\[.*\]/', '', $excerpt );

    return $excerpt;
}
// Muestra publicaciones de wp incrustadas en discourse a través de 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();
	?>
    
    <div style="display:none;"><?php echo esc_html( get_the_content( $post_id ) ); ?></div>
    <iframe width="690" height="2000" src="<?php echo esc_url( $permalink ); ?>" frameborder="0"></iframe>
	<?php

	return ob_get_clean();
}

Puedes ocultar el texto del post con CSS dirigiendo un atributo data en el tema de tu sitio Discourse. En WordPress, envuelve el extracto del post en un div con un atributo data establecido a algo como 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();
}

En Discourse, agrega algo así a tu tema:

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

No estoy seguro de lo bien que esto funcionará para el SEO, pero usar un atributo data es una buena manera de agregar estilos a los posts que se publican desde WordPress.

¿No es que esta plantilla simplemente añade el extracto? ¿O la configuración “mostrar publicación completa” de WP Discourse muestra la publicación completa para el marcador de posición {excerpt}?

El marcador de posición excerpt siempre se utiliza. Si seleccionas la configuración ‘mostrar publicación completa’, la publicación completa se sustituirá por el marcador de posición.