1

The plan is for users to be able to store locations and have notes and other variables for each location. These locations would later be shown on a map, so I would need to be able to quickly access the data also. I was just wondering what the best way to go about this would be before I try to get things set up with the map api so I don't have to go back and change everything later. Thanks.

1 Answer 1

1

You should store additional user data in wp_usermeta table. For that, use add_user_meta() function. Here's an example of how you can store locations

$location = array('name'   => 'example location name',
                  'street' => 'example street name',
                  'note'   => 'example user note'
                 );
add_user_meta($user_id, 'location', $location);

You can store as many locations as you want. To retrieve the locations use get_user_meta() They will be returned as array of locations.

$locations = get_user_meta($user_id, 'location');
foreach($locations as $location) {
    echo $location['name'];
}

To update a particular location, you can use update_user_meta(). But it's a little tricky as every location will have the same meta_key named location. So, you need to also send the previous meta_value to let Wordpress know exactly which row you want to update. Here's a example to update a location with street name abcd street

foreach($locations as $location) {
    if($location['street'] = 'abcd street') {
        $new_location = array('name'   => 'Modified location',
                              'street' => 'Modified street',
                              'note'   => 'Modified Note'
                             );
        update_user_meta($user_id, 'location', $new_location, $location);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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