0

The code I have is not working cause the value saved as array and I'm not sure how to fix this.

// Remove favorite authors from current users DB
function fav_author_remove_user(){
    check_ajax_referer( 'fav_authors_obj_ajax', 'security' );
    $remove_this_author = $_POST['clicked_author_id'];

    if ( current_user_can( 'edit_posts' ) ){
        $user_id = fav_authors_get_user_id();

        delete_user_meta( $user_id, FAV_AUTHORS_META_KEY, $remove_this_author );
    }
}
add_action( 'wp_ajax_remove_fav_author_id', 'fav_author_remove_user' );

I'm passing an author_ID on $remove_this_author and have to remove it out of a:3:{i:0;s:1:"4";i:1;s:1:"2";i:2;s:1:"3";}.

2

Thanks yo Howdy_McGee, Here is working code.

// Remove favorite authors from current users DB
function fav_author_remove_user(){
    check_ajax_referer( 'fav_authors_obj_ajax', 'security' );
    $remove_this_author = $_POST['clicked_author_id'];

    if ( current_user_can( 'edit_posts' ) ){
        $user_id = fav_authors_get_user_id();

        $author_list = get_user_meta( $user_id, FAV_AUTHORS_META_KEY, true );
        //print_r($author_list);
        $author_saved = array_search($remove_this_author, $author_list);
        if( FALSE !== $author_saved ){
            // Remove $author_saved
            unset($author_list[$author_saved]);
            $author_arr = ( is_array( $author_list ) ) ? $author_list : array( $author_list );
            update_user_meta( $user_id, FAV_AUTHORS_META_KEY, $author_arr );
        }
    }
}
add_action( 'wp_ajax_remove_fav_author_id', 'fav_author_remove_user' );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.