WordPress.org

Ready to get started?Download WordPress

Forums

Display based on mobile user agent (3 posts)

  1. Bloke
    Member
    Posted 1 year ago #

    I have a shortcode that refers to this function. I am using a mobile theme and I need a way to display the shortcode based on if the user agent is mobile device. I have tried different configurations and narrowed it down to working it this way. I just don't know how to tell it that "mobile" is "true".

    function getProducts()
    {
    
    $disp = new Display();
    ?>
    <!--if desktop display this way -->
    	<div id='sideNav'>
    		<?php $disp->displayCategories(); ?>
    	</div>
    	<div id="productContents">
    		<div id="breadcrumbs">
    		<?php $disp->getDisplayBreadcrumbs(); ?>
    	</div>
    <div class="description">
        		<?php $disp->singleOrCat(); ?>
        </div>   					
    
    <div class="productImage">
    
    	<p><em>Click item to enlarge</em></p>
    	<div class="productan">
    						<?php echo $disp->getSide(); ?>
    	</div>
    	<hr/>
    
    </div> 
    
    </div>
    
    <!--if mobile display this way -->
    
    <div id='sideNav'>
    		<?php $disp->displayCategories(); ?>
    	</div>
    	<div id="productContents">
    
    <div class="description">
        		<?php $disp->singleOrCat(); ?>
        </div>   					
    
    </div> 
    
    </div> 
    
    </div>
  2. Bloke
    Member
    Posted 1 year ago #

    I found a solution and here it is incase anyone else needs it. It works with the mobile theme I am using. When the theme detects a mobile device, it switches to the mobile theme which calls this. Found it here http://www.dannyherran.com/2011/02/detect-mobile-browseruser-agent-with-php-ipad-iphone-blackberry-and-others/

    function detect_mobile()
    {
        if(preg_match('/(alcatel|amoi|android|avantgo|blackberry|benq|cell|cricket|docomo|elaine|htc|iemobile|iphone|ipad|ipaq|ipod|j2me|java|midp|mini|mmp|mobi|motorola|nec-|nokia|palm|panasonic|philips|phone|playbook|sagem|sharp|sie-|silk|smartphone|sony|symbian|t-mobile|telus|up\.browser|up\.link|vodafone|wap|webos|wireless|xda|xoom|zte)/i', $_SERVER['HTTP_USER_AGENT']))
            return true;
    
        else
            return false;
    }
    $mobile = detect_mobile();
    
    if($mobile === true)
        {...do something
  3. jarmerson
    Member
    Posted 11 months ago #

    Thank you for this Bloke! I've been using plugins and themes for mobile and not very satisfied. I simply need to carry over most of the design elements from existing theme then clean up css and call a custom menu for mobile.

    This allows me to do this!

    Thanks for sharing.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.