Skip to main content
deleted 3 characters in body
Source Link
Buttered_Toast
  • 2.8k
  • 1
  • 8
  • 21

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']));
}

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']));
}

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']));
}
Source Link
Buttered_Toast
  • 2.8k
  • 1
  • 8
  • 21

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']));
}