我正在建立一个使用WordPress插件Thrive评论的网站。
它允许人们按排名靠前、最新或最老的方式对评论进行排序。
深入研究代码,我可以看到Top Rated是基于默认WordPress进行排序的comment_karma
领域
我想更改它,以便根据我创建的名为confidence_rank_cached
.
Thrive Comments插件有一个名为tcm_get_localization_parameters
我认为这是将PHP数据发送到前端,以便JavaScript排序脚本可以使用它。
以下是该功能的完整副本:
/**
* Get params to be used in javascript
*
* @return array
*/
public function tcm_get_localization_parameters() {
$post = $this->post_for_localization();
$post_id = empty( $post[\'ID\'] ) ? 0 : $post[\'ID\'];
$localization = array(
\'current_user\' => tcmh()->tcm_get_current_user(),
\'translations\' => include tcm()->plugin_path( \'includes/i18n.php\' ),
\'nonce\' => $this->create_nonce(),
\'routes\' => array(
\'comments\' => tcm()->tcm_get_route_url( \'comments\' ),
\'gravatar\' => tcm()->tcm_get_route_url( \'comments\' ) . \'/gravatar\',
\'live_update\' => tcm()->tcm_get_route_url( \'comments\' ) . \'/live_update\',
\'update_post_subscriber\' => tcm()->tcm_get_route_url( \'comments\' ) . \'/update_post_subscriber\',
\'generate_nonce\' => admin_url( \'admin-ajax.php\' ),
),
\'post\' => $post,
\'related_posts\' => tcmc()->get_related_posts( Thrive_Comments_Constants::TCM_NO_RELATED_POSTS, $args = array() ),
\'const\' => array(
\'toast_timeout\' => Thrive_Comments_Constants::TCM_TOAST_TIMEOUT, // Not sure if we really need this.
\'wp_content\' => rtrim( WP_CONTENT_URL, \'/\' ) . \'/\',
\'ajax_dash\' => array( Thrive_Comments_Constants::TCM_AJAX_DASH ),
\'site_url\' => get_site_url(),
\'post_url\' => apply_filters( \'tcm_post_url\', get_permalink() ),
\'moderation\' => array(
\'approve\' => Thrive_Comments_Constants::TCM_APPROVE,
\'unapprove\' => Thrive_Comments_Constants::TCM_UNAPPROVE,
\'spam\' => Thrive_Comments_Constants::TCM_SPAM,
\'unspam\' => Thrive_Comments_Constants::TCM_UNSPAM,
\'trash\' => Thrive_Comments_Constants::TCM_TRASH,
\'untrash\' => Thrive_Comments_Constants::TCM_UNTRASH,
\'unreplied\' => Thrive_Comments_Constants::TCM_UNREPLIED,
\'tcm_delegate\' => Thrive_Comments_Constants::TCM_DELEGATE,
\'tcm_featured\' => Thrive_Comments_Constants::TCM_FEATURED,
\'tcm_keyboard_tooltip\' => Thrive_Comments_Constants::TCM_KEYBOARD_TOOLTIP,
\'featured\' => Thrive_Comments_Constants::TCM_FEATURE_VALUE,
\'not_featured\' => Thrive_Comments_Constants::TCM_NOT_FEATURE_VALUE,
),
),
\'settings\' => tcms()->tcm_get_settings(),
\'close_comments\' => tcms()->close_comments( $post_id ) || ! $this->tcm_show_comments(),
\'sorting\' => tcms()->get_comment_sorting(),
\'tcm_customize_labels\' => tcms()->tcm_get_setting_by_name( Thrive_Comments_Constants::TCM_LABELS_KEY ),
\'tcm_social_apis\' => array(
\'facebook\' => Thrive_Dash_List_Manager::credentials( \'facebook\' ),
\'google\' => Thrive_Dash_List_Manager::credentials( \'google\' ),
),
\'email_services\' => tcamh()->get_email_services(),
\'tcm_accent_color\' => tcms()->tcm_get_setting_by_name( Thrive_Comments_Constants::TCM_ACCENT_COLOR ),
\'has_plugin_cache\' => tve_dash_detect_cache_plugin(),
\'default_author_picture_url\' => tcmh()->get_picture_url(),
);
/**
* Filter for adding extra params for comments localization in fronted
*
* @param array $localization the already built localization by TC
*/
return apply_filters( \'tcm_comments_localization\', $localization );
}
查看函数,您将看到它正在返回
apply_filters( \'tcm_comments_localization\', $localization );
.
所以我想我可以在里面装一个过滤器functions.php
我的孩子主题,使其按我的confidence_rank_cached
注释元数据字段,而不是默认的WordPresscomment_karma
领域
这是我在里面使用的代码functions.php
:
function custom_change_top_ranking_comment_sort( $localization ) {
$localization[\'sorting\'][\'sort_field\'] = \'confidence_rank_cached\';
return $localization;
}
add_filter( \'tcm_comments_localization\', \'custom_change_top_ranking_comment_sort\', 10, 1 );
添加代码并检查加载帖子的源代码后,我可以看到JavaScript
sort_field
正在使用
confidence_rank_cached
而不是
comment_karma
.
这很好,但排序工作不正常。当选择Top Rated作为排序选项时,它不会按我的confidence_rank_cached
元数据值。
我肯定我在这里的某个地方错过了一步。
我怀疑它不起作用,因为最初的JavaScript排序基于comment_karma
它来自主注释数据库表,现在我正在尝试使用它confidence_rank_cached
来自注释元数据表。。。但我对JavaScript和AJAX等还没有足够的理解来理解它。
有人能帮我看一下吗?
这里有一个实时的临时站点,您可以看到它的运行情况。我已在中禁用自定义代码functions.php
因此,您可以看到它的工作原理:https://wholesale-discussion.flywheelsites.com/new-post-test/
由于它还没有上线,您将需要这些登录详细信息来查看帖子。。。
用户名:explode密码:boomboom如果有帮助,这里有一份Thrive Comments前端JavaScript文件的副本。我将其缩小以便于阅读:https://pastebin.com/6NznyLKV