Discover in this tutorial how to configure a WordPress theme so that the images lightbox option is only accessible to administrators, via code.
This method allows you to effectively manage who can activate or view the lightbox effect (Expand on click) on images, ensuring that only administrators have this ability.
The theme.json file allows for such a degree of customization of a WordPress theme that it’s worth getting familiar with. That’s why I’ve published a series of tutorials focused on understanding how it works and, more importantly, how you can customize it to your liking.
Starting with WordPress 6.4, the lightbox feature was added, which allows you to click on an image to expand it. In general it is an interesting option, but you may prefer to disable it for non-admin users so that this option does not appear for authors, for example.
So in this tutorial you will see how to restrict this option:
Instructions to hide the lightbox from non-admin users
1. Modify the theme.json file with a filter
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this code you use the wp_theme_json_data_theme
filter in combination with a conditional tag, first to check if the current user has admin permissions. And secondly, if they don’t, the code modifies the theme.json to disable the lightbox interface for images, ensuring that only administrators can enable this setting.
2. Interface in the block editor
With the above code applied, non-admin users will not see the option to enable the lightbox in the block editor (1st image), while administrators will still have full access to this functionality (2nd image).
Conclusions
Now you know how to hide the lightbox option on images in a WordPress theme if the user does not have admin permissions.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉