Forum Replies Created

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter elitaz

    (@elitaz)

    Back to my question, if someone looking to allow only one comment per user per post I think I found a solution:

    add_filter('comments_open', 'restrict_users', 10, 2);
    
    function restrict_users($open, $post_id) {
       if (intval($post_id) && get_post($post_id)) {
           $args = array('post_id' => $post_id, 'count' => true);
           $user = wp_get_current_user();
           if ($user && intval($user->ID)) { // for registered users
               $skip = false;
               $ignoreTheseRoles = array('administrator', 'editor'); // which user roles should be ignored
               if ($user->roles && is_array($user->roles)) {
                   foreach ($user->roles as $role) {
                       if (in_array($role, $ignoreTheseRoles)) {
                           $skip = true;
                           break;
                       }
                   }
               }
               if (!$skip) {
                   $args['user_id'] = $user->ID;
                   $open = get_comments($args) ? false : true;
               }
           } else { // for guests
               $commenter = wp_get_current_commenter();
               if ($commenter && is_array($commenter) && isset($commenter['comment_author_email'])) {
                   $args['author_email'] = $commenter['comment_author_email'];
                   $open = get_comments($args) ? false : true;
               }
           }
       }
       return $open;
    }

    Now just need the way to let author to reply the comments…

    Thread Starter elitaz

    (@elitaz)

    I was able to disable post author to post comment, but now he is not able to write reply any more as well….

    global $current_user;
    $args = array('user_id' => $current_user->ID);
    if ( get_current_user_id() == $post->post_author ){
        echo 'disabled';
    } else {
       comment_form(
    		array(
    			'logged_in_as'       => null,
    			'title_reply'        => esc_html__( 'Leave a comment', 'twentytwentyone' ),
    			'title_reply_before' => '<h2 id="reply-title" class="comment-reply-title">',
    			'title_reply_after'  => '</h2>',
    		)
    	);
    }
    Thread Starter elitaz

    (@elitaz)

    Hi Ian. Thanks for reply. It works like a charm.
    Cheers mate!

    Thread Starter elitaz

    (@elitaz)

    Hi @ninjew,
    Yes.

    Thread Starter elitaz

    (@elitaz)

    Hi. Thanks for the reply. One more question, is it possible to show one member posts on the map without search? Like sort posts by user id? Thanks

    Thread Starter elitaz

    (@elitaz)

    And one more thing, is it any way to add post location via front end? For example while using Wp User frontend plugin. Thanks

Viewing 6 replies - 1 through 6 (of 6 total)