$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>ajax - Preprocess submitted data|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ajax - Preprocess submitted data

matteradmin9PV0评论

I created WordPress module, which adds my custom field in general settings in WordPress Admin. However after submitting whole form in admin settings, I need to modify my field's value before saving to database. How can I do that?

I would prefer to modify my field's data before any default WordPress form validation. Any clues? So far I spent on this hours, and could not google anything.

My code:

Located in my_module's main php file.

function my_module__section(){

    $settings_page_display = 'general';
    $section_id = 'my_module__section';
    add_settings_section(  
        $section_id,
        'Notification information',
        'my_module__section_description',
        $settings_page_display
    );

    $field_id = 'notification_text';
    $field_label = 'Name of customer';

    add_settings_field(
        $field_id,
        $field_label,
        'my_module__settings_fields_render',
        $settings_page_display,
        $section_id,
        [$field_id, 'text']
    );
    register_setting('general', $field_id, 'esc_attr');

}

function my_module__section_description() {
    echo '<p>Some description</p>';  
}

function my_module__settings_fields_render($args) {
    $field_id = $args[0];
    $field_type = $args[1];
    $option = get_option($field_id);
    echo '<input type="' . $field_type . '" id="'. $field_id .'" name="'. $field_id .'" value="' . $option . '" required>';
}

add_action('admin_init', 'my_module__section');

Note:

I am not searching for any frontend / javascript hacks - no exception.

I created WordPress module, which adds my custom field in general settings in WordPress Admin. However after submitting whole form in admin settings, I need to modify my field's value before saving to database. How can I do that?

I would prefer to modify my field's data before any default WordPress form validation. Any clues? So far I spent on this hours, and could not google anything.

My code:

Located in my_module's main php file.

function my_module__section(){

    $settings_page_display = 'general';
    $section_id = 'my_module__section';
    add_settings_section(  
        $section_id,
        'Notification information',
        'my_module__section_description',
        $settings_page_display
    );

    $field_id = 'notification_text';
    $field_label = 'Name of customer';

    add_settings_field(
        $field_id,
        $field_label,
        'my_module__settings_fields_render',
        $settings_page_display,
        $section_id,
        [$field_id, 'text']
    );
    register_setting('general', $field_id, 'esc_attr');

}

function my_module__section_description() {
    echo '<p>Some description</p>';  
}

function my_module__settings_fields_render($args) {
    $field_id = $args[0];
    $field_type = $args[1];
    $option = get_option($field_id);
    echo '<input type="' . $field_type . '" id="'. $field_id .'" name="'. $field_id .'" value="' . $option . '" required>';
}

add_action('admin_init', 'my_module__section');

Note:

I am not searching for any frontend / javascript hacks - no exception.

Share Improve this question edited Mar 15, 2019 at 21:20 Fusion asked Mar 15, 2019 at 21:01 FusionFusion 1377 bronze badges 2
  • It’s hard to comment without seeing your code – Andy Macaulay-Brook Commented Mar 15, 2019 at 21:02
  • I updated answer with code preview. – Fusion Commented Mar 15, 2019 at 21:17
Add a comment  | 

1 Answer 1

Reset to default 1

If I got your question right then maybe you could try using the update_option action, which according to the docs "Fires immediately before an option value is updated.". It gets option, old value and new value as parameters. https://developer.wordpress/reference/hooks/update_option/

The accepted answer here, Hook if somebody saves plugin options?, might also be relevant for you.

If you feel adventurous, you could add a custom sanitize callback to register_setting and use it to do some funky stuff with the setting value. But this might be a little unorthodox way.

Personally I've found out that digging into the WP source code on trac is also a good way to find useful actions and filters for your custom functions. In this case for example the option.php https://core.trac.wordpress/browser/tags/5.1.1/src/wp-includes/option.php

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far