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

更改文章发布的分类。

示例

始终将文章发布到“站点反馈”分类。

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 侧边栏中“分类”设置下显示的分类。

示例

仅显示“站点反馈”分类:

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 上是否为未列出状态。

示例

如果文章在“knowledge_base”WordPress 分类法中包含术语“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_*

在使用多站点网络时,过滤任何 WP Discourse 网络设置,格式为:wpdc_validate_site_[下划线设置名称]

示例

无论管理员面板中输入什么,自动将 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 分钟计划(返回 true)或更长的 24 小时计划(返回 false)检索 Discourse 评论数量,通常用于存档页面(也可以使用 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 登录链接。

示例

更改链接元素(添加类和标题属性)。

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

Webhooks

WP Discourse Webhook 过滤器。

wpdc_webhook_get_page_by_title_post_type

更改用于匹配 WordPress 文章和 Discourse 文章的文章类型。默认为“文章”。

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 用户 Webhook。

示例

工具

您可以与 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' );

Webhooks

WP Discourse Webhook 操作。

wpdc_before_webhook_post_update

在处理从 Discourse 接收到的更新主题 Webhook 数据之前运行。

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

在通过标题匹配文章以进行更新主题 Webhook 后运行。

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

在通过更新用户 Webhook 创建用户后运行。

示例
function wpdc_webhook_user_created_action( $discourse_user ) {
  ## 对 Discourse 用户执行某些操作。
}
add_action( 'wpdc_webhook_user_created', 'wpdc_webhook_user_created_action' );

wpdc_webhook_user_updated

在通过更新用户 Webhook 更新用户后运行。

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

在通过更新用户 Webhook 从 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 个赞