admin管理员组

文章数量:1122832

I am currently developing a WordPress plugin called FlexiPress. I've created a settings page in the WordPress dashboard where users can configure various features of the plugin.

I initially used a manual approach to create the form and manage the options, but I heard about the WordPress Settings API, which seems to be a cleaner and more secure way to manage plugin settings.

Could someone help me convert my current settings page using the WordPress Settings API? Indeed, I've read the WP doc but I still haven't figured out how to use the Settings API for my plugin's settings page. Here's the code for that settings page:

<?php
// File : admin/general-settings.php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

// Include feature files
require_once(plugin_dir_path(__FILE__) . '/../includes/disable-automatic-updates-emails/disable-automatic-updates-emails.php');
require_once(plugin_dir_path(__FILE__) . '/../includes/disable-attachment-pages/disable-attachment-pages.php');
require_once(plugin_dir_path(__FILE__) . '/../includes/completely-disable-comments/completely-disable-comments.php');

// Checks if features are enabled
$disableautomaticupdatesemails_enabled = get_option('flexipress_general_enabled_disableautomaticupdatesemails', false);
$disableattachmentpages_enabled = get_option('flexipress_general_enabled_disableattachmentpages', false);
$completelydisablecomments_enabled = get_option('flexipress_general_enabled_completelydisablecomments', false);

// Form processing during submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_changes'])) {
    $disableautomaticupdatesemails_enabled = isset($_POST['disableautomaticupdatesemails_enabled']) && $_POST['disableautomaticupdatesemails_enabled'] === 'on';
    $disableattachmentpages_enabled = isset($_POST['disableattachmentpages_enabled']) && $_POST['disableattachmentpages_enabled'] === 'on';
    $completelydisablecomments_enabled = isset($_POST['completelydisablecomments_enabled']) && $_POST['completelydisablecomments_enabled'] === 'on';

    // Records toggle switch status
    update_option('flexipress_general_enabled_disableautomaticupdatesemails', $disableautomaticupdatesemails_enabled);
    update_option('flexipress_general_enabled_disableattachmentpages', $disableattachmentpages_enabled);
    update_option('flexipress_general_enabled_completelydisablecomments', $completelydisablecomments_enabled);
}


// Displays the form and switch toggles
?>
<div class="wrap" id="flexipress-plugin">
    <h2><?php esc_html_e('General Settings', 'flexipress'); ?></h2>

    <form method="post" action="">
        <?php
        // Features and their status
        $features = array(
            'disableautomaticupdatesemails' => __('Disable Automatic Updates Emails', 'flexipress'),
            'disableattachmentpages' => __('Disable Attachment Pages', 'flexipress'),
            'completelydisablecomments' => __('Completely Disable Comments', 'flexipress'),
            // Add other features as needed
        );

        foreach ($features as $key => $label) :
            $enabled = get_option("general_enabled_$key", false);
        ?>
            <div class="feature-toggle-pair">
                <label class="switch">
                    <input type="checkbox" name="<?php echo esc_js( $key ); ?>_enabled" <?php checked($enabled); ?>>
                    <span class="toggle-slider round"></span>
                </label>
                
                <div class="feature-details">
                    <h3><?php echo esc_html( $label ); ?></h3>
                    <?php if ($key === 'disableautomaticupdatesemails'): ?>
                        <p><?php esc_html_e('Stop getting emails about automatic updates on your WordPress site.', 'flexipress'); ?></p> <!-- Function description -->
                    <?php elseif ($key === 'disableattachmentpages'): ?>
                        <p><?php esc_html_e('Hide the Attachment/Attachments pages on the frontend from all visitors.', 'flexipress'); ?></p> <!-- Function description -->
                    <?php elseif ($key === 'completelydisablecomments'): ?>
                        <p><?php esc_html_e('Disable comments for all post types, in the admin and the frontend.', 'flexipress'); ?></p> <!-- Function description -->
                    <?php else: ?>
                        <p>.</p> <!-- Default description -->
                    <?php endif; ?>
                </div>

            </div>
        <?php endforeach; ?>

        <button type="submit" name="save_changes"><?php esc_html_e('Save Changes', 'flexipress'); ?></button>
    </form>
</div>

If anyone is familiar with the WordPress Settings API and can offer me some help, it would be extremely helpful and I would be very grateful.

Thank you very much for your help! Alex

本文标签: How can I use the WordPress Settings API to create a settings page for my plugin