# WP Discourse Filters and Actions

**URL:** https://meta.discourse.org/t/wp-discourse-filters-and-actions/307211
**Category:** Administrators
**Tags:** wordpress
**Created:** [May 8, 2024, 6:47am UTC](https://meta.discourse.org/t/wp-discourse-filters-and-actions/307211 "2024-05-08T06:47:49Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [May 8, 2024, 6:47am UTC](https://meta.discourse.org/t/wp-discourse-filters-and-actions/307211/1 "2024-05-08T06:47:50Z")

</div>

These are all actions and filters provided by the [WP Discourse](https://github.com/discourse/wp-discourse) plugin. Examples are illustrative only. You will need to test and validate the example on your own instance before using it in production.

## Filters

For a general explanation on how you can use filters in Wordpress see

> **[Filters – Plugin Handbook | Developer.WordPress.org](https://developer.wordpress.org/plugins/hooks/filters/)**
>
> Filters are one of the two types of Hooks. They provide a way for functions to modify data during the execution of WordPress Core, plugins,…

### Publishing

[WP Discourse](https://github.com/discourse/wp-discourse) publishing filters.

#### discourse\_post\_types\_to\_publish

Filter the post types which appear in the “Post Types to Publish” setting post type list. Note that this will not enable or disable discourse publishing for post types.

##### Example

Only show “post” type:

```php
function filter_post_types( $post_types ) {
  return array( 'post' );
}
add_filter( 'wp_discourse_publish_categories', 'filter_post_types', 10, 1);

```

#### wpdc\_publish\_private\_post

Publish private Wordpress posts to Discourse.

#### Example

```php
function wpdc_custom_publish_private_post( $publish, $post_id ) {
  return 'private' === get_post_status( $post_id );
}
add_filter( 'wpdc_publish_private_post', 'wpdc_custom_publish_private_post', 10, 2 );

```

> [@WP Discourse Won't Publish Private Post](https://meta.discourse.org/t/wp-discourse-wont-publish-private-post/96166/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 u…

#### wpdc\_publish\_after\_save

Whether a specific post should be published to Discourse.

##### Example

> [@WP-Discourse \> 'Publish all new posts to Discourse.' \> not working](https://meta.discourse.org/t/wp-discourse-publish-all-new-posts-to-discourse-not-working/58250/13):
>
> It’s actually fairly simple to get this working. I’ve added a wpdc\_publish\_after\_save filter to the wp-discourse publish\_post\_after\_save function that you can hook into. It’s passed the variables $force\_publish, $post\_id, and $post. It’s not yet merged into the main code, but you can get it here: [https://github.com/scossar/wp-discourse/tree/beta-after-save-filter](https://github.com/scossar/wp-discourse/tree/beta-after-save-filter). Something like this added to your theme’s functions.php file will auto publish all posts from an array of feeds. There are some iss…

#### wp\_discourse\_before\_xmlrpc\_publish

Whether a specific post should be published to Discourse using xmlrpc.

##### Example

Don’t publish xmlrpc posts with “apples” in the title.

```php
function publish_after_save ( $publish_to_discourse, $post ) {
   return strpos($post->post_title, 'apples') !== false;
}
add_filter( 'wpdc_publish_after_save', 'publish_after_save', 10, 2 );

```

#### wpdc\_publish\_format\_title

Modify the title of the Discourse topic.

##### Example

> [@Using \_yoast\_wpseo\_title as the topic title](https://meta.discourse.org/t/using-yoast-wpseo-title-as-the-topic-title/106991/2):
>
> The WordPress plugin has a wpdc\_publish\_format\_title filter that’s applied to the post title before it’s published to Discourse. You can use the filter like this. I’m assuming that \_yoast\_wpseo\_title is saved as post metadata: add\_filter( 'wpdc\_publish\_format\_title', 'wpdc\_custom\_publish\_format\_title', 10, 2 ); function wpdc\_custom\_publish\_format\_title( $title, $post\_id ) { //$title = get\_post\_meta( $post\_id, '\_yoast\_wpseo\_title', true ); return $title; }

#### wp\_discourse\_excerpt

Modify the content of posts being sent to Discourse.

##### Example

> [@WP Discourse plugin tips and tricks](https://meta.discourse.org/t/wp-discourse-plugin-tips-and-tricks/50757#deal-with-wordpress-shortcodes-6):
>
> This topic has a number of tips and tricks for users of the [WP Discourse](https://github.com/discourse/wp-discourse) WordPress plugin. Adjust the comments\_number sync period for WordPress archive pages To get the correct number of Discourse comments for a post, the plugin needs to periodically make an HTTP request to Discourse to get the current comments number. On single pages this period is set to be no more often than every 10 minutes. For archive pages the period is set to no more than once every 24 hours. The reason for this is to a…

#### wpdc\_publish\_post\_category

Change the category to which the post is published.

##### Example

Always publish posts to the Site Feedback category.

```php
function wpdc_change_post_category( $category, $post_id ) {
  $categories = WPDiscourse\Utilities\Utilities::get_discourse_categories();
  return array_search("Site Feedback", array_column($categories, 'name'));
}
add_filter( 'wpdc_publish_post_category', 'wpdc_change_post_category' );

```

#### wp\_discourse\_publish\_categories

Filter the categories which appear in the Category setting in the [WP Discourse](https://github.com/discourse/wp-discourse) sidebar when creating or editing a Wordpress post.

##### Example

Only show the Site Feedback category:

```php
function filter_categories( $categories ) {
  return array_filter( $categories, function( $category ){
	return $category['name'] === 'Site Feedback';
  });
};
add_filter( 'wp_discourse_publish_categories', 'filter_categories', 10, 1);

```

#### wpdc\_publish\_unlisted

Determine whether the published post is unlisted on Discourse.

##### Example

Publish unlisted if the post has the term “document” in the “knowledge\_base” Wordpress taxonomy.

```php
function wpdc_unlisted_for_site_feedback( $unlisted, $post, $post_id ) {
  return has_term( "document", "knowledge_base", $post ) || $unlisted;
}
add_filter( 'wpdc_publish_unlisted', 'wpdc_unlisted_for_site_feedback' );

```

#### wpdc\_discourse\_username

Set the discourse username to be the set as the author of the Discourse post.

##### Example

Set “angus” as the author of all posts published to Discourse.

```php
function wpdc_custom_username( $discourse_username, $author_id ) {
  return 'angus';
}
add_filter( 'wpdc_discourse_username', 'wpdc_custom_username' );

```

#### wpdc\_publish\_body

Modify the body sent when creating or updating posts.

##### Example

Add the tags `tag1` and `tag2` to every post.

```php
add_filter( 'wpdc_publish_body', function( $body, $remote_post_type ) {
   if ( 'create_post' === $remote_post_type ) {
      $body['tags'] = array( 'tag1', 'tag2' );
   }
   return $body;
}, 10, 2);

```

### Settings

[WP Discourse](https://github.com/discourse/wp-discourse) setting filters.

#### wpdc\_utilities\_options\_array

Modify any setting used by [WP Discourse](https://github.com/discourse/wp-discourse) at the time the settings are used in the code.

##### Example

Automatically set the Discourse URL to `https://wordpress.pavilion.tech`, no mater what is entered in the admin panel

```php
function filter_network_url_setting( $options) {
  $options["discourse_url"] = "https://wordpress.pavilion.tech";
  return $options;
}
add_filter( 'wpdc_utilities_options_array', 'filter_network_url_setting', 10, 1);

```

#### wpdc\_validate\_site\_\*

Filter any [WP Discourse](https://github.com/discourse/wp-discourse) network setting when using a mutlisite network, using the format: `wpdc_validate_site_[underscored_setting_name]`

##### Example

Automatically set Discourse URL to `https://wordpress.pavilion.tech` no mater what is entered in the admin panel

```php
function filter_multisite_network_url_setting( $value ) {
  return "https://wordpress.pavilion.tech";
}
add_filter( 'wpdc_validate_site_url', 'filter_multisite_network_url_setting', 10, 1);

```

#### wpdc\_category\_cache\_minutes

Category list cache duration. Default is 10 minutes.

##### Example

Change the cache period to 5 minutes.

```php
function change_category_cache_minutes( $default ) {
  return 5;
}
add_filter( 'wpdc_category_cache_minutes', 'change_category_cache_minutes', 10, 1);

```

### Comments

Filters relating to Discourse comments in Wordpress.

#### discourse\_post\_avatar\_template\_size

Set the size of the avatar used in comments. Available sizes are set in the Discourse `avatar sizes` site setting (default: `20|25|32|45|60|120`).

##### Example

```php
function filter_avatar_size( $size ) {
  return 32;
}
add_filter( 'discourse_post_avatar_template_size', 'filter_avatar_size', 10, 1);

```

#### wpdc\_comment\_body

Filter the comment body HTML.

##### Example

> [@Onebox looks horrible on Discourse Comments (below WP Post)](https://meta.discourse.org/t/onebox-looks-horrible-on-discourse-comments-below-wp-post/78364/8):
>
> Something like this should work to remove oneboxes from comments. It’s assuming that the libxml extension is loaded on your server. (It probably is.) add\_filter( 'wpdc\_comment\_body', 'wpdc\_custom\_comment\_body' ); function wpdc\_custom\_comment\_body( $content ) { // Allows parsing misformed html. Save the previous value of libxml\_use\_internal\_errors so that it can be restored. $use\_internal\_errors = libxml\_use\_internal\_errors( true ); $doc = new \DOMDocument( '1.0', 'utf-8' ); $doc-\>loadHTML(…

#### discourse\_participant\_avatar\_template\_size

Set the size of participant avatars (same options as `discourse_post_avatar_template_size`).

##### Example

```php
function filter_participant_avatar_size( $size ) {
  return 20;
}
add_filter( 'discourse_participant_avatar_template_size', 'filter_participant_avatar_size', 10, 1);

```

#### wpdc\_join\_discussion\_link\_text

Set the Join Discussion link text. Only use if Text Options are not appropriate.

##### Example

> [@Link to Discourse topics in Wordpress Without Displaying Comments](https://meta.discourse.org/t/how-to-link-to-discourse-topics-without-displaying-comments/88486):
>
> The WP Discourse plugin now has an option to link to Discourse topics without displaying comments underneath the post. This is enabled by selecting the Link to Comments Without Displaying Them option from the plugin’s Commenting settings tab. When you do that, you will find something like this displayed underneath your posts: [20%20PM] That doesn’t look great, but you can add some styles to the link by targeting the .wpdc-join-discussion-link class. [01%20PM] As comments are added to the Dis…

#### wpdc\_comment\_sync\_period

Set the sync period for comments in seconds. Default 600, i.e. 10 minutes.

##### Example

Set the sync period to 5 minutes.

```php
function custom_comment_sync_period( $minutes, $post_id ) {
  return 300;
}
add_filter( 'wpdc_comment_sync_period', 'custom_comment_sync_period', 10, 2);

```

#### wpdc\_single\_page\_comment\_number\_sync

Determine whether to retrieve discourse comment counts on the regular 10 minute schedule (return true) or a longer 24 hour schedule (return false), typically used for archive pages (can also be set using the `discourse_archive_page_sync_period`, see below).

##### Example

Sync comment counts every 10 minutes on posts and the front page and every 24 hours on other pages.

```php
function my_namespace_single_page_sync( $single_page, $post_id ) {
    return is_front_page() || is_single( $post_id );
}
add_filter( 'wpdc_single_page_comment_number_sync', 'my_namespace_single_page_sync', 10, 2 );

```

#### discourse\_archive\_page\_sync\_period

Set the archive page comment count sync period in seconds, Default `86400` (24 hours).

##### Example

> [@WP Discourse plugin tips and tricks](https://meta.discourse.org/t/wp-discourse-plugin-tips-and-tricks/50757):
>
> This topic has a number of tips and tricks for users of the [WP Discourse](https://github.com/discourse/wp-discourse) WordPress plugin. Adjust the comments\_number sync period for WordPress archive pages To get the correct number of Discourse comments for a post, the plugin needs to periodically make an HTTP request to Discourse to get the current comments number. On single pages this period is set to be no more often than every 10 minutes. For archive pages the period is set to no more than once every 24 hours. The reason for this is to a…

#### wpdc\_load\_comments\_template\_for\_user

Determine whether [WP Discourse](https://github.com/discourse/wp-discourse) comments template loads for a specific user.

##### Example

Don’t load the comments for a user with the id `1`.

```php
function load_comment_template_for_user( $load_template, $current_user, $post_id ) {
   return $current_user->ID !== 1;
add_filter( 'wpdc_load_comments_template_for_user', 'load_comment_template_for_user', 10, 3 );

```

#### wpdc\_comments\_count

Filter the comment count for specific posts.

##### Example

```php
function filter_comments_count( $count, $post_id, $discourse_post_id ) {
  ## Change the comment count 
}
add_filter( 'wpdc_comments_count', 'filter_comments_count`, 10, 3 );

```

#### discourse\_replies\_html

Change the html of the comments.

##### Example

> [@Customize the structure of WP Discourse templates](https://meta.discourse.org/t/wp-discourse-template-customization/50754):
>
> The [WP Discourse](https://github.com/discourse/wp-discourse) plugin uses HTML templates to add markup to Discourse comments for publishing on WordPress and to WordPress posts for publishing on Discourse. The text content of these templates can be customized on the plugin’s options page. To change the structure of a templates requires you to add a function to your theme’s functions.php file This #howto will explain how to do that. The templates In the plugin’s code, the templates are static functions that return a mixture of html and templ…

#### discourse\_no\_replies\_html

Change the html shown when there are no comments.

##### Example

> [@Customize the structure of WP Discourse templates](https://meta.discourse.org/t/wp-discourse-template-customization/50754):
>
> The [WP Discourse](https://github.com/discourse/wp-discourse) plugin uses HTML templates to add markup to Discourse comments for publishing on WordPress and to WordPress posts for publishing on Discourse. The text content of these templates can be customized on the plugin’s options page. To change the structure of a templates requires you to add a function to your theme’s functions.php file This #howto will explain how to do that. The templates In the plugin’s code, the templates are static functions that return a mixture of html and templ…

#### discourse\_no\_connection\_html

Change the html shown when there is no connection to Discourse.

##### Example

> [@Customize the structure of WP Discourse templates](https://meta.discourse.org/t/wp-discourse-template-customization/50754):
>
> The [WP Discourse](https://github.com/discourse/wp-discourse) plugin uses HTML templates to add markup to Discourse comments for publishing on WordPress and to WordPress posts for publishing on Discourse. The text content of these templates can be customized on the plugin’s options page. To change the structure of a templates requires you to add a function to your theme’s functions.php file This #howto will explain how to do that. The templates In the plugin’s code, the templates are static functions that return a mixture of html and templ…

#### discourse\_comment\_html

Change the html of individual comments

##### Example

> [@Customize the structure of WP Discourse templates](https://meta.discourse.org/t/wp-discourse-template-customization/50754):
>
> The [WP Discourse](https://github.com/discourse/wp-discourse) plugin uses HTML templates to add markup to Discourse comments for publishing on WordPress and to WordPress posts for publishing on Discourse. The text content of these templates can be customized on the plugin’s options page. To change the structure of a templates requires you to add a function to your theme’s functions.php file This #howto will explain how to do that. The templates In the plugin’s code, the templates are static functions that return a mixture of html and templ…

#### discourse\_participant\_html

Change the html of participants

##### Example

> [@Customize the structure of WP Discourse templates](https://meta.discourse.org/t/wp-discourse-template-customization/50754):
>
> The [WP Discourse](https://github.com/discourse/wp-discourse) plugin uses HTML templates to add markup to Discourse comments for publishing on WordPress and to WordPress posts for publishing on Discourse. The text content of these templates can be customized on the plugin’s options page. To change the structure of a templates requires you to add a function to your theme’s functions.php file This #howto will explain how to do that. The templates In the plugin’s code, the templates are static functions that return a mixture of html and templ…

#### discourse\_publish\_format\_html

Change the html of the wordpress post details in the content published to Discourse.

##### Example

> [@Customize the structure of WP Discourse templates](https://meta.discourse.org/t/wp-discourse-template-customization/50754):
>
> The [WP Discourse](https://github.com/discourse/wp-discourse) plugin uses HTML templates to add markup to Discourse comments for publishing on WordPress and to WordPress posts for publishing on Discourse. The text content of these templates can be customized on the plugin’s options page. To change the structure of a templates requires you to add a function to your theme’s functions.php file This #howto will explain how to do that. The templates In the plugin’s code, the templates are static functions that return a mixture of html and templ…

### [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Provider

[WP Discourse](https://github.com/discourse/wp-discourse) [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Provider filters.

#### discourse\_email\_verification

Determine whether Discourse needs to verify a user’s email when signing in with [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true).

##### Example

Require email verification if the user’s email is not from the `discourse.org` domain.

```php
function require_email_verification_for_external_domains( $require_activation, $user ) {
   $user_info = get_userdata( $user->ID );
   return strpos( $user_info->user_email, 'discourse.org' ) == false || $require_activation;

}
add_filter( 'discourse_email_verification', 'require_email_verification_for_external_domains`, 10, 2 );

```

#### wpdc\_email\_verification\_not\_verified

Filters the email verification result when false.

##### Example

Always return a true verification result if user’s email is in the `discourse.org` domain.

```php
function wpdc_custom_email_verification_not_verified( $verified, $user_id) {
   $user_info = get_userdata( $user_id );
   return strpos( $user_info->user_email, 'discourse.org' ) !== false || $verified;
}
add_filter( 'wpdc_email_verification_not_verified', 'wpdc_custom_email_verification_not_verified`, 10, 2 );

```

#### wpdc\_email\_verification\_verified

Filters the email verification result when true.

##### Example

Always return a false verification result if user’s email is not in the `discourse.org` domain.

```php
function wpdc_custom_email_verification_verified( $verified, $user_id ) {
   $user_info = get_userdata( $user_id );
   return strpos( $user_info->user_email, 'discourse.org' ) !== false || false;
}
add_filter( 'wpdc_email_verification_verified', 'wpdc_custom_email_verification_verified`, 10, 2 );

```

#### wpdc\_sso\_avatar\_url

Set the avatar url of user on Discourse when signing in with [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true).

##### Example

> [@Don't update avatar if default](https://meta.discourse.org/t/dont-update-avatar-if-default/118877/4):
>
> That will be the problem. The WP Discourse plugin has a 'wpdc\_sso\_avatar\_url' filter that can be used to filter the avatar URL that is passed to Discourse. The filter is passed two parameters: $avatar\_url, and $user\_id. To avoid sending the default avatar to Discourse, you’d need to add some code to your theme’s functions.php file that hooks into the filter and returns an empty string for users who have the default avatar: add\_filter( 'wpdc\_sso\_avatar\_url', 'wpdc\_custom\_sso\_avatar\_url', 10, 2…

#### wpdc\_sso\_params

Change the [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) params sent when a user is logging in.

##### Example

> [@Manage group membership in Discourse with WP Discourse SSO](https://meta.discourse.org/t/managing-discourse-group-membership-with-wp-discourse-sso/74724/81):
>
> The add\_user\_to\_discourse\_groups function will create a Discourse user if it doesn’t already exist. If that function is being called for your users, then you should be fine. Where you might run into problems will be for existing WordPress users who got their membership before the code was added to your site. If that is the case, you will probably need to add the users to Discourse groups through the SSO parameters that are passed to Discourse on login. To pass additional SSO parameters with th…

#### wpdc\_bypass\_sync\_sso

Prevent a user’s Wordpress account being synced with their Discourse account.

##### Example

> [@How to prevent some WP users from being able to login to Discourse](https://meta.discourse.org/t/how-to-prevent-some-wp-users-from-being-able-to-login-to-discourse/93234/2):
>
> There is a wpdc\_sso\_provider\_before\_sso\_redirect action hook that can be used to bypass SSO. The hook is fired right before the SSO login redirect to Discourse. It is passed the current WordPress user\_id and user object as parameters: [wp-discourse/lib/sso-provider/discourse-sso.php at main · discourse/wp-discourse · GitHub](https://github.com/discourse/wp-discourse/blob/master/lib/sso-provider/discourse-sso.php#L189). The basic idea is that if a user doesn’t meet the condition you have set for logging into Discourse, you redirect them to some page and then call exit. If the user does meet…

#### wp\_new\_user\_notification\_email\_admin

Filters the contents of the new user notification email sent to the site admin.

##### Example

```php
function wp_custom_new_user_notification_email_admin( $email_opts, $user, $blogname ) {
  ## Modify the email before it's sent to the admin.
}
add_filter( 'wp_new_user_notification_email_admin', 'wp_custom_new_user_notification_email_admin', 10, 2 );

```

#### wp\_new\_user\_notification\_email

Filters the contents of the new user notification email sent to the new user.

##### Example

```php
function wp_custom_new_user_notification_email( $email_opts, $user, $blogname ) {
  ## Modify the email before it's sent to the user.
}
add_filter( 'wp_new_user_notification_email', 'wp_custom_new_user_notification_email', 10, 2 );

```

### [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Client

[WP Discourse](https://github.com/discourse/wp-discourse) [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Client filters.

#### wpdc\_sso\_client\_add\_link\_buttons\_on\_profile

Determine whether to show a link Discourse account button on Wordpress user’s profiles.

##### Example

```php
function wpdc_dont_add_account_link_button( $add_button ) {
   return false;
}
add_filter( 'wpdc_sso_client_add_link_buttons_on_profile', 'wpdc_dont_add_account_link_button' );

```

#### wpdc\_sso\_client\_updated\_user

Filter Discourse user data before updating the Wordpress user.

##### Example

Make sure the first letter in the user’s name is capitalised.

```php
function wpdc_ensure_capitalisation( $updated_user, $query ) {
   $updated_user['name'] = ucfirst( $updated_user['name'] );
   return $updated_user;
}
add_filter( 'wpdc_sso_client_updated_user', 'wpdc_dont_update_admin_users' );

```

#### wpdc\_sso\_client\_redirect\_after\_login

Filter the return\_sso\_url used to redirect a user after a successful login.

##### Example

Always redirect users to the “dashboard” after a successful login.

```php
function wpdc_always_redirect_to_dashboard( $return_sso_url ) {
   return "/dashboard";
}
add_filter( 'wpdc_sso_client_redirect_after_login', 'wpdc_always_redirect_to_dashboard' );

```

#### wpdc\_sso\_client\_redirect\_after\_failed\_login

Filter the login\_url used to redirect a user after a failed login.

##### Example

Always redirect users to the “help” page after an unsuccessful login.

```php
function wpdc_always_redirect_to_help( $redirect_url ) {
  return "/help";
}
add_filter('wpdc_sso_client_redirect_after_failed_login', 'wpdc_always_redirect_to_help');

```

#### wpdc\_nonce\_life

Set the nonce timeout. Default is 10 minutes.

##### Example

Set the nonce timeout to 5 minutes.

```php
function wpdc_custom_nonce_life( $nonce_timeout ) {
  return 5;
}
add_filter( 'wpdc_nonce_life', 'wpdc_custom_nonce_life' );

```

#### wpdc\_sso\_client\_login\_anchor

Change the text displayed on the Discourse login link shown on the Wordpress login form.

##### Example

Change the text to “Login with your forum account”.

```php
function wpdc_custom_sso_client_login_anchor ( $login_text ) {
  return "Login with your forum account";
}
add_filter( 'wpdc_sso_client_login_anchor', 'wpdc_custom_sso_client_login_anchor' );

```

#### wpdc\_sso\_client\_login\_button

Change the Discourse login link shown on the Wordpress login form.

##### Example

Change the link element (adding classes and title attributes).

```php
function wpdc_custom_sso_client_login_button( $button, $sso_login_url, $link_options ) {
   return sprintf( '<a class="wpdc-sso-client-login-link" href="%s" class="discourse-login-link" title="%s">%s</a>', esc_url( $sso_login_url ), sanitize_text_field( "Click to login with Discourse" ), sanitize_text_field( "Login with Discourse" ) );
}
add_filter( 'wpdc_sso_client_login_button', 'wpdc_custom_sso_client_login_button', 10, 3 );

```

#### wpdc\_sso\_client\_query

Add an arbitrary string to the query that initializes [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) to handle page caching issues.

##### Example

> [@Wordpress SSO Expired nonce](https://meta.discourse.org/t/wordpress-sso-expired-nonce/105159/15):
>
> I’ve run into a similar issue on another site. In that case, the problem seems to be caused by Object Caching for anonymous users. As a temporary fix, could you try updating the wp-discourse plugin to version 1.8.0 (it’s in the WordPress repo now) and then adding the following code to your theme’s functions.php file? add\_filter('wpdc\_sso\_client\_query', 'wpdc\_custom\_sso\_client\_query' ); function wpdc\_custom\_sso\_client\_query() { return wp\_generate\_password( 12, false ); } All the code is do…

#### wpdc\_sso\_client\_redirect\_url

Filter redirect url used after user is logged in.

##### Example

Redirect users to a dashboard after login.

```php
function wpdc_custom_sso_client_redirect_url( $encoded_url, $unencoded_url ) {
  return "/dashboard";
}
add_filter( 'wpdc_sso_client_redirect_url', 'wpdc_custom_sso_client_redirect_url', 10, 2);

```

### Webhooks

[WP Discourse](https://github.com/discourse/wp-discourse) webhooks filters.

#### wpdc\_webhook\_get\_page\_by\_title\_post\_type

Change the post type used to match Wordpress posts and Discourse posts. Default is “post”.

```php
function wpdc_match_custom_posts( $post_type ) {
  return 'custom_post_type';
}
add_filter( 'wpdc_webhook_get_page_by_title_post_type', 'wpdc_match_custom_posts' );

```

#### wpdc\_use\_discourse\_user\_webhook

Determine whether to use the Discourse user webhook.

##### Example

> [@Cannot connect WP Discourse to Discourse forum](https://meta.discourse.org/t/cannot-connect-wp-discourse-to-discourse-forum/51119/20):
>
> The update-user route is only initialized on WordPress if you have enabled the SSO Provider option. If you haven’t enable the SSO Provider option, then returning a 404 error is the correct response. From the option’s description: This setting will only be activated if your site is functioning as the SSO provider for Discourse (this can be overridden by hooking into the ‘wpdc\_use\_discourse\_user\_webhook’ filter.) The update-user webhook was added to the plugin before the Create or Sync Disco…

### Utilities

Filters you can use with [WP Discourse](https://github.com/discourse/wp-discourse) utilities.

#### wpdc\_auto\_create\_user\_require\_activation

Determine when a user created by the utility `create_discourse_user` requires activation.

##### Example

> [@Integrate Discourse with MemberMouse](https://meta.discourse.org/t/wp-membermouse-discourse-integration-guide/79636):
>
> I maintain Discourse and MemberMouse running on 2 sites. Hopefully this guide will be helpful to people. Your exact specifications may differ from what my desired results were. This guide assumes that you are familiar with MemberMouse [hooks](http://support.membermouse.com/support/solutions/articles/9000020294-membermouse-wordpress-hooks), [filters](http://support.membermouse.com/support/solutions/articles/9000020317-membermouse-wordpress-filters) and MemberMouse [PHP interface](http://support.membermouse.com/support/solutions/articles/9000020212-using-the-php-interface). And also assumes you can comfortably add custom code to WordPress via functions.php or your own custom plugin. This guide below is what we added in order to: Activate/deactivate the user in Discourse depending on the…

## Actions

For a general explanation on how to use actions in Wordpress see

> **[Actions – Plugin Handbook | Developer.WordPress.org](https://developer.wordpress.org/plugins/hooks/actions/)**
>
> Actions are one of the two types of Hooks. They provide a way for running a function at a specific point in the execution of WordPress Core,…

### Settings

[WP Discourse](https://github.com/discourse/wp-discourse) settings actions.

#### wpdc\_options\_page\_append\_settings\_tabs

Can be used for adding sub-tabs to [WP discourse](https://github.com/discourse/wp-discourse) setting tabs.

##### Example

```php
function my_wp_discourse_setting_tab( $tab, $parent ) {
   ## Add your tab.
}
add_action('wpdc_options_page_append_settings_tabs', 'my_wp_discourse_setting_tab', 10, 2);

```

#### wpdc\_options\_page\_after\_settings\_tabs

Called after the settings tabs.

##### Example

```php
function my_custom_wpdc_tab( $tab, $parent ) {
   ## Run your custom tab code.
}
add_action( 'wpdc_options_page_after_settings_tabs', 'my_custom_wpdc_tab', 10, 2 );

```

#### wpdc\_options\_page\_after\_tab\_switch

Called after a [WP Discourse](https://github.com/discourse/wp-discourse) tab is switched.

##### Example

```php
function my_custom_wpdc_tab_switch( $tab ) {
   ## Run your custom code after tab switch
}
add_action( 'wpdc_options_page_after_tab_switch', 'my_custom_wpdc_tab_switch' );

```

#### wpdc\_options\_page\_after\_form

Called after the [WP Discourse](https://github.com/discourse/wp-discourse) settings form is rendered.

##### Example

```php
function my_custom_wpdc_options_page_after_form( $tab ) {
   ## Run your custom code after settings are rendered.
}
add_action( 'wpdc_options_page_after_form', 'my_custom_wpdc_options_page_after_form' );

```

### Comments

[WP Discourse](https://github.com/discourse/wp-discourse) Comments actions.

#### wp\_discourse\_after\_comments

Run after comments are synced.

##### Example

```php
function my_custom_comment_handling( $topic_id ) {
   ## Run your custom code after comments are synced.
}
add_action( 'wp_discourse_after_comments', 'my_custom_comment_handling' );

```

### [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Provider

[WP Discourse](https://github.com/discourse/wp-discourse) [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Provider actions.

#### wpdc\_after\_sync\_sso

Run after a user is synced via [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true).

##### Example

> [@Accessing Discourse user avatar in WP functions.php](https://meta.discourse.org/t/accessing-discourse-user-avatar-in-wp-functions-php/95390/5):
>
> In the latest version of the plugin (1.7.5), I’ve added a wpdc\_after\_sync\_sso action hook that you can use to retrieve information from the Discourse user object. This hook will only work if you are using your WordPress site as the SSO Provider for Discourse, and have also enabled the Create or Sync Discourse Users on Login option. Next week I will look into adding a similar hook for when WordPress is functioning as the SSO Client. Any function that you hook into wpdc\_after\_sync\_sso will be pa…

#### wpdc\_sso\_provider\_before\_create\_user

Run before the Discourse user is created.

##### Example

```php
function my_custom_user_handling( $user_login, $user ) {
   ## Run your custom code before a user is created
}
add_action( 'wpdc_sso_provider_before_create_user', 'my_custom_user_handling', 10, 2 );

```

#### wpdc\_sso\_before\_login\_redirect

Run before a user is redirected to Wordpress login.

##### Example

```php
function my_custom_login_handling( $user_login, $user ) {
   ## Run your custom code before a user is redirected to login.
}
add_action( 'wpdc_sso_before_login_redirect', 'my_custom_login_handling', 10, 2 );

```

#### wpdc\_sso\_provider\_before\_sso\_redirect

Run before a user is redirected back to Discourse after Wordpress login.

##### Example

> [@How to prevent some WP users from being able to login to Discourse](https://meta.discourse.org/t/how-to-prevent-some-wp-users-from-being-able-to-login-to-discourse/93234/2):
>
> There is a wpdc\_sso\_provider\_before\_sso\_redirect action hook that can be used to bypass SSO. The hook is fired right before the SSO login redirect to Discourse. It is passed the current WordPress user\_id and user object as parameters: [wp-discourse/lib/sso-provider/discourse-sso.php at main · discourse/wp-discourse · GitHub](https://github.com/discourse/wp-discourse/blob/master/lib/sso-provider/discourse-sso.php#L189). The basic idea is that if a user doesn’t meet the condition you have set for logging into Discourse, you redirect them to some page and then call exit. If the user does meet…

### [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Client

[WP Discourse](https://github.com/discourse/wp-discourse) [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) Client actions.

#### wpdc\_sso\_client\_after\_login\_link

Run after Discourse login link is rendered.

##### Example

```php
function my_custom_login_html() {
   ## Add your custom html
}
add_action( 'wpdc_sso_client_after_login_link', 'my_custom_login_html' );

```

#### wpdc\_sso\_client\_after\_create\_user

Run after a Wordpress user is created.

##### Example

```php
function wpdc_modify_wordpress_user( $user_id ) {
   ## Modify the Wordpress user.
}
add_action( 'wpdc_sso_client_after_create_user', 'wpdc_modify_wordpress_user' );

```

#### wpdc\_after\_sso\_client\_user\_update

Run after a Wordpress user is updated.

##### Example

```php
function wpdc_modify_wordpress_user_after_update( $user_id, $query ) {
   ## Modify the Wordpress user.
}
add_action( 'wpdc_after_sso_client_user_update', 'wpdc_modify_wordpress_user_after_update' );

```

### Webhooks

[WP Discourse](https://github.com/discourse/wp-discourse) Webhook actions.

#### wpdc\_before\_webhook\_post\_update

Run before the update topic webhook data received from Discourse is processed.

##### Example

```php
function wpdc_before_webhook_post_update_changes( $json ) {
   ## Use the JSON received from Discourse.
}
add_action( 'wpdc_before_webhook_post_update', 'wpdc_modify_wordpress_user_after_update' );

```

#### wpdc\_webhook\_after\_get\_page\_by\_title

Run after posts are matched by title for the update topic webhook.

##### Example

```php
function wpdc_webhook_custom_after_get_page_by_title( $title ) {
  ## Do something with the matched titled.
}
add_action( 'wpdc_webhook_after_get_page_by_title', 'wpdc_webhook_custom_after_get_page_by_title' );

```

#### wpdc\_webhook\_user\_created

Run after a user is created by the update user webhook.

##### Example

```php
function wpdc_webhook_user_created_action( $discourse_user ) {
  ## Do something with the discourse user.
}
add_action( 'wpdc_webhook_user_created', 'wpdc_webhook_user_created_action' );

```

#### wpdc\_webhook\_user\_updated

Run after a user is updated by the update user webhook.

##### Example

```php
function wpdc_webhook_user_updated_action( $discourse_user ) {
  ## Do something with the discourse user.
}
add_action( 'wpdc_webhook_user_updated', 'wpdc_webhook_user_updated_action' );

```

#### wpdc\_webhook\_before\_update\_user\_data

Run before Wordpress user data is updated from Discourse user data by the update user webhook.

##### Example

```php
function wpdc_webhook_before_update_user_data_action( $wordpress_user, $discourse_user, $event_type ) {
  ## Do something with the wordpress or discourse users.
}
add_action( 'wpdc_webhook_before_update_user_data', 'wpdc_webhook_before_update_user_data_action' );

```
