How to set a php variable using a jQuery conditional test?


Staff member
I am trying to set a php variable using jQuery inside of a php file.

In my php file I want to run a jQuery script that tests if a condition is so and if so it sets a php var to one value or else it sets it to another value so I can use it later on in php.

I'm using this to try and set the size of my background image in wordpress based on window size. For now though, I'm just trying to echo the variable (
). Here is what I have:

<?php function bg_size() { ?>
        jQuery(document).ready(function($) {
            if ($(window).height()>720;  {
                <?php $bg_img_r = wp_get_attachment_image_src( $bg_img_id, 'fullbg' ); ?>
            if ($(window).height()<720; {
                <?php $bg_img_r = wp_get_attachment_image_src( $bg_img_id, 'smallbg' ); ?>
<?php } ?>

    echo $bg_img_r;

When I do this I get an error that
is an undefined variable.