admin管理员组

文章数量:1326287

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.

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.

Share Improve this question asked Jun 18, 2015 at 4:00 MikeMike 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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);
    }
}

本文标签: databasewhat is the best way to store user created data