• 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.

    **What currently happens**:

    On load, with the empty value in the database it shown an admin panel (Don’t need this on initial load)

    **What I’ll like to do**:
    – Show admin notice on correct validation using the script at the bottom.
    – How can I validate and reuse the ‘valid’ option if an user enters a bad value later?

    *Image for reference*:
    [![enter image description here][1]][1]

    ***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
        }
    

    *So I found this script online and would like to utilize it for the heading section*:

    
        function sanitize_number_callback ($input){
        
            if( !preg_match( '/...regex for valid here.../', $input ) ){
                add_settings_error(
                    'my_option',
                    esc_attr( 'my_option' ), //becomes part of id attribute of error message
                    __( 'Number must be a positive integer', 'wordpress' ), //default text zone
                    'error'
                );
                $input = get_option( 'my_option' ); //keep old value
            }
        
            return $input;
        }
    

    [1]: https://i.stack.imgur.com/yCkih.png

    • This topic was modified 4 years, 7 months ago by semirxbih.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Adding validation on text field using Settings API’ is closed to new replies.