如何创建自定义菜单项

时间:2019-08-14 作者:mrKC.988

我需要在插件中提供一些自定义菜单项。我的意思是人们可以进入仪表板>外观>菜单,并将此自定义菜单项添加到任何菜单位置。

所以我想知道如何在页面中添加元框,如何用选项填充它,以及如何在前端输出内容。

我试着搜索了很久,但在如何管理这样的事情上,我没有找到任何好的资源。

有什么帮助吗?有链接吗?非常感谢。

2 个回复
SO网友:user3135691

这是我正在开发的插件设置页面的精简版本。将其放入文件并将其包含在插件中。

在Google上搜索add_options_page, register_setting, add_settings_field 等将引导您找到WordPress的codex条目,并提供进一步的描述。

下面选项页的代码将呈现2个复选框,值设置为0或1/bool true/false。

我将把添加文本输入字段作为一个小作业留给您,因为这是大量(免费)代码。

HTML/CSS类是引导式的,带有一些特殊的代码,用于一个很好的复选框/开关。

如果答案对您有帮助,请将其标记为正确,谢谢

<?php
/* ============================
   Special Menu Settings Page
   ============================
*/
// Add the settings page
function special_menu_admin_menu() { 
    add_options_page(
        __(\'Special menu\', \'text-domain\'),          // Page Title
        __(\'Special menu\', \'text-domain\'),          // Menu Title
        \'manage_options\',                       // Capability to access it
        \'special-menu-settings\',                    // Menu Slug
        \'html_output_menu_settings_page\'            // Callback Function
    );
}
add_action(\'admin_menu\', \'special_menu_admin_menu\');

// Init the settings, which we want to save
function special_menu_settings_init() {

    // ------------------------------------------ Settings
    // Example Setting
    register_setting(
        \'special-menu-settings-group\',                      // Group Name
        \'setting_1\'                                         // Settings Name
    );

    // Setting 2
    register_setting(
        \'special-menu-settings-group\',                      // Group Name
        \'setting_2\'                                     // Settings Name
    );

    // ------------------------------------------ Sections
    add_settings_section(
        \'special-menu-settings-section-1\',                  // ID of the Element
        __(\'Special Menu settings\', \'text-domain\' ),            // Public Name
        \'\',                                             // Function to print description before actual form
        \'special-menu-settings\'                         // ID of the backend page
    );

    // ------------------------------------------- Fields

    // Setting 1
    add_settings_field(
        \'setting_1\',                                    // Name / ID 
        __(\'Special menu\', \'text-domain\'),          // Public Name
        \'special_menu_setting_1_callback\',      // Callback Function to print HTML
        \'special-menu-settings\',                            // Name of the settings page, where to put it
        \'special-menu-settings-section-1\'                   // section_where, label_for
    );

    // Setting 2
    add_settings_field(
        \'setting_2\',                                        // Name / ID 
        __(\'Enable tab 1\', \'text-domain\'),                      // Public Name
        \'special_menu_setting_2_callback\',                  // Callback Function to print HTML
        \'special-menu-settings\',                                // Name of the backend page, where to put it
        \'special-menu-settings-section-1\'                       // section_where, label_for
    );

}
add_action(\'admin_init\', \'special_menu_settings_init\');

// Render the options page in the backend
function html_output_menu_settings_page() { ?>
    <?php
        // In case you need access to the plugin folder path, e.g. use $dir . \'/images/whatever.jpg\';
        $dir = plugin_dir_path(__FILE__);
    ?>
    <div class="wrap">
        <h1 class="settings-title"><?php _e(\'Special menu settings\', \'text-domain\'); ?></h1>
        <div class="container-fluid">   
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                    <form method="post" action="options.php">
                        <?php
                            settings_fields(\'special-menu-settings-group\');
                            do_settings_sections(\'special-menu-settings\');
                            submit_button();
                        ?>
                    </form>
                </div>
            </div>
        </div>
    </div>  
    <?php
}


// Setting 1 HTML
function special_menu_setting_1_callback() { ?>
    <div class="row">
        <div class="col-md-3 text-right">
            <div class="switch">
                <input type="checkbox" class="checkbox" id="setting_1" name="setting_1" value="1" <?php checked(1, get_option(\'setting_1\'), true); ?> />
                <label for="setting_1" class="checkbox-label"></label>
            </div>
        </div>
    </div>
    <?php
}


// Setting 2 HTML
function special_menu_setting_2_callback() {
    $setting_2 = get_option(\'setting_2\');
    ?>
    <div class="row">
        <div class="col text-right">
            <div class="switch">
                <input type="checkbox" class="checkbox" id="setting_2" name="setting_2" value="1" <?php checked(1, get_option(\'setting_2\'), true); ?> />
                <label for="setting_2" class="checkbox-label"></label>
            </div>
        </div>
    </div>
    <?php
}

Bonus CSS, for the checkbox switch:

 // Switch Toggle Checkbox
.wrap-switch {
    background: #e9ecef;
    padding-top: 7px;
    padding-left: 18px;
    padding-right: 18px;
    border-top: 1px solid #ced4da;
    border-bottom: 1px solid #ced4da;
}

.switch {
    position: relative;
    float: left;
    width: 55px;
    height: 23px;
}

.switch:after {
    content: "";
    display: table;
}

.switch input[type="checkbox"] {
    visibility: hidden;
}

.switch label {
    position: absolute;
    top: 0;
    left: 0;
    width: 55px;
    height: 23px;
    background: tomato;
    border-radius: 50px;
}

.switch label:after {
    position: absolute;
    top: 1px;
    left: 1px;
    width: 21px;
    height: 21px;
    content: \'\';
    border-radius: 50px;
    background-color: white;
    transition: all .1s;
}

.switch input[type="checkbox"]:checked + label:after {
    left: 33px;
}
.switch input[type="checkbox"]:checked + label {
    background: lightgreen;
}

// On/Off Label
.input-group > .input-group-prepend > .input-group-text {
    border-right: 0;
}
.input-group > .input-group-append > .input-group-text {
    border-left: 0;
}

SO网友:Makiomar

您可以使用add_meta_box 并针对nav-menu 页请检查答案Here

相关推荐

Add extra markup to WP menus

我被要求将一个HTML网站重新开发为一个WP主题,它有一个非常高级的菜单结构,我不太确定如何复制它。第二个菜单项有一个子菜单,但它不仅仅是“li”中的“ul”,还有额外的div等。。因为下拉列表有3列布局,其中2列包含子链接,第3列包含内容。这是一个示例:<ul class=\"nav navbar-nav three\"> <li class=\"dropdown yamm-fw\"> <a href=\"#\" class=\"dropdown-toggl