Coding help with custom function

I am trying to display the bot only on the admin dashboard of the subsites, I tried using the following code in a custom function and with a mu-plugin.

This is the code I tried to use:

<?php
add_action( 'admin_head', function() {
?>
<script>
<!-- INT-->
<?php
if(is_main_site()):
if(is_user_logged_in()):
$current_user = wp_get_current_user();
?>
<script>
window.intercomSettings = {
app_id: "ft0ojlry",
name: "<?php echo $current_user->display_name ?>",
email: "<?php echo $current_user->user_email; ?>",
created_at: "<?php echo strtotime($current_user->user_registered); ?>",
user_id: "<?php echo get_current_user_id(); ?>",
user_hash: "<?php echo hash_hmac('sha256', get_current_user_id(), 'dij133XLgfbNlIdQBhmTVDsfPbhiZNbX_2mf5Vpf'); ?>"
};
</script>
<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/ft0ojlry';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();</script>
</script>
<?php
} );

Thanks in advance!

  • Ash
    • WordPress Hacker

    Hello Aaron

    Do you mean you want to apply the script only on subsite admins, but not on main site admin and network admin?

    If so, you can try the following code:

    <?php
    add_action( 'admin_head', function() {
    	// To check if it is network admin, if it is, do nothing.
    	if( is_network_admin() ) return;
    
    	// To check if it is main site, if it is, do nothing
    	if( is_main_site() ) return;
    
    	// If the user is not logged in, that's not possible on admin, but still, do nothing
    	if( ! is_user_logged_in() ) return;
    
    	$current_user = wp_get_current_user();
    	?>
    	<script>
    		window.intercomSettings = {
    			app_id: "ft0ojlry",
    			name: "<?php echo $current_user->display_name ?>",
    			email: "<?php echo $current_user->user_email; ?>",
    			created_at: "<?php echo strtotime($current_user->user_registered); ?>",
    			user_id: "<?php echo get_current_user_id(); ?>",
    			user_hash: "<?php echo hash_hmac('sha256', get_current_user_id(), 'dij133XLgfbNlIdQBhmTVDsfPbhiZNbX_2mf5Vpf'); ?>"
    		};
    	</script>
    	<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/ft0ojlry';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();</script>
    	<?php
    } );

    Let us know if that works for you.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.