4

Functions Php :

add_shortcode( 'membervip', 'memberviparea' );
function memberviparea( $atts, $content = null ) {
    if( is_user_logged_in() ) return '<p>' . $content . '</p>';
    else return;
}

Post :

[membervip]Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.[/membervip]

With this code I can show the links to only logged in members, but I want to only display the links for members who are logged in and have made a comment.

What code can do this?

  • Quick question - Do you want to show the links for any member that is logged in and has left a comment on ANY post on the website OR members who are logged in and have left on a comment on the current post? – Travis Pflanz Feb 8 '13 at 21:34
  • for any member that is logged in and has left a comment on ANY post on the website – M.UNLU Feb 8 '13 at 21:42
  • this should steer you in the right direction. Just make it work and check if number is greater than zero. – montrealist Feb 8 '13 at 22:15
  • I don't understand – M.UNLU Feb 8 '13 at 22:41
  • 1
    I answered the question .for any member that is logged in and has left a comment on ANY post on the website – M.UNLU Feb 8 '13 at 22:59
7

Check if the user has left a comment

// the user may have commented on *any* post
define( 'CHECK_GLOBAL_FOR_COMMENTS', TRUE );

//
// some more code
//

function memberviparea( $atts, $content = null ) {

    $post_id = 0;

    // if the user have to left a comment explicit on this post, get the post ID
    if( defined( 'CHECK_GLOBAL_FOR_COMMENTS' ) && FALSE === CHECK_GLOBAL_FOR_COMMENTS ) {
        global $post;

        $post_id = ( is_object( $post ) && isset( $post->ID ) ) ?
            $post->ID : 0;
    }

    if( is_user_logged_in() && user_has_left_comment( $post_id ) )
        return '<p>' . $content . '</p>';
    else
        return;

}

/**
 * Check if the user has left a comment
 *
 * If a post ID is set, the function checks if
 * the user has just left a comment in this post.
 * Otherwise it check if the user has left a comment on
 * any post.
 * If no user ID is set, the ID of the current logged in user is used.
 * If no user is currently logged in, the fuction returns null.
 *
 * @param int $post_id ID of the post (optional)
 * @param int $user_id User ID (required)
 * @return null|bool Null if no user is logged in and no user ID is set, else true if the user has left a comment, false if not
 */
function user_has_left_comment( $post_id = 0, $user_id = 0 ) {

    if( ! is_user_logged_in() && 0 === $user_id )
        return NULL;
    elseif( 0 === $user_id )
        $user_id = wp_get_current_user()->ID;

    $args = array( 'user_id' => $user_id );

    if ( 0 !== $post_id )
        $args['post_id'] = $post_id;

    $comments = get_comments( $args );

    return ! empty( $comments );

}

This function will check if the user has left a comment on the current post. If you want to check if the user generally left a comment (on any post), delete or comment out this line 'post_id' => $pid, // get only comments from this post and

Update

Because such a function could be usefull, I rewrite the code a bit to make it easier to reuse it. Now it is possible to check if the user left a comment on any post or on a specific post by passing the post ID to the function.

| improve this answer | |
  • See the update. I read your comments after posting my answer – Ralf912 Feb 8 '13 at 23:08
  • Beat me to it! Nice plugin, right there! – Travis Pflanz Feb 8 '13 at 23:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.