Archived topic
Different hooks for mobile and desktop
5 replies · Started by Sara on October 30, 2019
Is there a way to enable a hook just for mobile or just for desktop?
The reason I ask is that I'm using an external javascript for an opt-in lightbox. I want to use one script for desktop (which pops up on exit intent) and a different script for mobile (which pops up after a number of seconds).
If not - is there another to execute a javascript depending on what device the user is using?
Hi there,
You can try using the wp_is_mobile function to set the display conditions for the two different scripts like so:
<?php if ( wp_is_mobile() ) : ?>
/* Display and echo mobile specific stuff here */
<?php else : ?>
/* Display and echo desktop stuff here */
<?php endif; ?>
That is perfect! Thanks so much. Would the wp_is_mobile function also cover tablets? I just realized I want the first script to display for both mobile AND tablets.
That function should detect 'all' mobile browsers regardless of the screen size so Tablets would be included.
Excellent. Thanks again!
You're welcome