让代码更简单

使用WordPress Setting API实现自定义设置页面

重要:本文最后更新于2023-05-20 15:36:14,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗

wordpress后台的自定义设置页面实现方法很多,各种框架。wordpress官方也提供了一套设置页面的api,使用它你将获得一个完美适合wordpress后台的页面。当然不使用它也是可以的,比如狗哥就喜欢直接写html,因为不用记函数。

下面是一个使用WordPress Setting API实现自定义设置页面demo,复制下面的代码保存为my-custom-plugin.php文件,并在插件目录中创建一个my-custom-plugin文件夹,就能在后台插件列表中看见它了。

复制
<?php
/**
 * Plugin Name: My Custom Plugin
 * Description: A simple custom plugin with a settings page.
 * Version: 1.0
 * Author: Your Name
 */

// Add menu item for the settings page
add_action('admin_menu', 'my_custom_plugin_menu');

function my_custom_plugin_menu() {
    add_options_page('My Custom Plugin Settings', 'My Custom Plugin', 'manage_options', 'my-custom-plugin-settings', 'my_custom_plugin_settings_page');
}

// Display the settings page
function my_custom_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h1>My Custom Plugin Settings</h1>
        <form method="post" action="options.php">
            <?php
            settings_fields('my-custom-plugin-settings-group');
            do_settings_sections('my-custom-plugin-settings');
            submit_button();
            ?>
        </form>
    </div>
    <?php
}

// Register settings, sections, and fields
add_action('admin_init', 'my_custom_plugin_settings_init');

function my_custom_plugin_settings_init() {
    register_setting('my-custom-plugin-settings-group', 'my_custom_plugin_text');
    register_setting('my-custom-plugin-settings-group', 'my_custom_plugin_checkbox');
    register_setting('my-custom-plugin-settings-group', 'my_custom_plugin_select');

    add_settings_section('my-custom-plugin-settings-section', 'Settings', null, 'my-custom-plugin-settings');

    add_settings_field('my_custom_plugin_text', 'Text Input', 'my_custom_plugin_text_callback', 'my-custom-plugin-settings', 'my-custom-plugin-settings-section');
    add_settings_field('my_custom_plugin_checkbox', 'Checkbox', 'my_custom_plugin_checkbox_callback', 'my-custom-plugin-settings', 'my-custom-plugin-settings-section');
    add_settings_field('my_custom_plugin_select', 'Select Dropdown', 'my_custom_plugin_select_callback', 'my-custom-plugin-settings', 'my-custom-plugin-settings-section');
}

// Callback functions for input fields with descriptions and labels
function my_custom_plugin_text_callback() {
    $text = get_option('my_custom_plugin_text');
    echo '<input type="text" name="my_custom_plugin_text" value="' . esc_attr($text) . '">';
    echo '<p class="description">Enter some text here.</p>';
}

function my_custom_plugin_checkbox_callback() {
    $checkbox = get_option('my_custom_plugin_checkbox');
    echo '<label><input type="checkbox" name="my_custom_plugin_checkbox" value="1"' . checked(1, $checkbox, false) . '> Enable this option.</label>';
}

function my_custom_plugin_select_callback() {
    $select = get_option('my_custom_plugin_select');
    ?>
    <select name="my_custom_plugin_select">
        <option value="option1" <?php selected($select, 'option1'); ?>>Option 1</option>
        <option value="option2" <?php selected($select, 'option2'); ?>>Option 2</option>
        <option value="option3" <?php selected($select, 'option3'); ?>>Option 3</option>
    </select>
    <p class="description">Choose an option from the dropdown.</p>
    <?php
}

基本上一般的设置够用了,如果需要更强的组件就需要别的框架了。

感觉很棒!可以赞赏支持我哟~

0 打赏

评论 (0)

登录后评论
QQ咨询 邮件咨询 狗哥推荐