Home > Back-end >  Hide a div to user role in one page
Hide a div to user role in one page

Time:01-05

I would like to hide a particular div from a specific user role on a page of my site, I tried to insert the following code in the function.php file of my theme, but unfortunately it doesn't work, do you have any suggestions?

<?php
$user = wp_get_current_user();
if ( in_array( 'shop_manager', $user->roles ) ) {
?>
    <script>
jQuery(document).ready(function( $ ){
    jQuery( "#hide" ).empty();
    
});     

    </script>
<?php 
}
?>

CodePudding user response:

functions.php is for adding php functions to your site. If you wanted to print out HTML/JS in your theme, you would need to add this to your theme file (index.php or footer.php)

Place this code in your theme file and it should work.

CodePudding user response:

Use the footer hook to inject your css/js code:

function your_function() {
    $user = wp_get_current_user();
    if ( in_array( 'shop_manager', $user->roles ) ) {
        ?>
        <script>
        jQuery(document).ready(function( $ ){
            jQuery( "#hide" ).empty();
        });
        </script>
        <?php 
    }
}
add_action( 'wp_footer', 'your_function' );
  • Related