WordPress.org

Ready to get started?Download WordPress

Forums

PHP Browser Detection
Retina displays (4 posts)

  1. Paul
    Member
    Posted 1 year ago #

    Hi,
    great plugin!

    Is there a way to use this plugin to check for retina displays?
    I know about using media queries with CSS, but it would be nice to detect using php conditionals as well.

    http://wordpress.org/extend/plugins/php-browser-detection/

  2. Paul
    Member
    Posted 1 year ago #

    I figured that this wasn't possible so I made a simple function out of solution 5 suggested on this blogpost:

    http://www.bdoran.co.uk/2012/08/28/detecting-and-delivering-images-to-retina-displays/

    function get_pixel_ratio() {
    
            $pixelRatio = 1;
            if (isset($_COOKIE["pixel_ratio"])) {
                $pixelRatio = $_COOKIE["pixel_ratio"];
            } else {
                ?>
                <script language="javascript">
                    writeCookie();
                    function writeCookie()
                    {
                        var pixelRatio = 1;
                        if (window.devicePixelRatio) {
                            pixelRatio = window.devicePixelRatio;
                        }
                        document.cookie = "pixel_ratio=" + pixelRatio + ";" + document.cookie;
                        location = '<?= $_SERVER['PHP_SELF'] ?>';
                    }
                </script>
    
            <?php
        }
    
        return $pixelRatio;
    }
  3. Paul
    Member
    Posted 1 year ago #

    Just for future reference, the above function did not work that well because in some cases it reloaded/redirected back to the front page if the cookie wasn't set.

    I used a slightly modified technique presented on this blogpost:
    http://oxidedesign.com/detecting-a-retina-display/

    So here is the function I'm using now which seems to work fine:

    function get_pixel_ratio() {
    
          if ( isset( $_COOKIE['retina'] ) ) {
    
            $pixel_ratio = $_COOKIE['retina']; 
    
          } else { ?>
    
          <script type="text/javascript">
    
          var retina = 'retina='+ window.devicePixelRatio +';'+ retina;
    
          document.cookie = retina;
    
          if ( document.cookie.length !== 0 ) { document.location.reload(true); }
    
          </script>
    
          <?php } 
    
          return $pixel_ratio;
        }
  4. Mindshare Studios, Inc.
    Member
    Plugin Author

    Posted 1 year ago #

    Detecting retina displays is not something we are planning on including with PHP Browser Detection at this time.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.