0

How to Save Multiple Metabox values ?

I have 3 fields like below image.

enter image description here

Should I use below code ?

if (isset($_POST['name'])) {
    update_post_meta($post_id, 'name', sanitize_text_field($_POST['name']));
}

if (isset($_POST['address'])) {
    update_post_meta($post_id, 'address', sanitize_text_field($_POST['address']));
}

if (isset($_POST['phone'])) {
    update_post_meta($post_id, 'phone', sanitize_text_field($_POST['phone']));
}

Is there any way to Save multiple metabox values ?

1
  • 1
    Yes, that's exactly the way you should save. However, if the, let's say, $_POST['name'] is not set, the meta field name won't be created and if you need to use it for Query, you won't get desired results. Better you save an empty value if $_POST['name'] is not set. That's my own opinion though.
    – Abhik
    Jul 2 at 3:26

1 Answer 1

2

I don't know where this code exists, if it's in a callback of a ajax or post event but it doesn't really matter.

Assuming your code works as expected you could make it a bit easier to manage, lets say you have another 7 meta fields you need to update, you'll need to copy paste your if conditions every time which is not ideal.

PHP also has a built in function for dealing with requests, filter_input, this function allows you to sanitize and validate the request data exactly as you need it, there are many flag for data types and common values like email.

So you could do something like this

$fields = [
    'name'    => FILTER_SANITIZE_STRING,
    'address' => FILTER_SANITIZE_STRING,
    'phone'   => FILTER_SANITIZE_NUMBER_INT,
];

foreach ($fields as $field_name => $flag) {
    if (!empty($field = filter_input(INPUT_POST, $field_name, $flag))) {
        update_post_meta($post_id, $field_name, sanitize_text_field($field));
    }
}

So now if you need to add another field, lets say email, you can just update the $field array instead of creating a new condition. This

$fields = [
    'name'    => FILTER_SANITIZE_STRING,
    'address' => FILTER_SANITIZE_STRING,
    'phone'   => FILTER_SANITIZE_NUMBER_INT,
    'email'   => FILTER_SANITIZE_EMAIL
];

Instead of another

if (isset($_POST['email'])) {
    update_post_meta($post_id, 'phone', sanitize_text_field($_POST['email']));
}
5
  • Thanks @Buttered_Toast. Actually I am learning Plugin Development. I have Custom Post News. I would like to save some other values with this Custom Post. In this case I am using Metaboxes. I don't know whether is it proper way. I have to save almost 40 other values. You can check this screenshot. i.stack.imgur.com/fgWah.png
    – Foysal
    Jul 3 at 10:49
  • 1
    You have events you can hook into on save, the wordpress plugin development handbooks goes over that as well. Using my method at least you wont need to create 40+ if conditions =D Jul 3 at 10:54
  • Thanks @Buttered_Toast. I know there is a save hook. But am I in right way ? Is it the proper way to develop a plugin ? Thanks.
    – Foysal
    Jul 3 at 11:17
  • 1
    With the minimal code you provided I can say for sure because I don't know what the plugin is about, what I know so far is that you have a POST request and you want yo update meta data based on that request, the way you did it is correct, using sanitize_text_field before saving the data is really good and appropriate, you can make your code more maintainable as I showed. With the code you provided so far you're doing fine. continue reading the wordpress plugin development handbook and you'll be fine Jul 3 at 12:00
  • Thanks @Buttered_Toast. Did you check this post ? stackoverflow.com/questions/69207368/…
    – Foysal
    Jul 10 at 10:54

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.