? The Snippet:
if ( wp_is_mobile() ) {
/* Display and echo mobile-specific stuff here */
}
ℹ️ About the Snippet
With this small snippet, you’re able to check if the current user is from the mobile browser on your website as it’s a Boolean function so it’ll return True or False so that you can detect that user is coming from the mobile device or not. If the response is True, then the user is using a mobile/tablet device. On the other hand, if it returned False, then the user is not using a mobile/tablet device.
✅ How to use the Snippet
To use this simple snippet, you have to write the mentioned snippet anywhere in the theme where you need it. While using it, you can display the relevant stuff based on your condition.
For example if you want to show a paragraph for a mobile user on the Homepage of your WordPress website, what you’ll do is:
- Edit the Homepage file
It can be front-page.php, index.php or any page template you selected to show as Homepage.
- Paste the Code Snippet
In that page, go to the line where you want to write that paragraph and paste the below code:
if ( wp_is_mobile() ) {
<p>This paragraph is shown to mobile users only, desktop users won't see it </p>
}
To clarify, this will now show this paragraph to