Skip to:
Content
Pages
Categories
Search
Top
Bottom

How To Add “New Topic” button


  • erich199
    Participant

    @erich199

    In loop-fourms.php file add your custom “Add Topic” button:

    <a href="/new-topic/?ForumId=<?php echo bbp_get_forum_id()?>">New Topic</a>

    Add this code to a custom .php file or directly to your theme function.php file:

    //BBpress New Topic Button // 
    add_shortcode('wpmu_bbp_topic', 'wpmu_bbp_create_new_topic', 10);
    function wpmu_bbp_create_new_topic(){
    	
    	if ( isset($_GET['ForumId']) ){
    		
    		return do_shortcode("[bbp-topic-form forum_id=".$_GET['ForumId']."]");
    		
    	}else{
    		
    		return do_shortcode("[bbp-topic-form]");
    		
    	}
    }
    //End BBpress New Topic Button //

    Make sure you’ve created a custom page titled “new topic” with a friendly url of “new-topic”

    Place the new shortcode we registered into this page:

    [wpmu_bbp_topic]

    This will automatically display contact create form of the selected forum if ID exists in the URL otherwise fall back to default shortcode.

    You can see it in action on my site:
    http://artofwargaming.net/forums/

    Hope this helps anyone who was looking to create a “new topic” button.

    I can’t take credit for this, this was all thanks to the help of the guys over at WPMUDEV – Sajid, Anang, and Tyler.

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

  • Robkk
    Moderator

    @robkk

    Thanks for sharing!! I overthought the development of trying to do this functionality, the guys who helped you made it seem so simple haha.


    AdventureRidingNZ
    Participant

    @adventureridingnz

    This looks great, I just tried it on my forum and it works on categories but not on forums.
    I’m running latest WP, BBPress, BuddyPress versions with the Brickyard theme and the code is installed in my theme’s functions.php file..

    Cheers
    Eddie
    http://www.AdventureRidingNZ.co.nz


    erich199
    Participant

    @erich199

    @robkk,
    I thought the exact same thing when they sent the code. Hopefully we see this as a core feature in the future updates of bbpress.

    One can make the button visible to logged in users only by using the
    <?php if ( is_user_logged_in() ) : ?> button code here <?php endif; ?>


    @adventureridingnz
    ,

    I’m not sure, I’ll have to do some testing on my site to see if it will work with forums.


    reedy
    Participant

    @reedy

    Any way of implementing this without editing core files? I’m fine editing the functions file but am less happy with modifying loop-fourms.php


    Robkk
    Moderator

    @robkk

    @reedy

    copy loop-forums.php or any other templates you want to edit to a child theme into a folder called bbpress and you should be fine.

    Theme Compatibility


    erich199
    Participant

    @erich199

    I actually placed my button inside of loop-topics.php and it works for my needs.

    My users have the option to click the button or use the form at the bottom of the page.


    chemdata
    Participant

    @chemdata

    Has anyone tried to make the link take you to form at the bottom of the page and/or to make it go to new form but with the forum id of the forum where it originated from. If so how?

    Thanks.


    LeonFletcher
    Participant

    @leonfletcher

    Sorry for reviving an old thread, but I’ve followed the instructions exactly, but the Forum ID doesn’t automatically change to the forum you were on


    hotloverspassion
    Participant

    @hotloverspassion

    It doesn’t work for me either. When I click on the New topic link I created, it takes me to 404 page. Any suggestions anyone?

    Thanks


    Robin W
    Moderator

    @robin-w

    my style pack plugin has a new topic button

    https://wordpress.org/plugins/bbp-style-pack/


    erich199
    Participant

    @erich199

    @hotloverspassion,

    did you create a new page and name it “new topic”. This is where you will place the short code [wpmu_bbp_topic]


    willad
    Participant

    @willad

    Hello, I have also been trying to get a new topic button working for some time. In case it is of help to anyone, this is what seems to work for me.

    I created a wordpress page template “forum.php” and added the following code – it adds a ‘New Question’ button to the forum page but not if it’s the parent forum:

    <?php
    get_header(); 
    
    while ( have_posts() ) : the_post();
    if((($post_type = get_post_type()) == 'forum') ) //make sure we are on a forum
    {
      if(bbp_get_forum_id() != 0) //don't add the button to the parent forum
      {
        ?>
        <a href="<?php echo esc_url( add_query_arg( 'c', bbp_get_forum_id( ), site_url( '/new-topic/' ) ) )?>">New Question</a>
        <?php
      }
    }
    the_content();
    endwhile; // End of the loop.    
    ?>
    <?php get_footer();

    Now when I click the New Question link it opens the page new-topic and the forum I was on when I pressed the button is the forum the new question will be placed in.

    new-topic is a page I created in wordpress, and I gave it a template page-new-topic.php

    It contains the following code:

    <?php
    get_header(); 
    $my_c = get_query_var( 'c' );
    $forum_add_topic='[bbp-topic-form forum_id='.$my_c.']';
    echo do_shortcode($forum_add_topic);
    ?>
    <?php get_footer();

    The line

    <a href="<?php echo esc_url( add_query_arg( 'c', bbp_get_forum_id( ), site_url( '/new-topic/' ) ) )?>">New Question</a>

    in forum.php adds a variable c (which contains the current forum id() -bbp_get_forum_id( ) ) to the url when New Question is clicked.

    In page-new-topic.php this variable c is added to the shortcode [bbp-topic-form forum_id=XXX]

    do_shortcode then runs the shortcode which creates the form.

    I also had to add the following to functions.php to tie the two pages together:

    function add_custom_query_var( $vars ){
      $vars[] = "c";
      return $vars;
    }
    add_filter( 'query_vars', 'add_custom_query_var' );

    You could add this as a plugin if you wish.

    The idea for the shortcode came from this post: post

    How to pass variables through wordpress urls came from this post:

    post-2

    I am new to WordPress and PHP so there may be better ways of doing this but, as I said, this seems to work.


    rajesh896
    Participant

    @rajesh896

    Make sure you’ve created a custom page titled “new topic” with a friendly url of “new-topic”

    Place the new shortcode we registered into this page:

    [wpmu_bbp_topic]

    This will automatically display contact create form of the selected forum if ID exists in the URL otherwise fall back to default shortcode.

    You can see it in action on my site:
    happy deepavali images


    Shaktimaan
    Participant

    @umar007

    Ok i have created the New topic page as suggested by @erich199. It works as expected but the problem is i also have buddypress group forums.

    The form shows all group forums including those that user has not joined and let them create topic to any group forum.

    I would like to either

    1. Hide all group forums or
    2. Show only those group forums that user has joined.

    Thanks


    urphy73
    Participant

    @urphy73

    Hello
    If want to use that for only 1 forum where exactly di I have to put the ID in this code ?
    Thanks a lot

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