我正在尝试更新核心WordPress选项,image_default_link_type
, 但WordPress拒绝更新它。插件本身注册的选项将更新。下面是一个简单的示例:
add_action( \'init\', \'wpse_register_plugin_settings\' );
add_action( \'admin_menu\', \'wpse_add_plugin_admin_menu_entry\' );
/**
* Register a plugin setting.
*/
function wpse_register_plugin_settings() {
add_settings_field(
\'my_plugin_settings\',
__( \'Settings\', \'my-plugin\' ),
\'wpse_display_settings_field\',
\'my-plugin-admin-options\',
);
register_setting(
\'my-plugin-admin-options\',
\'my_plugin_setting\',
);
}
/**
* Callback function for the plugin settings section.
*/
function wpse_display_settings_field() {
require_once plugin_dir_path( \'my-plugin\' ) . \'partials/admin/settings/section-display.php\';
}
/**
* Add the settings page as an entry in the admin menu.
*/
function wpse_add_plugin_admin_menu_entry() {
add_options_page(
__( \'My Plugin Settings\', \'my-plugin\' ),
__( \'My Plugin\', \'my-plugin\' ),
\'manage_options\',
\'my-plugin-setting-page-slug\',
\'wpse_output_plugin_settings_page\'
);
}
/**
* Callback function for the plugin settings page.
*/
function wpse_output_plugin_settings_page() {
// Check user has permissions.
if ( ! current_user_can( \'manage_options\' ) ) {
wp_die(
\'<h1>\' . esc_html__( \'You need a higher level of permission.\', \'my-plugin\' ) . \'</h1>\' .
\'<p>\' . esc_html__( \'Sorry, you are not allowed to use the plugin admin settings.\', \'my-plugin\' ) . \'</p>\',
403
);
}
require_once plugin_dir_path( \'my-plugin\' ) . \'partials/admin/settings/display.php\';
}
目录
section-display.php
:
<label for="my_plugin_setting">
<input type="checkbox" name="my_plugin_setting" id="my_plugin_setting" value="1" <?php checked( get_option( \'my_plugin_setting\' ) ); ?> />
<?php esc_html_e( \'Some setting\', \'my-plugin\' ); ?>
</label>
<br/>
<select name="image_default_link_type">
<!-- some options -->
</select>
目录
display.php
:
<?php
/**
* Plugin settings.
*/
?>
<div class="wrap">
<h2><?php esc_html_e( \'My Plugin Settings\', \'my-plugin\' ); ?></h2>
<div>
<form method="post" action="options.php">
<?php settings_fields( \'my-plugin-admin-options\' ); ?>
<?php do_settings_sections( \'my-plugin-admin-options\' ); ?>
<?php submit_button( esc_html__( \'Save Changes\' ) ); ?>
</form>
</div>
</div>
WordPress似乎正在阻止更新设置。如何通过插件的设置页面启用对核心选项的更新?