最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Show admin notice on incorrect value on form field

programmeradmin1浏览0评论

So I'm still learning about the Settings API on WordPress and can't seem to figure out how to handle admin notices on an empty database and then have validation.

Registration:

function register_insta_heading()
{
    add_settings_section(
        "", //1. Linked to: add_settings_section(section)
        "", //Section title
        null,
        "insta_heading_field"); //1. Linked to: add_settings_field(page)

    add_settings_field(
        "",
        "Heading", //Add the 'Access Token' title
        "heading_display", //Call posts_number_display function
        "insta_heading_field", //2. Linked to: do_settings_field
        "");

    register_setting(
        'instagram-settings',
        'insta_heading' );
}

Display:

function heading_display() {
    ?>
    <label for="insta_heading">
        <?php if (empty(get_option('insta_heading'))): ?>
            <div class="notice notice-error is-dismissible">
                <p><?php _e('Please input a module heading', 'instagram-enabler'); ?></p>
            </div>
            <input type="text" name="insta_heading" id="insta_heading" value="<?php echo get_option('insta_heading'); ?>" size="64" class="regular-text code" />
        <?php else: ?>
            <input type="text" name="insta_heading" id="insta_heading" value="<?php echo get_option('insta_heading'); ?>" size="64" class="regular-text code" />
        <?php endif; ?>
    </label>
    <p class="description">
        <?php _e("Enter the module heading", "instagram-enabler"); ?>
    </p>
    <?php
}
add_action( 'admin_init', 'register_insta_heading' );

Show:

function instagram_showpage() {
    ?>
    <div class="wrap">
    <h2>Instagram</h2>
    <form method="post" action="options.php">
        <table class="form-table">
            <?php
                settings_fields( 'instagram-settings' );
                do_settings_sections("insta_heading_field");
                submit_button();
            ?>
        </table>
    </form>
    </div><?php
}
发布评论

评论列表(0)

  1. 暂无评论