Skip to:
Content
Pages
Categories
Search
Top
Bottom

Synchronize creation Forum to a taxonomy forum

  • @kimis

    Participant

    Hello, I have a taxonomy forums that is is linked to the user, how can I do to create a forum on bbpress and when it’s done it also create a forum on my taxonomy

    my code is :

    function create_forum_taxonomy_entry($forum_id, $forum_args) {

    $forum_title = get_the_title($forum_id);
    $forum_slug = sanitize_title($forum_title);

    error_log(“Création du forum : ” . $forum_title);
    error_log(“Slug du forum : ” . $forum_slug);

    // Insertion du forum dans la taxonomie forum
    if (!term_exists($forum_title, ‘forums’)) {
    $result = wp_insert_term($forum_title, ‘forums’, array(
    ‘slug’ => $forum_slug
    ));

    if (is_wp_error($result)) {
    error_log(“Erreur lors de l’insertion du terme : ” . $result->get_error_message());
    } else {
    error_log(“Term créé avec succès : ” . print_r($result, true));
    }
    } else {
    error_log(“Le terme existe déjà : ” . $forum_title);
    }
    }
    add_action(‘bbp_insert_forum’, ‘create_forum_taxonomy_entry’, 10, 2); but this function isn’t called

    the file has another function below and it works so it’s not the calling of the file

Viewing 9 replies - 1 through 9 (of 9 total)
  • @robin-w

    Moderator

    I think you need to hook to

    do_action( 'bbp_new_forum_post_extras', $forum_id );

    which is in the function bbp_new_forum_handler

    @kimis

    Participant

    I did

    add_action('bbp_new_forum_post_extras', 'my_custom_bbp_new_forum_handler');
    
    function my_custom_bbp_new_forum_handler( $action = '' ) {
    error_log('test function');
        if ( 'bbp-new-forum' !== $action ) {
            return;
        }
    
        if ( ! bbp_verify_nonce_request( 'bbp-new-forum' ) ) {
            bbp_add_error( 'bbp_new_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
            return;
        }
    
        do_action( 'bbp_new_forum_post_extras', $forum_id );
    
        $forum_title = get_the_title($forum_id);
        $forum_slug = sanitize_title($forum_title);
    
        if (!term_exists($forum_title, 'forums')) {
            $result = wp_insert_term($forum_title, 'forums', array(
                'name' => $forum_title,
                'slug' => $forum_slug
            ));
    
            if (is_wp_error($result)) {
                error_log("Erreur lors de l'insertion du terme : " . $result->get_error_message());
            } else {
                error_log("Terme de taxonomie 'forums' créé avec succès : " . print_r($result, true));
            }
        } else {
            error_log("Le terme existe déjà : " . $forum_title);
        }
    
        $redirect_url = bbp_get_forum_permalink( $forum_id );
        bbp_redirect( $redirect_url );
    }

    but it still doesn’t work the first error log doesn’t appear on the debug file

    @robin-w

    Moderator

    so how are you creating the forum on bbpress? ie I go into xx and I click this and then type that

    @kimis

    Participant

    I have the plugin BBpress that allow me to create a forum on the admin panel on wordpress, the form is basic I just have to tape a name and pusblished it. Forum > Add new > tape the name > published and my forum is create. I also have a taxonomy forum that is linked to the user for that I do use the plugin User Taxonomy & Directory.

    @robin-w

    Moderator

    ok so from the admin area I think you need to hook to

    do_action( 'bbp_forum_attributes_metabox_save', $forum_id );

    @kimis

    Participant

    Well it still doesn’t work, what if instead of that, I add a select on the form to create a forum, I create it also in the admin panel. how can I do that because I did

    add_action('bbp_new_forum_form_after_content', 'add_formation_field_to_forum_form');
    function add_formation_field_to_forum_form() {
        $formations = get_terms(array(
            'taxonomy'   => 'formation',
            'hide_empty' => false,
        ));
        
        if (!empty($formations) && !is_wp_error($formations)) {
            echo '<div class="bbp-form">
                    <label for="forum-formation">Sélectionner une formation :</label>
                    <select name="forum_formation" id="forum-formation">';
            
            foreach ($formations as $formation) {
                echo '<option value="' . esc_attr($formation->term_id) . '">' . esc_html($formation->name) . '</option>';
            }
            
            echo '</select>
                  </div>';
        }
    }

    I want to link the forum to the taxonomy formation I think it should be more easier but in that way it doesn’t work I have linked formation to forum by doing

    function link_formation_taxonomy_to_forums() {
        register_taxonomy_for_object_type('formation', 'forum');
    }
    add_action('init', 'link_formation_taxonomy_to_forums');

    @kimis

    Participant

    sorry I did
    add_action('bbp_new_forum_form', 'add_formation_field_to_forum_form');

    @kimis

    Participant

    i did something else that actually work thanks for your help

    @robin-w

    Moderator

    glad you are fixed

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