update_option_{$option} not working (do function after options are saved)
-
20-05-2021 - |
質問
Below is my code for a simple plugin settings page. It works and saves an option ("EN", "CZ").
However, what I intend to do is run a function after the options get saved (see at the very bottom). I use this wordpress function for it:
do_action( "update_option_{$option}", mixed $old_value, mixed $value, string $option )
Funnily enough, the very same code did work last week; right now, anything I try to echo inside the function just doesn't get printed, so I assume the function doesn't even start running.
Could anyone please point me in the right direction here?
<?php
add_action( 'admin_menu', 'cmi_csv_import_plugin_page' );
// tick
function cmi_csv_import_plugin_page() {
add_options_page(
'CSV Import - PUDR', // page <title>Title</title>
'CSV Import', // menu link text
'manage_options', // capability to access the page
'cmi-csv-import', // page URL slug
'cmi_csv_page_content', // callback function /w content
5 // priority
);
}
function cmi_csv_page_content () {
?>
<div class="wrap">
<h2>Import CSV</h2>
<form action="options.php" method="post">
<?php
// this is what the setting is called and how you retrieve the option later! (with get_options)
settings_fields( 'cmi_lang' );
// slug name of the page whose settings sections you want to output
// Use this in a settings page callback function to output all the
// sections and fields that were added to that $page with add_settings_section() and add_settings_field()
do_settings_sections( 'cmi-csv-import' );
submit_button( 'Aktualizovat metadata', 'primary' );
?>
</form>
</div>
<?php
}
// add an action on admin init
//
//
//
add_action ('admin_init', 'cmi_csv_admin_init');
function cmi_csv_admin_init () {
// a/ register settings
$args = array(
'type' => 'string',
// ↓ callable
'sanitize_callback' => 'cmi_csv_validate_options',
'default' => NULL
);
register_setting ('cmi_lang', 'cmi_lang', $args);
// b/ add a settings section
add_settings_section (
'cmi-csv-section-main',
'Import CSV (metadata PUDR)',
// callable ↓ - echoes anything in the section
'cmi_csv_section_text',
// page name
'cmi-csv-import'
);
// c/ add a settings field
add_settings_field (
'cmi_lang',
'Jazyk aktualizovaného pole',
// ↓ callback
'cmi_csv_setting_jazyk',
'cmi-csv-import',
'cmi-csv-section-main'
);
};
function cmi_csv_section_text() {
echo '<p>Nastavte jazyk.</p>';
};
function cmi_csv_setting_jazyk () {
// Get option 'beast_mode' value from the database
// Set to 'disabled' as a default if the option does not exist
$options = get_option( 'cmi_lang', 'EN');
// Define the radio button options
$items = array( 'EN', 'CZ');
/*$jazyk = get_option ('cmi_lang');*/
foreach( $items as $item ) {
// Loop the two radio button options and select if set in the option value
echo "<label><input " . checked( $item, $options, false) . "
value='" . esc_attr( $item ) . "' name='cmi_lang'
type='radio'/> " . esc_html( $item ) . "</label><br/>";
}
}
function cmi_csv_validate_options ($input) {
$input = sanitize_text_field( $input );
return $input;
};
add_action('update_option_cmi_lang', function($old, $new) {
if (!$new) {
echo "didn't run";
return;
};
/// DO SOME STUFF
}, 1, 2);
解決
Answer: the code above works perfectly fine; it just doesn't echo anything inside the admin section. The 'stuff' I did with the update_option hook didn't work for reasons independent of the code above.
所属していません wordpress.stackexchange