WordPress后台自定义子页面无法将信息保存至数据库
Hey there, let’s work through this frustrating issue where your post-format selection subpage isn’t saving settings—even though the option is registered in wp_options. Since it worked before, the problem’s likely a small oversight in your code that crept in between your second and third use. Here are the most common fixes to check:
1. Verify Nonce Implementation (Most Likely Culprit)
WordPress uses nonces to prevent CSRF attacks, and missing or incorrect nonce validation will silently block your save action without an error message.
- Check your form code: Make sure you’re outputting a nonce field inside your subpage’s form:
<?php // Add this inside your form tag wp_nonce_field('mytheme_save_post_formats', 'mytheme_post_formats_nonce'); ?> - Check your save handler: Ensure you’re validating the nonce before processing the save:
function mytheme_save_post_formats_settings() { // Validate nonce first if (!isset($_POST['mytheme_post_formats_nonce']) || !wp_verify_nonce($_POST['mytheme_post_formats_nonce'], 'mytheme_save_post_formats')) { wp_die('Invalid nonce verification.'); } // Rest of your save logic... } add_action('admin_post_save_mytheme_post_formats', 'mytheme_save_post_formats_settings');
2. Confirm User Permission Checks
If your save function isn’t verifying the user has the right permissions, WordPress will block the save action.
Add this at the top of your save handler:
if (!current_user_can('manage_options')) { wp_die(__('You don’t have sufficient permissions to modify these settings.')); }
Also, double-check that the capability parameter when registering your subpage matches the permission you’re checking (e.g., manage_options).
3. Fix Data Sanitization/Validation
If your input data is being sanitized incorrectly, it might turn into an empty value or get rejected entirely. For post formats (which are slugs like aside, gallery), use this to clean the input:
// Assuming your form field is named 'mytheme_selected_formats' $selected_formats = isset($_POST['mytheme_selected_formats']) ? array_map('sanitize_key', $_POST['mytheme_selected_formats']) : array(); update_option('mytheme_active_post_formats', $selected_formats);
Avoid over-sanitizing—using sanitize_text_field on an array will break it, so use array_map for multi-select fields.
4. Avoid Field Name Conflicts
Generic field names like post_formats might clash with core WordPress or other plugin fields. Rename your form fields and option key to something unique (e.g., prefix with your theme’s slug: mytheme_selected_formats instead of post_formats).
5. Check Hook Priority and Form Action
Ensure your save handler is hooked to the right action, and your form’s action attribute points correctly:
- Set your form’s action to:
<form method="post" action="<?php echo admin_url('admin-post.php'); ?>"> <input type="hidden" name="action" value="save_mytheme_post_formats"> <!-- Rest of your form fields --> </form> - Hook your save function to
admin_post_{your_action_name}(as shown in the nonce example above) instead of generic hooks likeadmin_init, which can lead to timing issues.
6. Rule Out Cache Interference
If you’re using a caching plugin, clear your site and admin cache temporarily. Sometimes cached versions of the admin page can make it look like settings aren’t saving, even if they are. You can also disable caching plugins briefly to test.
Quick Full Example Reference
Here’s a condensed working snippet to compare against your code:
// Register subpage function mytheme_add_post_formats_subpage() { add_submenu_page( 'mytheme_main_admin_page', // Parent page slug 'Post Formats Settings', 'Post Formats', 'manage_options', 'mytheme_post_formats', 'mytheme_render_post_formats_subpage' ); } add_action('admin_menu', 'mytheme_add_post_formats_subpage'); // Render subpage form function mytheme_render_post_formats_subpage() { $active_formats = get_option('mytheme_active_post_formats', array()); ?> <div class="wrap"> <h1>Post Formats Settings</h1> <form method="post" action="<?php echo admin_url('admin-post.php'); ?>"> <?php wp_nonce_field('mytheme_save_post_formats', 'mytheme_post_formats_nonce'); ?> <input type="hidden" name="action" value="save_mytheme_post_formats"> <h2>Select Post Formats to Activate</h2> <?php $formats = get_post_format_slugs(); foreach ($formats as $format) { ?> <label> <input type="checkbox" name="mytheme_selected_formats[]" value="<?php echo esc_attr($format); ?>" <?php checked(in_array($format, $active_formats)); ?>> <?php echo ucfirst($format); ?> </label><br> <?php } ?> <?php submit_button('Save Settings'); ?> </form> </div> <?php } // Save handler function mytheme_save_post_formats_settings() { // Validate nonce and permissions if (!isset($_POST['mytheme_post_formats_nonce']) || !wp_verify_nonce($_POST['mytheme_post_formats_nonce'], 'mytheme_save_post_formats')) { wp_die('Invalid nonce.'); } if (!current_user_can('manage_options')) { wp_die('Insufficient permissions.'); } // Sanitize and save data $selected_formats = isset($_POST['mytheme_selected_formats']) ? array_map('sanitize_key', $_POST['mytheme_selected_formats']) : array(); update_option('mytheme_active_post_formats', $selected_formats); // Redirect back to subpage with success message wp_redirect(add_query_arg('settings-updated', 'true', admin_url('admin.php?page=mytheme_post_formats'))); exit; } add_action('admin_post_save_mytheme_post_formats', 'mytheme_save_post_formats_settings');
Go through each of these checks step by step—chances are one of these fixes will resolve your save issue.
内容的提问来源于stack exchange,提问作者Marcos Di Paolo

