i make this but not working, on end is full code
- Hook into User Registration: The code uses the
add_action
function to hook into thehivepress/v1/models/user/register
action, which is triggered when a new user registers. - Define the Listing ID: It sets the ID of the listing that should be added to the user’s favorites. In this case, the listing ID is
127
. - Retrieve Current Favorites: It retrieves the current list of favorite listings for the newly registered user using the
get_user_meta
function. If the user has no favorites yet, it initializes an empty array. - Add Listing to Favorites: It checks if the listing with ID
127
is already in the user’s favorites. If not, it adds the listing ID to the array of favorites. - Update User Meta: Finally, it updates the user’s meta data with the new list of favorite listings using the
update_user_meta
function.
In summary, this code ensures that every new user will have the listing with ID 127
automatically added to their list of favorite listings upon registration.
what is bad?
add_action(
'hivepress/v1/models/user/register',
function($user_id, $values){
$listing_id = 127; // ID of the listing to add as favorite
// Get the current user's favorite listings
$favorites = get_user_meta($user_id, 'hp_favorites', true);
if (!$favorites) {
$favorites = array();
}
// Add the listing to favorites if it's not already there
if (!in_array($listing_id, $favorites)) {
$favorites[] = $listing_id;
update_user_meta($user_id, 'hp_favorites', $favorites);
}
},
1000,
2
);