Skip to:
Content
Pages
Categories
Search
Top
Bottom

Getting forum page name from plugin


  • SooperGenius
    Participant

    @soopergenius

    Hi,

    Here’s my situation. I have a site with several forums. Let’s call them Forum A, B and C. All users to the site can view Forum A. Through other actions that happen on the site users collect points. At a certain point total Forum B becomes available. At another point total Forum C becomes available. There is an external webservice I can call to see if the user has the appropriate number of points for the relevant Forum.

    As far as I can tell, bbPress doesn’t have the built in capability to handle that particular permissions scenario so I’m building a plugin. My plugin is hooking on the “user_has_cap” filter to determine whether a user can see a particular forum. I don’t want to make an external webservice call on every single current_user_can() call that gets made during a page load, so in my hook function I want to immediately check to see if the current request is a Forum page.

    For example, if the requested page was “http://localhost/wordpress/forums/forum/forum-b/” then being able to get the pagename “forums” it would allow me to jump out of my hook function immediately if the request wasn’t for a forum page or topic. Below is a copy of code/pseudocode for what I’m doing in the plugin.

    function forum_lock_check($allcaps, $cap) {
        if (pagename != "forums") return $allcaps;	
    
        if (webservice says I can) {
            $allcaps["can-view-forum"] = true;
        }
        return $allcaps;
    }
    
    add_filter("user_has_cap", "forum_lock_check", 10, 3); 

    Is there a wordpress or bbpress function that gives me the pagename the forum is attached to? Of course, if there is an easier way to do what I want, I’m all ears for that as well. Thanks for any help you can give me.

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

  • Robin W
    Moderator

    @robin-w

    is_bbpress()

    should do it I think.

    Otherwise bbpress has custom post types of forum, topic and reply.

    You should also consider disabling or modifying the bbpress search function, as otherwise searches will go forum wide, giving a backdoor.

    Good luck !


    SooperGenius
    Participant

    @soopergenius

    is_bbpress() almost works. Thanks for the pointer there, by the way. I can’t see how I missed that function in all my searching. I failed my Google-fu obviously. My code now looks like this in the my-plugin.php file.

    function forum_lock_check($allcaps, $cap) {
    	if (function_exists("is_bbpress")) {
    		$is_bbpress = is_bbpress();
    	} else {
    		$is_bbpress = false;
    	}
    	
    	return $allcaps;
    }
    
    add_filter("user_has_cap", "forum_lock_check", 10, 3); 

    Here’s the funny part. (Funny = weird not Funny = haha) If I’m navigating to the main forums page (http://localhost/wordpress/forums/), then everything proceeds swimmingly. If I then navigate to a forum within the page (http://localhost/wordpress/forums/forum.forum-a) or to a thread within the forum (http://localhost/wordpress/forums/topic/post-1) I then get several of the following errors.

    Notice: Trying to get property of non-object in C:\xampp\htdocs\wordpress\wp-includes\query.php on line 3792
    
    Notice: Undefined property: WP_Query::$post in C:\xampp\htdocs\wordpress\wp-includes\query.php on line 3306

    Commenting out the “$is_ppbress = is_bbpress();” line removes the errors but I’ve got no idea why WordPress suddenly doesn’t like the code. Have you seen this before? What file is is_bbpress() in?

    Thanks, Robin.


    SooperGenius
    Participant

    @soopergenius

    Nevermind, Robin. It’s just a Notice and not an error at this point. I’m not going to stress over it at the moment. The code is working fine. Thanks for the help.


    Robin W
    Moderator

    @robin-w

    ok if it proves an issue, you could use

    if ( is_singular( array('forum', 'topic', 'reply') ) ) {
        //  conditional content/code
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar