How to target a settings page?
Support › MB Settings Page › How to target a settings page?
- This topic has 1 reply, 2 voices, and was last updated 1 year, 7 months ago by
Peter.
-
AuthorPosts
-
August 31, 2023 at 4:26 PM #43099
Patrik Jansson
ParticipantI don't understand how to target a settings page
The below code works perfect on custom post types etc, but I have not been able how to target this on a custom field inside a settings page with ID for example 'settingspageid'?// Reusable sanitization function
function my_sanitize_telefonnummer_field($value) {
// Remove non-numeric characters, including spaces and '+'
$value = preg_replace('/[^0-9+]/', '', $value);// Remove leading zeros
$value = ltrim($value, '0');// Default country code
$default_country_code = '0046';// Check if the value starts with '+'
if (strpos($value, '+') === 0) {
// Extract the country code from the value
$country_code = '';
$i = 1;
while ($i < strlen($value) && is_numeric($value[$i])) {
$country_code .= $value[$i];
$i++;
}
// Remove the country code from the value
$value = substr($value, $i);// Use the extracted country code or fall back to the default
$country_code = !empty($country_code) ? '00' . $country_code : $default_country_code;// Prepend the country code to the sanitized value
$value = $country_code . $value;
} else {
// Use the default country code
$value = $default_country_code . $value;
}return $value;
}// Update sanitized_telefonnummer field when telefonnummer is saved
function update_sanitized_telefonnummer($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;$telefonnummer = get_post_meta($post_id, 'telefonnummer', true);
if (!empty($telefonnummer)) {
// Apply the new sanitization logic
$sanitized_telefonnummer = my_sanitize_telefonnummer_field($telefonnummer);// Update the second custom field with the sanitized value
update_post_meta($post_id, 'sanitized_telefonnummer', $sanitized_telefonnummer);
} else {
// If telefonnummer is empty, delete the second custom field
delete_post_meta($post_id, 'sanitized_telefonnummer');
}
}
add_action('save_post', 'update_sanitized_telefonnummer');August 31, 2023 at 10:40 PM #43106Peter
ModeratorHello,
I see you use the function
get_post_meta()
to get the custom field value, so you can use the functionget_option()
to get the option value (settings page).Or use the helper function
rwmb_meta()
, follow the documentation https://docs.metabox.io/extensions/mb-settings-page/#getting-field-value -
AuthorPosts
- You must be logged in to reply to this topic.