Php 添加用户';用户元数据中的注释计数

Php 添加用户';用户元数据中的注释计数,php,wordpress,metadata,Php,Wordpress,Metadata,我使用以下代码获取用户在其他用户帖子上发表的总评论(不包括用户自己帖子上发表的评论): 现在,我想用这个值更新所有用户的。因此,$meta\u值将是评论总数(如上所示),而$meta\u键可以是自定义用户\u评论 如何为所有用户添加此元数据?以及如何使其在用户对其他用户的帖子发表评论时更新值?使用插件: 在插件激活时修改所有用户元 检查每个张贴的评论,必要时更新 您必须使用每个函数中提供的用户ID来填补空白: <?php /** * Plugin Name: (SO) Comment

我使用以下代码获取用户在其他用户帖子上发表的总评论(不包括用户自己帖子上发表的评论):

现在,我想用这个值更新所有用户的。因此,
$meta\u值
将是评论总数(如上所示),而
$meta\u键
可以是
自定义用户\u评论

如何为所有用户添加此元数据?以及如何使其在用户对其他用户的帖子发表评论时更新值?

使用插件:

  • 在插件激活时修改所有用户元
  • 检查每个张贴的评论,必要时更新
您必须使用每个函数中提供的用户ID来填补空白:

<?php
/**
 * Plugin Name: (SO) Comment to User Meta
 * Plugin URI: http://stackoverflow.com/a/25464099/1287812
 * Author: brasofilo
 */

/**
 * Modify all user meta on plugin activation
 */
register_activation_hook(   __FILE__, function() 
{
    # Security checks
    if ( ! current_user_can( 'activate_plugins' ) )
        return;

    $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
    check_admin_referer( "activate-plugin_{$plugin}" );

    # ITERATE THROUGH USERS AND UPDATE METADATA
    foreach( get_users() as $user ) 
    {
        # http://codex.wordpress.org/Function_Reference/update_user_meta
        #
        $user_comment_count = grab_custom_user_comments_count( $user->data->ID );
    }
});

/**
 * Update user meta on comment post
 * looks like this hook only runs for logged users, checking for that just to be safe
 *
 * @param $id           Comment ID
 * @param $comment_obj  Full comment details
 */
add_action( 'wp_insert_comment', function( $id, $comment_obj ) 
{
    # Only on backend and for logged users
    if( is_admin() || !is_user_logged_in() )
        return; 

    # CURRENT LOGGED USER
    global $user_ID;
    get_currentuserinfo();

    # POST AUTHOR
    # http://codex.wordpress.org/Function_Reference/update_user_meta
    #
    $user_comment_count = grab_custom_user_comments_count( $comment_obj->user_id );
    // You can compare $user_ID and $comment_obj->user_id here
}, 10, 2 );

谢谢你的回答。如果可能的话,我需要进一步的澄清。首先,不推荐使用update\u usermeta,需要使用update\u user\u meta。请添加用户ID,以便我了解如何插入该ID?此外,该值是否仅在用户在其他人的帖子上发表评论时更新?因此,如果用户在自己的帖子上发表评论,值不会改变。是的,我从谷歌复制了URL,正确的是
update\u user\u meta
。我已经用一些用法示例和更多注释更新了答案,以澄清您的疑问。我的朋友,您为我节省了很多时间的麻烦,非常感谢!
<?php
/**
 * Plugin Name: (SO) Comment to User Meta
 * Plugin URI: http://stackoverflow.com/a/25464099/1287812
 * Author: brasofilo
 */

/**
 * Modify all user meta on plugin activation
 */
register_activation_hook(   __FILE__, function() 
{
    # Security checks
    if ( ! current_user_can( 'activate_plugins' ) )
        return;

    $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
    check_admin_referer( "activate-plugin_{$plugin}" );

    # ITERATE THROUGH USERS AND UPDATE METADATA
    foreach( get_users() as $user ) 
    {
        # http://codex.wordpress.org/Function_Reference/update_user_meta
        #
        $user_comment_count = grab_custom_user_comments_count( $user->data->ID );
    }
});

/**
 * Update user meta on comment post
 * looks like this hook only runs for logged users, checking for that just to be safe
 *
 * @param $id           Comment ID
 * @param $comment_obj  Full comment details
 */
add_action( 'wp_insert_comment', function( $id, $comment_obj ) 
{
    # Only on backend and for logged users
    if( is_admin() || !is_user_logged_in() )
        return; 

    # CURRENT LOGGED USER
    global $user_ID;
    get_currentuserinfo();

    # POST AUTHOR
    # http://codex.wordpress.org/Function_Reference/update_user_meta
    #
    $user_comment_count = grab_custom_user_comments_count( $comment_obj->user_id );
    // You can compare $user_ID and $comment_obj->user_id here
}, 10, 2 );
stdClass Object
(
    [comment_ID] => 36
    [comment_post_ID] => 885
    [comment_author] => nicename
    [comment_author_email] => email@mail.com
    [comment_author_url] => 
    [comment_author_IP] => 127.0.0.1
    [comment_date] => 2014-08-23 19:16:49
    [comment_date_gmt] => 2014-08-23 17:16:49
    [comment_content] => Lorem ipsum lorem
    [comment_karma] => 0
    [comment_approved] => 1
    [comment_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36 FirePHP/4Chrome
    [comment_type] => 
    [comment_parent] => 0
    [user_id] => 1
)