下面是我的简单插件设置页面代码。它工作并保存一个选项(“EN”、“CZ”)。
但是,我要做的是在保存选项后运行一个函数(见最下面)。我使用wordpress函数:
do_action( "update_option_{$option}", mixed $old_value, mixed $value, string $option )
有趣的是,上周同样的代码也起了作用;现在,我在函数中尝试回显的任何内容都不会被打印出来,所以我假设函数甚至没有开始运行。
谁能给我指一下这里的正确方向吗?
<?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);