1

I'm looking to trigger a message upon the selection of a specific user role on the WP admin user-edit.php page. (Perhaps this could be a div that is hidden and upon selection of the specific role from the dropdown the div is unhidden).

I have seen what could be a workable solution in #5 here but I don't know how to insert the on change triggers into the role select dropdown of the user edit page.

Any advice/pointers would be most appreciated.

1 Answer 1

0

You can use jQuery or plain javascript to add event listener to the role select element. E.g.

jQuery(document).on('ready', function(){

    jQuery('select#role').on('change', function(){
      // some if statement here
      alert('Role changed');
    });

  });

To add this to your admin you can either use admin_enqueue_scripts or admin_footer hooks.

To enqueue script file,

function my_admin_enqueue_scripts( $hook_suffix ) {
  if ( 'user-edit.php' === $hook_suffix || 'user-new.php' === $hook_suffix ) {
    wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false );
  }
} 
add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');

To inline the script in admin footer,

function my_admin_enqueue_scripts() {
  global $pagenow;
  if ( 'user-edit.php' !== $pagenow && 'user-new.php' !== $pagenow ) {
    return;
  }
  ?>
  <script>
  jQuery(document).on('ready', function(){

    jQuery('select#role').on('change', function(){
      alert('Role changed');
    });

  });
  </script>
  <?php
}
add_action('admin_footer', 'my_admin_enqueue_scripts');

If there's a popup you want to show, when a certain role is selected, then you could add the popup html to the admin footer and have the change action make it visible. Should you need styling for the popup, use wp_enqueue_style() in admin_enqueue_scripts.

P.s I added the user-new.php check as an extra example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.