I want to add a little code snippet to the edit page if the current user is of a specific user role.
I do know how to check for the userrole in php, but I have no clue how to find out if the page is an edit post or edit pages page and how to add the snippet with php. Please note that jQuery must be available at this point.
I don't want to user wp_enqueue_scripts
because it really only is a little snippet and I don't want to create a new file for this. The code should happily live inside my functions.php
Edit
According to Greg McMullen's answer I wrote the following which works well.
function function_to_run() {
if ($_GET['action'] != "edit"){
return;
}
$user_roles = array('role1', 'role2');
foreach ($user_roles as $user_role) {
if( current_user_can($user_role) ) {
wp_enqueue_script( 'pending_post', get_template_directory_uri() . '/js/script1.js' );
}
else {
wp_enqueue_script( 'pending_post', get_template_directory_uri() . '/js/script2.js' );
}
}
}
add_action( 'admin_enqueue_scripts', 'function_to_run' );
However, I think that it's very dirty to include 2 different files that contain 2 lines of code each.
Even if it's not best practice, I would like to add the js code to the php file. How is that possible?