WP Discourse Won't Publish Private Post

(Paul H Gross) #1


I am using Discourse with SSO from Wordpress and all discussions are private. But I want Wordpress to not only publish public posts on my Discourse forum but also private posts. It works fine for public but never shows up for private posts. Only my WP subscribers have access to my Discourse forum. So I’m wondering if I can make a mod to the plug-in. I found the code for valid_post_sync wp_discourse_publish.php but that is higher level --for post types like post or page. I cannot find the code that might be filtering the post for privacy. Can you tell where I might find this and if there is a best practice for modifying the WPDiscourse plugin with this mod?



(Simon Cossar) #2

Do your private posts have a post type? If so, the reason the post type isn’t showing up in the Post Types to Publish setting (found on the plugin’s Publishing tab) is probably because of this:

It is only looking for public post types. You could try hooking into the discourse_post_types_to_publish filter and adding your private post type to the $post_types array.

The best practice is to use an available action or filter hook and make your modifications in a separate plugin. If you need a hook added to the plugin, just ask. My goal with the plugin is to make it as extensible as possible.

(Paul H Gross) #3

Thanks for the fast and thorough reply. I’m a semi-regular PHP code reader, an occasional PHP code modifier and a very rare PHP code writer. I presume I can learn how to “hook into the discourse_post_types_to_publish” from other hook examples and then either replace the array to add to it.

WRT other hooks, I have another site using WP Discourse. I use a modified Buddy Press plugin to make their first name a Unique and obfuscated ID because it is a site about health issues and people want to connect but stay anonymous. I am using SSO but I want to change the mapping of WP user profiles fields for Discourse so that name becomes username. (I don’t know that this is the right way to solve this give the role of usernames in SSO so feel free to say there is a different way to do this other than hooking the mapping.) Any insight would be helpful.


(Simon Cossar) #4

To add a private post type, try something like this, with the name of your post type substituted for ‘new_type’. Let me know if you’re still having trouble publishing the posts.

add_filter( 'discourse_post_types_to_publish', 'wpdc_custom_post_types_to_publish' );
function wpdc_custom_post_types_to_publish( $post_types ) {
    $post_types['new_type'] = 'new_type';

    return $post_types;

If the first name is guaranteed to be unique and is a valid Discourse username, you can set it in the SSO parameters like this:

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params', 10, 2 );
function wpdc_custom_sso_params( $params, $user ) {
    $params['username'] = $user->first_name;

    return $params;

For this to override the Discourse username for users who already exist on your forum, you’ll need to select the Discourse option sso overrides username. You might want to include some code to handle the case where the first_name isn’t set.

You can add the above two functions either to your theme’s functions.php file, or to a plugin.

(Paul H Gross) #5

Thanks a ton Simon. This was super helpful. I need to read about creating post types but I see how this will work. Thanks for the guidance on this and the username issue as well…


(Paul H Gross) #6

Okay, having a read a bunch about creating new post types, I’m worried that this will change the behavior of WP Discourse in the way I want (publish my private post) but likely break another plug in that I’m very dependent on called Subscribe2. I have two audiences that follow by blog – members (registered users) and public users (anonymous users that have submitted an email for notifications). If I mark a post ‘private’, an email notification goes out to all of my members but not my public subscribers. This is very helpful for my management of information flow.

But now I want my members to discuss my private (or public) post on Discourse and hence the desire to publish my posts that are marked private. It looks like creating a new post type that gets the post type definition for a post and simply modifies the args to be “public => false” will work for WP Discourse but will not be seen as a post to notify on Subscribe2.

I am wondering if there is a way of hooking to just add the logic so that the function posts_types_to_publish did not look at the arg public and just managed the excluded types.


(Simon Cossar) #7

I was assuming that your private posts were already using a custom post type, but it sounds as though they are just regular posts, set to ‘private’ here:


There are two places in the wp-discourse code where the post_status is checked to make sure that a post has the published status before publishing it to Discourse.

If you can wait until early next week, I’ll add a filter that lets you override this behaviour.

(Paul H Gross) #8

You are correct. Sorry that I misunderstood your initial question about my post type (learning curve). I can certainly wait a week to avoid changing plugin code and having an appropriate hook.

From looking at the code, I don’t understand how this relates to the “post_types_to_publish” which checks for public => true. But this is probably my lack of understanding between the post_types args and the post_status.

I will keep an eye out for an update.

Thank you!


(Simon Cossar) #9

I have added a wpdc_publish_private_post filter hook that you can use to publish private posts to Discourse. The filter passes up to two parameters to a function that you hook into it. The parameters are $publish (set to false) and $post_id, the WordPress post id.

If you wish to allow all private posts to be published to Discourse, you can add something like this to a plugin, or your theme’s functions.php file. You can name your hook functions however you choose, just be sure that the name is unique in your site’s codebase.

add_filter( 'wpdc_publish_private_post', 'wpdc_custom_publish_private_post', 10, 2 );
function wpdc_custom_publish_private_post( $publish, $post_id ) {
    if ( 'private' === get_post_status( $post_id ) ) {

        return true;

    return false;

If you would only like to allow certain private posts to be published, you’ll need to do something like this:

add_filter( 'wpdc_publish_private_post', 'wpdc_custom_publish_private_post', 10, 2 );
function wpdc_custom_publish_private_post( $publish, $post_id ) {
    $post = get_post( $post_id );
    if ( /* Some condition that checks the $post object */ ) {
        return true;
    return false;

One thing to note is that the ‘Show Full Post’ button on Discourse will not work for private WordPress posts - Discourse can’t retrieve the post’s content. You might want to disable this feature on Discourse by deselecting embed truncate in your Discourse settings.

(Paul H Gross) #10

Hi Simon,

Thanks for creating this in such a short time. I have added the filter and the function in my functions.php and turned off embed truncate but private posts are still not posting for me. Let me know what you need me to do to debug…

On an unrelated note, does WP Discourse pull new categories from discourse into the create/link dialogue to the right of the post editor? I don’t see my other created categories…



(Simon Cossar) #11

Make sure you are on version 1.7.5 of the plugin and that ‘Publish to Discourse’ is selected in the post’s Discourse metabox. Double check for errors in the code you have added. Copying the first code example I provided into your theme’s functions.php file should work in most cases. It can be difficult to debug errors in a live environment. If you have a development version of your site, you can add some code to the wpdc_custom_publish_private_post() function to be sure that it’s being called. I usually create a debug.log file and write to that file to see what’s going on. See this post for details about how to set that up.

If you have added categories to Discourse, you need to select ‘Force Category Update’ from the WP Discourse Publishing options tab and then click ‘Save Options’ to pull in the new categories. You are the second person who has asked about this recently, so I’m going to change that behaviour in the plugin’s next update.

(Paul H Gross) #13

I’m too embarrassed to say what I did wrong. Works perfectly. Thanks for the “instructions for dummies”.

Really appreciate your responsiveness and the value of this plugin.