Genbu Theme, a WordPress theme club

bbPress Functionality Plugins

i need several feature in my support forum:

  1. modify author of topic and reply.
  2. enable backtick for code just like and theme hybrid.
  3. add [[private] shortcode [/private]] so user can post sensitive information and data in bbPress.
  4. search form for topic and reply
  5. hide my reply and topic to non registered user.

bbPress author metabox

first i need to add support for author in bbPress topic and reply post type.

/* hook post type feature to init, as recomendation in codex */
add_action( 'init', 'gt_bbpress_add_author' );

 * Add Author Feature in BBPress Topics and Replay
 * @since 0.1
function gt_bbpress_add_author() {

/* add author support for post type "topic" and "reply" */
add_post_type_support( 'topic', 'author' );
add_post_type_support( 'reply', 'author' );

now all i need is to add all registered user in author meta-box drop down cause WordPress only add member with ‘author’ capability.

/* filter to change drop down user in author metabox */
add_filter('wp_dropdown_users', 'gt_bbpress_author_metabox');

 * Add all member in Author Meta Box
 * as default WordPress only add member with "author" capability
 * in author metabox
 * BBPress use function "bbp_get_topic_post_type" and "bbp_get_reply_post_type"
 * to determined post type used as topic and reply, ideally this should be used as
 * conditional check.
 * we wont use that,and set it directly to "topic" and "reply"
 * The one we are filtering is 'post_author_meta_box'
 * defined in wp-admin\includes\meta-boxes.php
 * @since 0.1
function gt_bbpress_author_metabox( $output ){

    /* globalize post and post type object. */
    global $post,$post_type;

    /* conditional check if it's a topic or reply post type,
    * so it only apply in replay and topic edit screen
    if ( is_admin() && ($post_type == 'topic' ||  $post_type == 'reply') ) {

        /* get all user. */
        $users = get_users();

        /* start with select output */
        $output = '<select id="post_author_override" name="post_author_override" class="">';

        /* for each user create an option drop down */
        foreach($users as $user){

            /* get topic/reply submitter as selected author */
            $selected = ( $post->post_author == $user->ID )?"selected='selected'":'';

            /*  create the options drop down  */
            $output .= '<option value="'.$user->ID.'"'.$selected.'>'.$user->user_login.'</option>';
        /* wrap it up. */
        $output .= "</select>";
    /* and return the output. */
    return $output;

now can switch the user in every topic and reply. very simple right?

Enable backtick in bbPress and register private shortcode

i merge this feature in one plugin. i can’t remember why. for backtick i use Miohki Backticks i found it from GitHub. i modify it a little, i just remove `add_filter( ‘bbp_get_form_topic_content’, ‘mk_bb_code_trick_reverse’, 1 );` from the original plugin because i don’t need it.

for private shortcode, i just create a simple shortcode:

/* register private shortcode */

 * Private Shortcode
 * created so user can post sensitive data
 * only administrator and the user who post can read.
 * To Enable this shortcode in bbpress use "bbPress2 shortcode whitelist" plugin.
 * and add 'private' in as whitelist shortcode in plugin admin settings.
 * TODO:
 * add it automaticly (self declare to "bbPress2 shortcode whitelist" plugin)
 * @since 0.1
function gt_bbress_private_sc( $atts, $content = null ){

    /* get author id */
    $author_id = get_the_author_ID();

    /* get current user id */
    $user_id = get_current_user_id();;

    /* if the user is administrator */
    if ( current_user_can('administrator') )
        $content = '<div><strong>Private SC:</strong> '.$content.'</div>';

    /* if the current user is the user who post it */
    elseif ( $user_id == $author_id )
        $content = '<div><strong>My Private Content:</strong> '.$content.'</div>';

    /* if the user don't have privelage to read the content. */
        $content = '<div>Private Content</div>';

    /* return the shortcode content */
    return $content;

and of course i need to add info/notice to user so they know how to add code with backtick, and private shortcode.

/* add info text in before topic notices hook */

/* add info text in before replay notices hook */

 * Notice for user
 * Add message notice in bbpress form so user know how to use it.
 * @since 0.1
function genbutheme_bbpress_form_notice(){

    /* display notice for user about backtick and private shortcode. */
    echo '<div>Please put code in between `backtick` characters in visual editor.</br>For private or sensitive data, use shortcode [[private]your data[/private]].</div>';

How to Enable Shortcode in bbPress

bbPress use different system for topic and content, so as default shortcode is not enable in topic and reply. There’s an easy way to enable shortcode in topic and reply. I just need to filter `bbp_get_reply_content` and `bbp_get_form_topic_content` with `do_shortcode` but it’s not save to give all registered user the power of shortcode, they, for example: they can post topic and reply with contact form shortcode, or any other “not-save-shortcode”.

The solution is to selectively allow shortcode th bbPress: bbPress2 shortcode whitelist . so i install the plugin and add “private” as allowed shortcode in my bbPress install.

Add search form widget for bbPress

for this feature i just need to create a custom search form with topic and reply post type variable. like this:


<form method="get" action="<?php echo trailingslashit( home_url() ); ?>">
    <input type="hidden" name="post_type[]" value="topic" />
    <input type="hidden" name="post_type[]" value="reply" />
    <input type="text" name="s" value="<?php if ( is_search() ) echo esc_attr( get_search_query() ); else apply_atomic('search_form_text', esc_attr_e( 'Search forum...', 'genbu' )); ?>" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" />
    <input name="submit" type="submit" value="<?php apply_atomic('search_submit_text', esc_attr_e( 'Search', 'genbu' )); ?>" />
</form><!-- .search-form -->

</div><!-- .search -->

Hide admin reply and topic to non member for bbPress

i want my forum to be indexed by search engine, so i can get search engine traffic for each topic posted. but i also don’t want my support for user is open. that’s the reason i create this feature. It’s inspired by old Theme Hybrid forum when only Justin Tadlock reply is hidden for non-member. (now all topic is member-only in Theme Hybrid)

/* filter topic and reply */
add_filter('bbp_get_reply_content', 'gt_bbpress_admin_content_filter');
add_filter('bbp_get_topic_content', 'gt_bbpress_admin_content_filter');

 * Display notice to un-verified user,
 * or display the content to member with verified role.
 * @since 0.1
function gt_bbpress_admin_content_filter($content){

    /* get author id */
    $author_id = get_the_author_ID();

    /* if current user is administrator, display it */
    if ( current_user_can('administrator') )
        $content = $content;

    /* if current user don't have 'verified' role,
     * and the content is from "administrator"
     * display error message
    elseif ( !current_user_can('verified') && user_can( $author_id, 'administrator' ) )
        $content = '<div>Member only content.</div>';

    /* return the content */
    return $content;

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.