Check if Device is Mobile in WordPress

Check if Device is Mobile in WordPress

? 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:

  1. Edit the Homepage file

    It can be front-page.php, index.php or any page template you selected to show as Homepage.

  2. 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 the Mobile users only. Moreover, it’s not limited to the paragraph, you can show specific buttons, notifications, posts etc to the mobile users with this snippet.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get regular WordPress updates directly in your inbox.

shares