admin管理员组文章数量:1391736
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.
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.
Share Improve this question asked Feb 6, 2020 at 14:00 apalinapalin 155 bronze badges1 Answer
Reset to default 0You 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.
本文标签: Adding javascript trigger to user role selection on the useredit page
版权声明:本文标题:Adding javascript trigger to user role selection on the user-edit page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744766306a2624062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论