bbPress

Simple, Fast, Elegant

bbPress support forums » Troubleshooting

Private Forum script

(41 posts)
  • Started 1 year ago by papercuts3
  • Latest reply from davidbessler
  • This topic is resolved
  1. papercuts3
    Member

    I needed desperately a script that would restrict all access to unregistered users so I wrote one. But I am an absolute beginner so don't know no nothin' about elegance or whatnot but I just tested it in my installation and it works.

    The thing is naturally bb_press has authentication scripts. but to make all you website protected you need to put this in your header.php. However bb-login.php, which your users need to login also use the header.php, so I had to make sure that this small script runs on all pages EXCEPT the login page.

    Here's the script:

    <?php
    $login_page = $bb->path . 'bb-login.php';
    if ($_SERVER['PHP_SELF'] != $login_page) {
    if ( !bb_is_user_logged_in() ) {
    header('Location: ' . $bb->domain . $bb->path . 'bb-login.php');
    } else { }
    }
    else { }
    ?>

    How to Use it:
    1. Copy paste the above code to your my-templates/header.php AT THE VERY TOP of the file. Make sure you leave no empty space before the <?php line (use something like TextPad - it's free). So you header should look like:
    <?php
    $login_page = $bb->path . 'bb-login.php';
    if ($_SERVER['PHP_SELF'] != $login_page) {
    if ( !bb_is_user_logged_in() ) {
    header('Location: ' . $bb->domain . $bb->path . 'bb-login.php');
    } else { }
    }
    else { }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ....

    That's it. Since I use the values you provided in you BBPRESS setup all the paths should be correct.

    Like I said I am complete newbie, so use at your own risk. BACKUP your header.php, or if something goes wrong just delete the script you just added.

    Hope it works for you,

    Caner

    Posted 1 year ago #
  2. Good thinking. Using the logged in status natural to bbPress to do this. Just looking at the code it would work, but I will test this out on my dev forum and report back as well. I could see this expanded into a plugin very easily....

    Trent

    Posted 1 year ago #
  3. Thank you papercuts3!

    Works great!

    Posted 1 year ago #
  4. topiQ
    Member

    thanks for this one! nice work.
    ps: would be great if the admin could restrict some forums to certain users. would be great if this could be done!

    Posted 1 year ago #
  5. papercuts3
    Member

    topicQ, you are quite right. The lack of Forum restriction per user I think is a major drawback in bb-Press, but I'm guessing it's also high up on their list of things to do since all forum applications have it.

    Otherwise a plugin could do that but it would have to be extensive.

    Posted 1 year ago #
  6. topiQ
    Member

    it would be great if someone could write a plugin...
    coz i dont think that there will be an update soon. but who knows...
    forum restrictions is really essential for my board.

    greetings topiQ

    Posted 1 year ago #
  7. here is the plugin..
    please note that the bb hook for initialization was change after the blix release (0.73). it was changed from 'init' to 'bb_init'


    function my_check_private_forum($test='') {
    global $bb,$bb_current_user;
    $login_page = $bb->path . 'bb-login.php';
    if ($_SERVER['PHP_SELF'] != $login_page) {
    if ( !$bb_current_user ) {
    header('Location: ' . $bb->domain . $bb->path . 'bb-login.php');
    }
    }
    }
    add_action( 'bb_init', 'my_check_private_forum');

    Posted 1 year ago #
  8. so1o: does this plugin need to be used with the code posted earlier in this thread, or is it standalone?
    If it is standalone (ment to function on its own) I am not getting any results. I put the plugin into my-plugins and no results. Everything loads as normal.
    Any pointers.

    Posted 1 year ago #
  9. i would think it is standalone..

    are you sure you aren't logged in.. it would redirect the user to login.php if he isnt logged in. if you are logged in it would load normally

    also check the bb-settings.php file.. check what is the name of the hook for initialization. as i mentioned the hook name was changed from init to bb-init.

    if the bb-settings file has this
    do_action('init', '');
    then change the last line of the plugin to
    add_action( 'init', 'my_check_private_forum');

    Posted 1 year ago #
  10. works now. I mis-read your disclaimer earlier. So those of us using .73 need to change 'bb-init' to 'init' in the plug-in.

    Posted 1 year ago #
  11. but i find some problem when we long in my forum chases its tail on the login.php form.. do you find that problem?

    Posted 1 year ago #
  12. i think this would be safer option..
    function my_check_private_forum() {
    global $bb;
    $login_page = $bb->path . 'bb-login.php';
    if (!bb_is_user_logged_in() && $_SERVER['PHP_SELF'] != $login_page) {
    header('Location: ' . $bb->domain . $bb->path . 'bb-login.php');
    }
    }
    add_action( 'bb_init', 'my_check_private_forum');

    any thoughts?.. any one?

    Posted 1 year ago #
  13. topiQ
    Member

    where do i have to put this code? in the header.php file? and whats the difference between the first post's code? can you restrict the user group of a forum with this one?!

    Posted 1 year ago #
  14. this is a plugin.. create a something.php in the plugins directory and drop it in there..

    the plugin will not restrict user on a forums basis..

    Posted 1 year ago #
  15. So10. That is beautiful! There are 4 or 5 of you that really are rocking and rolling! I will do some research here because really there are about 20-30 plugins now for bbPress bouncing around. I will try and think of a way to get them listed in one place.....

    Trent

    Posted 1 year ago #
  16. topiQ
    Member

    @so1o: yes thats clear, but what is the difference between your plugin and the one from papercuts3's first post?!

    @trent: as a moderator you should add the new plugins here as there are already some...
    http://bbpress.org/plugins/

    Posted 1 year ago #
  17. papercuts3
    Member

    topicQ: I think the function is exactly the same but my initial post was a working script where you had to make a change in your files, the one so10 sent is an actual plugin, which is a better way of integrating new options.

    Thanks for following up on this, we have now a plugin for private forums.

    Cheers

    Posted 1 year ago #
  18. topiQ
    Member

    ok. now everything works fine...
    just one more question:
    is it okay if i put the "my_check_private_forum()" in the header.php before the doctype stuff (due to W3C)?!

    Posted 1 year ago #
  19. nothing prevents you from doing that... only thing is tomorrow if you change the template you will have to have this code there too..

    if you keep it in a plugin file in the my-plugins dir it will automatically come thru with the new files..

    cheers

    Posted 1 year ago #
  20. topiQ
    Member

    so with your plugin i dont have to add the "my_check_private_forum()" to the header.php?! if i dont do this it does not work...

    Posted 1 year ago #
  21. please note that the bb hook for initialization was change after the blix release (0.73). it was changed from 'init' to 'bb_init'

    check the bb-settings.php file.. check what is the name of the hook for initialization. as i mentioned the hook name was changed from init to bb-init.

    if the bb-settings file has this
    do_action('init', '');
    then change the last line of the plugin to
    add_action( 'init', 'my_check_private_forum');

    Posted 1 year ago #
  22. @topiQ - I am working on permissions for plugin page, but I still don't have access to that one yet.

    @papercuts - Great work on this.

    Trent

    Posted 1 year ago #
  23. topiQ
    Member

    @so1o: it works now! do u use a newer release than .73?!

    Posted 1 year ago #
  24. yup.. i use the latest on the repository..

    cheers

    Posted 1 year ago #
  25. Would there be a way to exclude certain forums for this plugin as "public" forums or just have a list of "private" forums. Basically, can this be adapted to actually have private forums and topics in that forum that don't show up unless logged in, but the rest show up all the time?

    This has been asked for several times and would be really useful for even myself as I would like to have certain posts only for members!

    Trent

    Posted 1 year ago #
  26. Selectable Private Forums
    http://www.adityanaik.com/projects/plugins/bb-private-forums/

    Posted 1 year ago #
  27. topiQ
    Member

    it does not work for me. when i want to access the private forum plugin in the admin panel i get this error:

    Fatal error: Call to undefined function get_option() in /srv/www/httpd/phost/c/com/pytalhost/cessi/web/bbpress/my-plugins/private-forums.php on line 22

    Posted 1 year ago #
  28. sorry.. the theme currently works only with the site_options plugin. i'll fix the plugin to check for that

    thanks

    Posted 1 year ago #
  29. topiQ
    Member

    i installed the site_opitons plugin. but i get some kinds of errors.
    in the "private forum management" i get this errors:

    Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /srv/www/httpd/phost/c/com/pytalhost/cessi/web/bbpress/my-plugins/private-forums.php on line 29
    value="2" > News

    and if i want to change the message a user gets if he is not allowed to access a private forum:

    Fatal error: Call to undefined function is_serialized() in /srv/www/httpd/phost/c/com/pytalhost/cessi/web/bbpress/my-plugins/site-options.php on line 190

    thanks for your help!

    PS: in the SITE OPTIONS tab i get nothing but a submit button...

    Posted 1 year ago #
  30. i'll fix this and get a new version out in the evening

    thanks

    Posted 1 year ago #

RSS feed for this topic

Reply »

You must log in to post.

Code is Poetry.