Forum Replies Created
-
In reply to: How To Add “New Topic” button
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:
I am new to WordPress and PHP so there may be better ways of doing this but, as I said, this seems to work.