Change $content_width based on screen size (functions.php)

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.

Ideally 1080 for Desktop, 720 for tables, and 290 for mobile.

Any ideas?

Topic plugin-jetpack Wordpress

Category Web


The nature of $content_width is not to be responsive, there is just nothing you can do to it on wordpress side that will change that. For example it is the width that youtube videos will get when they are embedded and the canculation is done only once and will not be redone again unless you clear the relevant cache.

In general you should avoid any client properties detection on server side (what happens if the window size is changed, something that can happen even on surface and ipad), but if you have a good reason for that, you should filter the content before outputting it to adjust relevant sizes.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.