WP Discourse のフィルターとアクション

これらは WP Discourse プラグインが提供するすべてのアクションとフィルターです。例は参考用です。本番環境で使用する前に、ご自身のインスタンスでテストおよび検証してください。

フィルター

WordPress でフィルターを使用する方法についての一般的な説明は以下をご覧ください。

公開

WP Discourse の公開に関するフィルター。

discourse_post_types_to_publish

「公開する投稿タイプ」設定の投稿タイプリストに表示される投稿タイプをフィルターします。ただし、これによって Discourse への投稿タイプの公開の有効化または無効化は行われません。

「投稿」タイプのみを表示する場合:

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

wpdc_publish_private_post

非公開の WordPress 投稿を Discourse に公開します。

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

特定の投稿を Discourse に公開するかどうかを判定します。

wp_discourse_before_xmlrpc_publish

特定の投稿を xmlrpc を使用して Discourse に公開するかどうかを判定します。

タイトルに「apples」を含む xmlrpc 投稿は公開しない。

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

Discourse トピックのタイトルを変更します。

wp_discourse_excerpt

Discourse に送信される投稿の内容を変更します。

wpdc_publish_post_category

投稿が公開されるカテゴリを変更します。

常に投稿を「Site Feedback」カテゴリに公開する。

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

WordPress 投稿の作成または編集時に、WP Discourse サイドバーの「カテゴリ」設定に表示されるカテゴリをフィルターします。

「Site Feedback」カテゴリのみを表示する場合:

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

公開された投稿が Discourse でリストから除外されるかどうかを判定します。

投稿が WordPress 分類「knowledge_base」の用語「document」を含む場合に、リストから除外して公開する。

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

Discourse 投稿の著者として設定される Discourse ユーザー名を設定します。

Discourse に公開されるすべての投稿の著者を「angus」にする。

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

wpdc_publish_body

投稿の作成または更新時に送信される本文を変更します。

すべての投稿に tag1tag2 のタグを追加する。

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

設定

WP Discourse の設定フィルター。

wpdc_utilities_options_array

コード内で設定が使用される時点で、WP Discourse が使用する任意の設定を変更します。

管理画面に入力された内容に関係なく、Discourse URL を https://wordpress.pavilion.tech に自動設定する。

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

マルチサイトネットワークを使用する際に、wpdc_validate_site_[アンダースコア付き設定名] という形式で、WP Discourse のネットワーク設定をフィルターします。

管理画面に入力された内容に関係なく、Discourse URL を https://wordpress.pavilion.tech に自動設定する。

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

カテゴリリストのキャッシュ期間。デフォルトは 10 分です。

キャッシュ期間を 5 分に変更する。

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

コメント

WordPress 内の Discourse コメントに関連するフィルター。

discourse_post_avatar_template_size

コメントで使用されるアバターのサイズを設定します。利用可能なサイズは Discourse の「avatar sizes」サイト設定で設定されます(デフォルト: 20|25|32|45|60|120)。

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

wpdc_comment_body

コメント本文の HTML をフィルターします。

discourse_participant_avatar_template_size

参加者のアバターサイズを設定します(discourse_post_avatar_template_size と同じオプション)。

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

「ディスカッションに参加する」リンクのテキストを設定します。テキストオプションが適切でない場合のみ使用してください。

wpdc_comment_sync_period

コメントの同期間隔を秒単位で設定します。デフォルトは 600(つまり 10 分)です。

同期間隔を 5 分に設定する。

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

通常の 10 分スケジュールで Discourse コメント数を取得するかどうか(true を返す)、またはアーカイブページなどで通常使用される 24 時間スケジュールで取得するかどうか(false を返す)を判定します(discourse_archive_page_sync_period を使用して設定することもできます。詳細は後述)。

投稿とフロントページでは 10 分ごとにコメント数を同期し、他のページでは 24 時間ごとに同期する。

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

アーカイブページのコメント数同期期間を秒単位で設定します。デフォルトは 86400(24 時間)です。

wpdc_load_comments_template_for_user

特定のユーザーに対して WP Discourse のコメントテンプレートを読み込むかどうかを判定します。

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

特定の投稿のコメント数をフィルターします。

function filter_comments_count( $count, $post_id, $discourse_post_id ) {
  ## コメント数を変更
}
add_filter( 'wpdc_comments_count', 'filter_comments_count`, 10, 3 );

discourse_replies_html

コメントの HTML を変更します。

discourse_no_replies_html

コメントがない場合に表示される HTML を変更します。

discourse_no_connection_html

Discourse に接続できない場合に表示される HTML を変更します。

discourse_comment_html

個別のコメントの HTML を変更します

discourse_participant_html

参加者の HTML を変更します

discourse_publish_format_html

Discourse に公開されるコンテンツ内の WordPress 投稿詳細の HTML を変更します。

DiscourseConnect プロバイダー

WP Discourse DiscourseConnect プロバイダーフィルター。

discourse_email_verification

DiscourseConnect でサインインする際に、Discourse がユーザーのメールを検証する必要があるかどうかを判定します。

ユーザーのメールが discourse.org ドメイン以外の場合、メール検証を必須にする。

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

検証結果が false の場合のメール検証結果をフィルターします。

ユーザーのメールが discourse.org ドメインの場合、常に検証結果を true にする。

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

検証結果が true の場合のメール検証結果をフィルターします。

ユーザーのメールが discourse.org ドメイン以外の場合、常に検証結果を false にする。

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

DiscourseConnect でサインインする際に、Discourse 上のユーザーのアバター URL を設定します。

wpdc_sso_params

ユーザーがログインする際に送信される DiscourseConnect パラメータを変更します。

wpdc_bypass_sync_sso

ユーザーの WordPress アカウントと Discourse アカウントの同期を防止します。

wp_new_user_notification_email_admin

サイト管理者に送信される新規ユーザー通知メールの内容をフィルターします。

function wp_custom_new_user_notification_email_admin(  $email_opts, $user, $blogname ) {
  ## 管理者に送信される前にメールを変更。
}
add_filter( 'wp_new_user_notification_email_admin', 'wp_custom_new_user_notification_email_admin', 10, 2 );

wp_new_user_notification_email

新規ユーザーに送信される新規ユーザー通知メールの内容をフィルターします。

function wp_custom_new_user_notification_email(  $email_opts, $user, $blogname ) {
  ## ユーザーに送信される前にメールを変更。
}
add_filter( 'wp_new_user_notification_email', 'wp_custom_new_user_notification_email', 10, 2 );

DiscourseConnect クライアント

WP Discourse DiscourseConnect クライアントフィルター。

wpdc_sso_client_add_link_buttons_on_profile

WordPress ユーザーのプロフィールに Discourse アカウントリンクボタンを表示するかどうかを判定します。

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

WordPress ユーザーを更新する前に Discourse ユーザーデータをフィルターします。

ユーザー名の最初の文字を大文字にする。

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

正常なログイン後にユーザーをリダイレクトするために使用される return_sso_url をフィルターします。

常にログイン後にユーザーを「ダッシュボード」にリダイレクトする。

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

失敗したログイン後にユーザーをリダイレクトするために使用される login_url をフィルターします。

常にログイン失敗後にユーザーを「ヘルプ」ページにリダイレクトする。

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

Nonce のタイムアウト時間を設定します。デフォルトは 10 分です。

Nonce のタイムアウト時間を 5 分に設定する。

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

wpdc_sso_client_login_anchor

WordPress ログインフォームに表示される Discourse ログインリンクの表示テキストを変更します。

テキストを「フォーラムアカウントでログイン」に変更する。

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

WordPress ログインフォームに表示される Discourse ログインリンクを変更します。

リンク要素を変更する(クラスと title 属性の追加)。

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

ページキャッシュの問題に対処するために、DiscourseConnect を初期化するクエリに任意の文字列を追加します。

wpdc_sso_client_redirect_url

ユーザーがログインした後に使用されるリダイレクト URL をフィルターします。

ログイン後にユーザーをダッシュボードにリダイレクトする。

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

ウェブフック

WP Discourse ウェブフックフィルター。

wpdc_webhook_get_page_by_title_post_type

WordPress 投稿と Discourse 投稿を一致させるために使用される投稿タイプを変更します。デフォルトは「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

Discourse ユーザーウェブフックを使用するかどうかを判定します。

ユーティリティ

WP Discourse ユーティリティで使用できるフィルター。

wpdc_auto_create_user_require_activation

ユーティリティ create_discourse_user によって作成されたユーザーがアクティベーションを必要とするタイミングを判定します。

アクション

WordPress でアクションを使用する方法についての一般的な説明は以下をご覧ください。

設定

WP Discourse 設定アクション。

wpdc_options_page_append_settings_tabs

WP Discourse 設定タブにサブタブを追加するために使用できます。

function my_wp_discourse_setting_tab( $tab, $parent ) {
   ## タブを追加。
}
add_action('wpdc_options_page_append_settings_tabs', 'my_wp_discourse_setting_tab', 10, 2);

wpdc_options_page_after_settings_tabs

設定タブの後に呼び出されます。

function my_custom_wpdc_tab( $tab, $parent ) {
   ## カスタムタブコードを実行。
}
add_action( 'wpdc_options_page_after_settings_tabs', 'my_custom_wpdc_tab', 10, 2 );

wpdc_options_page_after_tab_switch

WP Discourse のタブが切り替えられた後に呼び出されます。

function my_custom_wpdc_tab_switch( $tab ) {
   ## タブ切り替え後にカスタムコードを実行
}
add_action( 'wpdc_options_page_after_tab_switch', 'my_custom_wpdc_tab_switch' );

wpdc_options_page_after_form

WP Discourse 設定フォームがレンダリングされた後に呼び出されます。

function my_custom_wpdc_options_page_after_form( $tab ) {
   ## 設定がレンダリングされた後にカスタムコードを実行。
}
add_action( 'wpdc_options_page_after_form', 'my_custom_wpdc_options_page_after_form' );

コメント

WP Discourse コメントアクション。

wp_discourse_after_comments

コメントが同期された後に実行されます。

function my_custom_comment_handling( $topic_id ) {
   ## コメント同期後にカスタムコードを実行。
}
add_action( 'wp_discourse_after_comments', 'my_custom_comment_handling' );

DiscourseConnect プロバイダー

WP Discourse DiscourseConnect プロバイダーアクション。

wpdc_after_sync_sso

ユーザーが DiscourseConnect を介して同期された後に実行されます。

wpdc_sso_provider_before_create_user

Discourse ユーザーが作成される前に実行されます。

function my_custom_user_handling( $user_login, $user ) {
   ## ユーザー作成前にカスタムコードを実行
}
add_action( 'wpdc_sso_provider_before_create_user', 'my_custom_user_handling', 10, 2 );

wpdc_sso_before_login_redirect

ユーザーが WordPress ログインにリダイレクトされる前に実行されます。

function my_custom_login_handling( $user_login, $user ) {
   ## ログインリダイレクト前にカスタムコードを実行。
}
add_action( 'wpdc_sso_before_login_redirect', 'my_custom_login_handling', 10, 2 );

wpdc_sso_provider_before_sso_redirect

WordPress ログイン後にユーザーが Discourse に戻される前に実行されます。

DiscourseConnect クライアント

WP Discourse DiscourseConnect クライアントアクション。

wpdc_sso_client_after_login_link

Discourse ログインリンクがレンダリングされた後に実行されます。

function my_custom_login_html() {
   ## カスタム HTML を追加
}
add_action( 'wpdc_sso_client_after_login_link', 'my_custom_login_html' );

wpdc_sso_client_after_create_user

WordPress ユーザーが作成された後に実行されます。

function wpdc_modify_wordpress_user( $user_id ) {
   ## WordPress ユーザーを変更。
}
add_action( 'wpdc_sso_client_after_create_user', 'wpdc_modify_wordpress_user' );

wpdc_after_sso_client_user_update

WordPress ユーザーが更新された後に実行されます。

function wpdc_modify_wordpress_user_after_update( $user_id, $query ) {
   ## WordPress ユーザーを変更。
}
add_action( 'wpdc_after_sso_client_user_update', 'wpdc_modify_wordpress_user_after_update' );

ウェブフック

WP Discourse ウェブフックアクション。

wpdc_before_webhook_post_update

Discourse から受信したトピック更新ウェブフックデータが処理される前に実行されます。

function wpdc_before_webhook_post_update_changes( $json ) {
   ## Discourse から受信した JSON を使用。
}
add_action( 'wpdc_before_webhook_post_update', 'wpdc_modify_wordpress_user_after_update' );

wpdc_webhook_after_get_page_by_title

トピック更新ウェブフックのためにタイトルで投稿が一致した後に実行されます。

function wpdc_webhook_custom_after_get_page_by_title( $title ) {
  ## 一致したタイトルに対して何かを行う。
}
add_action( 'wpdc_webhook_after_get_page_by_title', 'wpdc_webhook_custom_after_get_page_by_title' );

wpdc_webhook_user_created

ユーザー更新ウェブフックによってユーザーが作成された後に実行されます。

function wpdc_webhook_user_created_action( $discourse_user ) {
  ## Discourse ユーザーに対して何かを行う。
}
add_action( 'wpdc_webhook_user_created', 'wpdc_webhook_user_created_action' );

wpdc_webhook_user_updated

ユーザー更新ウェブフックによってユーザーが更新された後に実行されます。

function wpdc_webhook_user_updated_action( $discourse_user ) {
  ## Discourse ユーザーに対して何かを行う。
}
add_action( 'wpdc_webhook_user_updated', 'wpdc_webhook_user_updated_action' );

wpdc_webhook_before_update_user_data

ユーザー更新ウェブフックによって Discourse ユーザーデータから WordPress ユーザーデータが更新される前に実行されます。

function wpdc_webhook_before_update_user_data_action( $wordpress_user, $discourse_user, $event_type ) {
  ## WordPress または Discourse ユーザーに対して何かを行う。
}
add_action( 'wpdc_webhook_before_update_user_data', 'wpdc_webhook_before_update_user_data_action' );
「いいね!」 5