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

Ciao @simon

Ho utilizzato il tuo template per personalizzare discourse_publish_format_html in modo da visualizzare tutti i post in un iframe. Tuttavia, il parametro src dell’iframe generato è vuoto. Vedi qui:

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

Hai idea del perché? Di seguito il mio codice:

// mostra il post in 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();

    // Restituisci l'output predefinito, oppure fai qualcos'altro qui.
    return $output;
}
add_filter( 'discourse_publish_format_html', 'your_namespace_publish_format_html' );

Potresti aver bisogno di usare post_id per ottenere il permalink. Puoi verificare se qualcosa del genere funziona?

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

Ha funzionato bene! Grazie ancora per il supporto

@simon

Ora voglio aggiungere il testo del post nascosto all’interno dell’argomento, in modo che venga rilevato dalla funzione di ricerca e indicizzato correttamente dai crawler dei motori di ricerca.

Quali modifiche devo apportare al mio codice attuale qui sotto per inserire un <div style="display:none;">...</div> con il contenuto del post?

// Rimuove tutto ciò che sembra uno shortcode. Questo rimuoverà tutto ciò che si trova nel post
// all'interno delle parentesi quadre `[...]`.
add_filter( 'wp_discourse_excerpt', 'testeleven_remove_shortcodes' );
function testeleven_remove_shortcodes( $excerpt ) {
    $excerpt = preg_replace( '/\[.*\]/', '', $excerpt );

    return $excerpt;
}
// mostra i post di wp incorporati in discourse tramite 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();
}

Puoi nascondere il testo del post con il CSS prendendo di mira un attributo data nel tema del tuo sito Discourse. Su WordPress, avvolgi l’estratto del post in un div con un attributo data impostato su qualcosa come 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();
}

Su Discourse, aggiungi qualcosa di simile al tuo tema:

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

Non sono sicuro di quanto bene questo funzioni per la SEO, ma l’uso di un attributo data è un buon modo per aggiungere stili ai post pubblicati da WordPress.

Non aggiunge forse questo modello semplicemente l’estratto? Oppure l’impostazione “mostra post completo” di WP Discourse visualizza il post completo per il segnaposto {excerpt}?

Il segnaposto excerpt viene sempre utilizzato. Se selezioni l’impostazione ‘visualizza post completo’, il post completo sostituirà il segnaposto.