How to display content for mobile- or desktop only using WordPress “wp_is_mobile”.

Did you know that you can use WordPress built-in “mobile detect” function wp_is_mobile to create a simple shortcode that can hide certain parts of your content from mobile visitors and-/or desktop visitors.

The wp_is_mobile function returns true when your site is viewed from a mobile browser. Using this function you can create adaptive-/responsive wordpress themes based on visitor device. This snippet will allow you to use shotcodes the [desktoponly] desktop content [/desktoponly] or [mobileonly] mobile content [/mobileonly] to determine what content should be returned to the visitor.

Add this code to your functions.php

Related links:


3 Replies to “How to display content for mobile- or desktop only using WordPress “wp_is_mobile”.”

  1. not works!
    it shows:

    Your PHP code changes were rolled back due to an error on line 467 of file wp-content/themes/sydney/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘<', expecting end of file

    1. Hi Eds,
      sounds like you have a syntax error in your functions file. The snippet works, but be sure to remove the enclosure tags “< ?php" and "?>” from the code snippet above – before you paste it in to your functions.php file – since your functions.php file already have the enclosure tags in the beginning and in end of the functions.php file.

      Also, you can check (lint) your code to check for errors in some code editors – or use an online service, such as:

Leave a Reply

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