WP Discourse Filters and Actions

These are all actions and filters provided by the 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

Publishing

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:

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

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

wpdc_publish_after_save

Whether a specific post should be published to Discourse.

Example

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.

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

wp_discourse_excerpt

Modify the content of posts being sent to Discourse.

Example

wpdc_publish_post_category

Change the category to which the post is published.

Example

Always publish posts to the Site Feedback category.

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 sidebar when creating or editing a Wordpress post.

Example

Only show the Site Feedback category:

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.

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.

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.

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 setting filters.

wpdc_utilities_options_array

Modify any setting used by 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

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 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

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.

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
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

discourse_participant_avatar_template_size

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

Example
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

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.

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.

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

wpdc_load_comments_template_for_user

Determine whether WP Discourse comments template loads for a specific user.

Example

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

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
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

discourse_no_replies_html

Change the html shown when there are no comments.

Example

discourse_no_connection_html

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

Example

discourse_comment_html

Change the html of individual comments

Example

discourse_participant_html

Change the html of participants

Example

discourse_publish_format_html

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

Example

DiscourseConnect Provider

WP Discourse DiscourseConnect Provider filters.

discourse_email_verification

Determine whether Discourse needs to verify a user’s email when signing in with DiscourseConnect.

Example

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

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.

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.

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.

Example

wpdc_sso_params

Change the DiscourseConnect params sent when a user is logging in.

Example

wpdc_bypass_sync_sso

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

Example

wp_new_user_notification_email_admin

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

Example
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
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 Client

WP Discourse DiscourseConnect 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
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.

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.

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.

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.

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”.

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).

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 to handle page caching issues.

Example

wpdc_sso_client_redirect_url

Filter redirect url used after user is logged in.

Example

Redirect users to a dashboard after login.

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 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”.

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

Utilities

Filters you can use with WP Discourse utilities.

wpdc_auto_create_user_require_activation

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

Example

Actions

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

Settings

WP Discourse settings actions.

wpdc_options_page_append_settings_tabs

Can be used for adding sub-tabs to WP discourse setting tabs.

Example
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
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 tab is switched.

Example
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 settings form is rendered.

Example
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 Comments actions.

wp_discourse_after_comments

Run after comments are synced.

Example
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 Provider

WP Discourse DiscourseConnect Provider actions.

wpdc_after_sync_sso

Run after a user is synced via DiscourseConnect.

Example

wpdc_sso_provider_before_create_user

Run before the Discourse user is created.

Example
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
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

DiscourseConnect Client

WP Discourse DiscourseConnect Client actions.

wpdc_sso_client_after_login_link

Run after Discourse login link is rendered.

Example
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
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
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 Webhook actions.

wpdc_before_webhook_post_update

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

Example
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
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
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
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
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' );
5 Likes