WP Discourse - Template Customization

Use case: There are YouTube links on my WordPress posts. I want to automatically get the youtube video to appear on the respective discourse post after publishing the WordPress post.

Problem: It doesn’t seem to work because after syncing, the discourse post html is

<p>https://www.youtube.com/watch?v=alJEZwwtQ3U</p>

the youtube video will only successfully appear if i remove the <p></p>

so how do i remove the <p></p>? I hope not to remove the <p></p> tags manually as I might have thousands of posts.

Not sure if I can remove the <p></p> tags directly in the functions.php file.

I followed your customization tutorial. This is the code in the functions.php

// Adds the featured image to the post that is published to Discourse.
function my_namespace_publish_format( $input ) {
    ob_start();
    ?>
    {excerpt} **<-----can i remove the p tags here?**
    <?php
    $output = ob_get_clean();
    
    // Note: the call to apply_filters() that was in the original function has been removed.
    return $output; 
}

add_filter( 'discourse_publish_format_html', 'my_namespace_publish_format' );
1 Like

Depending on how your videos have been published, you might be able to use something like this:

add_filter( 'discourse_publish_format_html', 'my_namespace_publish_format', 10, 2 );
function my_namespace_publish_format( $input, $post_id ) {
	$post_content = apply_filters( 'the_content', get_post( $post_id )->post_content );
	$videos = get_media_embedded_in_content( $post_content );
	$video_string = '';
	foreach( $videos as $video ) {
	    $video_string .= $video . '<br>';
    }
    ob_start();

    echo '<small>Originally published at {blogurl}</small><br><br>';
    echo $video_string;
    echo '{excerpt}';
    $output = ob_get_clean();

    return $output;
}

This should work if the videos are embedded on your WordPress site. It make take some trial and error to get the formatting correct. The main problem I ran into with it was that if there are leading spaces before the Youtube iframe tag, Discourse will interpret it as code.

Let me know if this approach doesn’t work on your site.

What I would like to get to work would be to have Youtube videos displayed when users click the “Show Full Post” button on Discourse to expand a WordPress post. I am not sure if that is going to be possible though.

1 Like

hi simon

thanks for responding. appreciate the help immensely.

but it doesnt seem to work

the output was

<small>Originally published at http://staging.a1.sg/matrix/</small><br><br><iframe title="GCE O-Level 
A-Maths: Matrix &amp; its Inverse" width="525" height="394" 
src="https://www.youtube.com/embed/alJEZwwtQ3U?feature=oembed" frameborder="0" 
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>. 
</iframe><br><p><iframe title="GCE O-Level A-Maths: Matrix &amp; its Inverse" width="525" 
height="394" src="https://www.youtube.com/embed/alJEZwwtQ3U?feature=oembed" frameborder="0" 
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>. 
</iframe></p>

i also added an extra code in the functions.php file after trying out the above proposed solution to ensure the youtube link stays as a string.

remove_shortcode( 'embed' );
remove_filter( 'the_content', [ $GLOBALS['wp_embed'], 'autoembed' ], 8 );
remove_filter( 'the_content', [ $GLOBALS['wp_embed'], 'run_shortcode' ], 8 );
remove_action( 'edit_form_advanced', [ $GLOBALS['wp_embed'], 'maybe_run_ajax_cache' ] );

but the output (as shown below) has < p > tags too. as mentioned earlier, the < p > tags in a discourse posts are causing the issue.

<small>Originally published at http://staging.a1.sg/matrix/</small><br><br>. 
<p>https://www.youtube.com/watch?v=alJEZwwtQ3U</p>

i.e. if i remove the p tags, the link will autoembed on discourse. but it’s still a manual process.

or is there a way to mass create posts on discourse? using data from google sheets.

this way, i can do away with wordpress.

the equivalent in wordpress is wpallimport.com

The output from your first code example looks close to what I was expecting. I think the problem with it is that you need to add https://www.youtube.com/embed to your Discourse allowed iframes site setting. That will allow the iframe that is in the post’s markup to display as a video player on Discourse.

3 Likes

works beautifully!!! Thank you!!!

but there are 2 embeds intead of one.

output is below. anything wrong with the code you suggested to me?

<small>Originally published at http://staging.a1.sg/beautiful/</small><br><br><iframe src="https://www.youtube.com/embed/alJEZwwtQ3U" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe><br><p><iframe src="https://www.youtube.com/embed/alJEZwwtQ3U" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>

add_filter( 'discourse_publish_format_html', 'my_namespace_publish_format', 10, 2 );
function my_namespace_publish_format( $input, $post_id ) {
	$post_content = apply_filters( 'the_content', get_post( $post_id )->post_content );
	$videos = get_media_embedded_in_content( $post_content );
	$video_string = '';
	foreach( $videos as $video ) {
	    $video_string .= $video . '<br>';
    }
    ob_start();

    echo '<small>Originally published at {blogurl}</small><br><br>';
    echo $video_string;
    echo '{excerpt}';
    $output = ob_get_clean();

    return $output;

Can you try publishing a post without using the custom template now? It may be that all that needed to be done was to add https://www.youtube.com/embed to your Discourse allowed iframes site setting.

Which editor are you using on your WordPress site, the old Classic Editor, or the new Block Editor? Also, are you publishing the full post to Discourse, or just an excerpt?

This could be done by writing a small script to publish posts from a CSV file.

1 Like
  1. it works. thank you simon. is there a way to remove the link back to wordpress? as shown as the screenshot below.

  1. could you kindly point me towards a direction or a tutorial of what sort of script should be written?
1 Like

I guess the fastest way would be hiding it with CSS.

To prevent the link to your WordPress domain appearing below the topic title, deselect the “Add Featured Links” option on your WP Discourse Publishing settings page. To remove the featured link from posts that have already been published, the link can either be hidden with CSS, or it can be removed from your Discourse site’s database. I am away from work for the next couple of days, but can take care of that for you when I get back to work on Wednesday.

If you can wait until Wednesday, I can give you a hand with this. If you need to do it sooner than that, you could try creating a new topic in our support category that asks how to create topics through the API from a CSV file.

2 Likes