BuddyDev

Search

[Resolved] Buddyblog change author if post is published in a specific category

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45196

    will be expecting your feed back sir.

    thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #45197

    Hi Tosin,

    Please try this code

    
    
    /**
     * Update BuddyBlog Author when a post is submitted in a specific category.
     */
    
    add_filter( 'bblpro_post_submission_prepared_data', function ( $post_data, $form_id, $post, $is_submission ) {
    	// update author if the category was selected. Don't do anything on edit.
    
        $anonymous_cat_id = 1487;
        $anonymous_user_id = 4766806;
    
        if( ! $is_submission ) {
            return $post_data;
        }
    
    	$selected_terms_ids = isset( $post_data['tax_input'] ) ? (array) $post_data['tax_input'] : array();
    
    	$selected_categories = isset( $selected_terms_ids['category'] ) ? (array) $selected_terms_ids['category']: array();
    
        if( ! $selected_categories  || ! in_array( $anonymous_cat_id, $selected_categories ) ) {
            return $post_data;
        }
    
    	// Update.
    	$post_data['post_author'] = $anonymous_user_id;
    	// keep current user data for future?
    	if ( ! empty( $post_data['ID'] ) ) {
    		update_post_meta( $post_data['ID'], '_bbl_original_post_author', get_current_user_id() );
    	}
    
    	return $post_data;
    }, 10, 4 );
    
    

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45200

    I just tested the code but it did not work, the current logged in user is still the post author instead of the assigned anonymous user.

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #45201

    Hi Tosin,
    Please share your complete posting workflow(is the post moderated etc). If it’s not working, I need to understand your workflow to amend the code.

    Also, are you sure that user Id you used for anonymous user actually exists?

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45205

    YES im sure the anonymous user ID exists.

    1. workflow setting = on submission – publish post
    2. I also have a custom post taxonomy called (locations)
    3. redirect user to = Yes, to the single post screen.
    4. im also using the shortcode [bbl-create-by-post-type post_type=’post’] in the (create-new-post) page
    5. I also have buddyblog pay per post active

    This is all the buddyblog code I have I dont think there should be any conflict

    
     // Request confirmation before post submission.
    add_filter( 'bblpro_submission_button_classes', function ( $classes ) {
    	array_push( $classes, 'bbl-confirm-action' );
    
    	return $classes;
    } );
    
    // Buddyblog minimum words for publishing
    add_filter( 'bblpro_validation_errors', function ( $errors, $data ) {
    
    	if ( $errors->has_errors() ) {
    		return $errors;
    	}
    	$content     = empty( $data['bbl_post_content'] ) ? '' : $data['bbl_post_content'];
    	$words_count = (int) str_word_count( $content );
    
    	if ( $words_count < 100 ) {
    		$errors->add( 'bbl_post_content', _x( 'A minimum of 100 words is required to publish.', 'Post form validation message', 'buddyblog-pro' ) );
    	}
    	return $errors;
    }, 10, 2 );
    
    /**
     * Assign (Sponsored Post) category to BuddyBlog pay per post articles on checkout.
     */
    add_action( 'bblpro_ppp_post_published', function( $post_id ) {
    	$post_type = get_post_type( $post_id );
    	if ( 'post' != $post_type ) {
    		return;
    	}
    	// Get category.
    	$newcat = get_term_by( 'name', 'Sponsored posts', 'category' );
    	if ( ! $newcat ) {
    		return;
    	}
    	wp_set_post_categories( $post_id, $newcat->term_id, true );
    } );
    
    /**
     * Update BuddyBlog Author when a post is submitted in a specific category.
     */
    add_filter( 'bblpro_post_submission_prepared_data', function ( $post_data, $form_id, $post, $is_submission ) {
    	// update author if the category was selected. Don't do anything on edit.
    
        $anonymous_cat_id = 1487;
        $anonymous_user_id = 4766806;
    
        if( ! $is_submission ) {
            return $post_data;
        }
    
    	$selected_terms_ids = isset( $post_data['tax_input'] ) ? (array) $post_data['tax_input'] : array();
    
    	$selected_categories = isset( $selected_terms_ids['category'] ) ? (array) $selected_terms_ids['category']: array();
    
        if( ! $selected_categories  || ! in_array( $anonymous_cat_id, $selected_categories ) ) {
            return $post_data;
        }
    
    	// Update.
    	$post_data['post_author'] = $anonymous_user_id;
    	// keep current user data for future?
    	if ( ! empty( $post_data['ID'] ) ) {
    		update_post_meta( $post_data['ID'], '_bbl_original_post_author', get_current_user_id() );
    	}
    
    	return $post_data;
    }, 10, 4 );
    
  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45239

    Any update sir

    thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #45251

    Hi Tosin,
    Thank you for the patience.

    There was a slight mistake on my part.

    Please use the updated code

    
    
    /**
     * Update BuddyBlog Author when a post is submitted in a specific category.
     */
    add_filter( 'bblpro_post_submission_prepared_data', function ( $post_data, $form_id, $post, $is_submission ) {
    	// update author if the category was selected. Don't do anything on edit.
    
        $anonymous_cat_id = 1487;
        $anonymous_user_id = 4766806;
    
        if( ! $is_submission ) {
            return $post_data;
        }
    
    	$selected_terms_ids = isset( $_POST['tax_input'] ) ? (array) $_POST['tax_input'] : array();
    
    	$selected_categories = isset( $selected_terms_ids['category'] ) ? (array) $selected_terms_ids['category']: array();
    
        if( ! $selected_categories  || ! in_array( $anonymous_cat_id, $selected_categories ) ) {
            return $post_data;
        }
    
    	// Update.
    	$post_data['post_author'] = $anonymous_user_id;
    	// keep current user data for future?
    	if ( ! empty( $post_data['ID'] ) ) {
    		update_post_meta( $post_data['ID'], '_bbl_original_post_author', get_current_user_id() );
    	}
    
    	return $post_data;
    }, 10, 4 );
    
    

    I had used $post_data(which only contained basic details) instead of $_POST.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45254

    YES YES YES! It worked perfectly Brajesh

    Thanks so much Brajesh ive been looking for a solution to anononymous content for a long time now.

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #45261

    You are welcome 🙂
    I am glad to be of service.

    Regards
    Brajesh

The topic ‘ [Resolved] Buddyblog change author if post is published in a specific category’ is closed to new replies.

This topic is: resolved