I am flooding comment with my plugin in a reasonable manner:

function log_my_comments($post_id)
    $my_comments = get_my_comment_data($post_id);

    foreach($my_comments as $my_comment) {
            'comment_post_ID' => $post_id,
            'comment_type' => $my_comment,
            'comment_type' => 'my_comment',
    } //endforeach

add_action( 'save_post', 'log_my_comments', 9 );
add_action( 'new_to_publish', 'log_my_comments', 9 );

It will flood comment not more than 5 in a single transaction. Now when I am using my function for user role 'author' it blocks me with:

You are posting comments too quickly. Slow down.

and makes the post to pending.

I can easily resolve the issue by adding the capability moderate_comments to the user, but it is a core feature, and I do not want to hack the default author role. I just want to resolve the issue in a local scope, and not global.

So I took the easiest way of doing this:

function log_my_comments($post_id)
    $my_comments = get_my_comment_data($post_id);

    // Disable comment flooding
    add_filter('comment_flood_filter', '__return_false', 11);

    foreach($my_comments as $my_comment) {
        // wp_insert_comment();
    } // endforeach

    // Enable comment flooding again
    remove_filter('comment_flood_filter', '__return_false');

add_action( 'save_post', 'log_my_comments', 9 );
add_action( 'new_to_publish', 'log_my_comments', 9 );

But it actually is not working. I tried with priority 11 and PHP_INT_MAX also. Need your suggestion, how can I deal with this?


Thinking about this, I think I found a way of doing this in a right way. But I'm confused on how to execute the idea.

When registering a custom post type (CPT) we have two declarations like:

'capability_type' => 'mycpt',
'map_meta_cap'    => true,

When adding a user role, we can assign the custom capabilities like below:

$the_user = new WP_User( $user_id );
$capability_type = 'mycpt';
$the_user->add_cap( "read_{$capability_type}" );
$the_user->add_cap( "edit_{$capability_type}" );
// ...

In this way we can assign custom capabilities to the user and they map to the core capabilities (read_post, edit_post etc.) when the map_meta_cap is true (as per my understanding).

I'm thinking of a similar solution where I can give the user a custom permission for my custom comment type (mycomment) which will be mapped to the core capability: moderate_comments like moderate_mycomment.

And if that capability (moderate_mycomment) is mapped properly, WP core won't show the error, as for the user the moderate_comments permission is set.

But how can I do this?

PS. I'm storing comments with a custom comment type. (Reference thread)

  • It's really unclear what your goal is. Can you reword the question to explain what you want to do, for which users, and then explain what is not happening correctly? Are you trying to disable flood for one user? – Slam Sep 16 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.