Skip to:
Content
Pages
Categories
Search
Top
Bottom

show message if user is logged in but does not have participation rights

  • @haddlyapis

    Participant

    Hi there,
    Due to a high number of spambot registrations, any user who registers for the forum initially gets “Spectator” rights. Once I check (usually within 12-24 hours) that they are not spam, I change them to “Participant”.
    Some people get annoyed that they cannot post immediately. And I would like to create a popup banner that appears when users login for the first time but cannot post. I am capable of the JS and the CSS, but not quite the PHP.

    Basically, I want to enqueue a JS file if the user is a “spectator” and is “logged in”.

    Could you please advise how to complete my function (hopefully just the commented parts), which will go in the functions.php file, and what add_action arguments should be applied if I am wrong?

    function forum_user_is_spectator(){
    if ( is_bbpress() && is_user_logged_in() ) {
        //$User_ID = get user ID
        //$Spectator = find out if User Has Spectator rights
        if ($Spectator){
           wp_enqueue_script( 'spectator-js', get_template_directory_uri() . '/js/spectator.js', array(), false, true);
           }
        }
    }

    add_action('wp_enqueue_scripts', 'forum_user_is_spectator');

    thx in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • @robin-w

    Moderator

    you add action is right, and I’d have this as the function (not tested!)

    function forum_user_is_spectator(){
    if ( is_bbpress() && is_user_logged_in() ) {
       $User_ID = get_current_user_id() ;
    	$Spectator = (!empty(current_user_can( 'spectate' )) ? 1  : 0) ;
        if ($Spectator){
           wp_enqueue_script( 'spectator-js', get_template_directory_uri() . '/js/spectator.js', array(), false, true);
           }
        }
    }

    @robin-w

    Moderator

    Sorry, I’m being brain dead this afternoon.

    Spectate is a capability shared by many roles, so the code above is rubbish !

    Try this

    function forum_user_is_spectator(){
    if ( is_bbpress() && is_user_logged_in() ) {
        $user_ID = get_current_user_id() ;
        $role = bbp_get_user_role( $user_id );
        if ($role == bbp_get_spectator_role()){
           wp_enqueue_script( 'spectator-js', get_template_directory_uri() . '/js/spectator.js', array(), false, true);
           }
        }
    }

    @haddlyapis

    Participant

    great @robin-w , this is very useful.
    Unfortunately, I just copied and pasted your code into my functions.php file and it didn’t work.
    Took me 3 hours to figure out that the variable is spelled incorrectly within the bbp_get_spectator() function. Once I corrected this, it works!!
    Thx for your prompt help. All the best. This topic is now closed.

    @robin-w

    Moderator

    great- glad you are fixed, and sorry for the spelling !

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar