admin管理员组文章数量:1293548
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);
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);
Share
Improve this question
edited May 3, 2021 at 11:37
Kristýna Šulcová
asked Apr 28, 2021 at 10:07
Kristýna ŠulcováKristýna Šulcová
255 bronze badges
2
- 1 can you fix the indenting of your code? it's very confusing and difficult to read – Tom J Nowell ♦ Commented Apr 28, 2021 at 10:24
- @TomJNowell I have made an honest attempt at fixing the indentation, but I'm a newbie and I'm not sure I know what the best practices are... is it any better now? – Kristýna Šulcová Commented May 3, 2021 at 11:38
1 Answer
Reset to default 0Answer: 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.
本文标签: pluginsupdateoptionoption not working (do function after options are saved)
版权声明:本文标题:plugins - update_option_{$option} not working (do function after options are saved) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741579414a2386482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论