On clicking on a div, I get the "data-animal" attribute (js function).
<div data-animal="dog"></div>
<div data-animal="cat"></div>
<div data-animal="pig"></div>
With my ajax call, I send it to this php function:
add_action( 'wp_ajax_animals', 'user_animals' );
add_action( 'wp_ajax_nopriv_animals', 'user_animals' );
function user_animals() {
$user = wp_get_current_user();
$animals_all = $_POST['animal'];
// get every animal as single row
add_user_meta($user->ID, 'animals_all' , $animal);
// get all animals of the same user
$wp_user_search = new WP_User_Query( array( 'fields' => 'animal' ) );
$get_users = $wp_user_search->get_results();
// now put all animals into one row
update_user_meta($user->ID, 'animals_all' , $get_users);
// delete the single row(s)
delete_user_meta($user->ID, 'animal');
}
My problem: WordPress serialize the data automatically. How can I undo or prevent WordPress from doing that? maybe_serialize / maybe_unserialize isn’t working.
How it is at my sql at the moment (just an example):
animals_all | a:8:{i:0;s:31:"cat";i:1;s:57:"dog";i:2;s:19: ...
How I need it at my sql:
animals_all | cat,dog,pig