0

I've got a WordPress install where I'm trying to change this content_width code:

if( ! isset( $content_width ) ) $content_width = 290;

in my functions.php file based on the users screen size. I've tried using CSS media queries, but for our particular use-case, I need to be able to change this in the functions.php file based on the users screen size.

Something perhaps like this?:

if (isset($_GET['width'])) {
$width = $_GET['width'];
if ($width <= 480) { //mobile devices
if( ! isset( $content_width ) ) $content_width = 290;
} elseif ($width <= 720){ //tablets
if( ! isset( $content_width ) ) $content_width = 720;
} else { //desktops
if( ! isset( $content_width ) ) $content_width = 1080;
}
echo $content_width;
}

Let me know if you have any suggestions. Thanks!

Jason
  • 23
  • 6
  • where does `$_GET['width']` come from? –  Feb 15 '16 at 20:45
  • Not sure, I just found it on a separate forum. Really I'm just trying to adjust this code: `if( ! isset( $content_width ) ) $content_width = 290;` based on the users browser size. – Jason Feb 15 '16 at 23:56
  • I'd like it to be 1080 on Desktop, 720 on tablets, and 290 on mobile. – Jason Feb 15 '16 at 23:57

1 Answers1

0

Like this route:

 <?php
  session_start();
 if(isset($_POST['width'])){
 $_SESSION['screen_size'] = array();
 $_SESSION['screen_size']['width'] = intval($_POST['width']);
 $_SESSION['screen_size']['height'] = intval($_POST['height']);
 }

 if(!isset($_SESSION['screen_size'])){
 ?>
 <html>
 <head>
 <script>
 function getSize(){
 document.getElementById('inp_width').value=screen.width;
 document.getElementById('inp_height').value=screen.height;
 document.getElementById('form_size').submit();
 }
 </script>
 </head>
 <body onload='getSize()'>
 <form method='post' id='form_size'>
 <input type='hidden' name='width' id='inp_width'/>
 <input type='hidden' name='height' id='inp_height'/>
 </form>
 </body>
 </html>


 <?php
 }else{
 var_dump($_SESSION['screen_size']);
 }

You can't use PHP alone to get the browser width, but I know you know that. This sample code could give you an idea of the right route to take. I got it from this link:

Get browser width using php

Community
  • 1
  • 1
Kyle Burkett
  • 1,375
  • 12
  • 28
  • Yes, I'm aware that php alone can't get the width. Could you help relate how I can use your code above to adjust this code: `if( ! isset( $content_width ) ) $content_width = 290;` in my wordpress functions.php file based on the users browser size? – Jason Feb 15 '16 at 23:58
  • add that code to your functions.php and then set your width like so: if( ! isset( $content_width ) ) $content_width = ($_SESSION['screen_size']); Vote my answer up if this helps – Kyle Burkett Feb 16 '16 at 20:47
  • $_GET['width'] does nothing unless you are using get method to pass the width value determined in another function. For Example, in the code above we use method POST. The code you probably borrowed the GET from was similar but used GET method instead of POST. Also, you borrowed a piece that doesnt benefit you without the other piece. – Kyle Burkett Feb 16 '16 at 20:52