简体   繁体   中英

How do I show admin_notice after submitting a custom form which is rendered by function in WordPress plugin?

I am developing a simple WordPress plugin where admin can create user roles using an admin form. I want to display an admin_notice if a role is already existing.

Problem I am having is even the role exists, the message is not appearing. If I understand properly, this is because the form is actually being redirected to admin-post.php . If so, how can I show the admin_notice after submission?

In short I have this:

  • One function in my plugin to render the form.
  • One function to save. Before saving, WP checks whether role exists. If yes, I want to display an admin_notice and "return". Otherwise, role gets saved.

The code snippet to check and show admin_notice :

<?php

function save_user_role()
{
    $role_name = $_POST['role_name'];
    $role_slug = sanitize_title_with_dashes($role_name);
    $role = get_role($role_slug);
    if (!empty($role))
    {
        $notice = 'Role exists';
        do_action('admin_notices', $this->display_notice($notice));

        // return; // If I use the return the correct message coming up in a blank page.

    }
    else
    {

        // Save the role

    }
}

Below is the full code that I have written

function save_user_role() {
   // Role already exists?
   $role_name = $_POST[ 'coolmedia_role_name' ];
   $role_slug = sanitize_title_with_dashes( $role_name );
   $role = get_role( $role_slug );

   if( ! empty( $role ) ) {
      // Error. Role already exists
      $notice = 'Speficed role ' . $role_name . ' already exists';
      do_action( 'admin_notices', $this->display_notice( $notice ) );
   } else {
     // Safe to create the new role
     $role_caps = array(
       'read'  => true,
     );

     ...

     add_role( $role_slug, esc_html( $role_name ), $role_caps );

     // Redirect back to form page
     wp_redirect( admin_url( 'admin.php?page=user-roles' ) );
   }
}

Display notice function:

function add_user_role_markup() { ?>
     <div class="wrap">
        <h1>Add Role</h1>
        <form method="post" action="<?php esc_html(admin_url('admin-post.php')); ?>">
        <table class="form-table">
           <tr class="first">
              <th>Role name</th>
              <td><input required type="text" id="role_name" name="role_name" /></td>
           </tr>
           <tr>
               <th>
                   <?php
('coolmedia-role-save', 'coolmedia-role-nonce'); ?>
                   <input type="hidden" name="action" value="new_user_role">
               </th>
               <td><?php
('Save Role'); ?></td>
           </tr>
        </table>
        </form>
</div>
<?php
}

It depends on when save_user_role is being called and what else happens after that function. It's possible that the admin_notices hook is firing, but that (as you suggest) there's a redirect happening which would cause it not to fire after the redirect because (rightfully) save_user_role isn't being called again, or that save_user_role is firing after the admin_notices hook is called (in which case you're too late).

One solution might be to temporarily store an option in the database with your notice, and then check for it and display it as needed if it exists. This way, WordPress can do its redirect and then check for and display your notice when it returns.

Something like this. (Note: my add_action assumes this is happening in a class which I assume is how you've structured it based on your use of $this->display_notice() )

function save_user_role(){
  ...
  if( ! empty( $role ) ) {
    // Error. Role already exists
    $notice = 'Specified role ' . $role_name . ' already exists';
    update_option( 'coolmedia_role_exists_message', $notice, 'no' );
  }
  ...
}

add_action( 'admin_notices', array( $this, 'maybe_display_notice' ) );
function maybe_display_notice(){
  $notice = get_option( 'coolmedia_role_exists_message', false );
  if( $notice ){
    delete_option( 'coolmedia_role_exists_message' );
    $this->display_notice( $notice );
  }
}

function display_notice( $notice ){
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM