Pass user role to javascript code inside body

To my understanding the mentioned plugin is intended for adding either css or js to the site’s header, body or footer, not PHP.

I would recommend creating a custom plugin for integrating your custom script on the site. This way you’re not depending on other plugins and you have complete control on how and when the script is added to the site.

You can learn more about creating a custom plugin from the WP Dev docs, Plugin Basics.

In your custom plugin you could then add the script conditionally based on the user and one’s role. Simple example below. Uncomment if statements, or add more, as needed.

add_action( 'wp_head', 'my_maybe_add_chatbot' );
function my_maybe_add_chatbot() {

  // only logged in users?
  // if ( ! is_user_logged_in() ) {
  //   return;
  // }

  // Only certain role
  // $user = wp_get_current_user();
  // $allowed_role="some_role";
  // if ( ! in_array( $allowed_role, (array) $user->roles ) ) {
  //   return;
  // }

  // other guards..

  my_print_chatbot_script();
}

function my_print_chatbot_script() {
  ?>
  <!-- script here -->
  <?php
}

You can of course leave out the PHP if statements, always print the script, and just print the roles in the middle of the script. But I don’t think this is a clean way to do things.

add_action( 'wp_head', 'my_print_chatbot_script_with_role' );
function my_print_chatbot_script_with_role() {
  $user = wp_get_current_user();
  ?>
  <script>
    // script...
    title: "TestBot",
    initPayload: <?php echo (array) $user->roles[0]; ?>,
    customData: { language: "en" },
    // script...
  </script>
  <?php
}

Third option is to put the script into a separate js file and enqueue it with PHP. Then inline the role data to be used in the main script file.

add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
function my_enqueue_scripts() {
  $user = wp_get_current_user();
  
  wp_enqueue_script(
    'my-script',
    plugin_dir_url( __FILE__ ) . '/js/script.js',
    array(),
    '1.0.0',
    true
  );

  // access in js console.log( MY_SCRIPT_USER_ROLES );
  wp_add_inline_script(
    'my-script', 
    'const MY_SCRIPT_USER_ROLES = ' . json_encode( (array) $user->roles ), 
    'before'
  );
}