0

I have the below div where i use a PHP if else to change the background color of the div based on the $tteg value which is equal to a JS value called {{ reviewsOverall }} , i don't know if i did equal them correctly but it seems to work, the problem i am facing is the color of the background is always red, i am not sure if the below code is correct, please help.

What i want to do is IF {{ reviewsOverall }} value is below 7 div background-color to be red, else to be green, i want to mention that {{ reviewsOverall }} value starts from 0 till 10 and when its for example 5 its without decimals but if its 3.1 or others its with one decimal after the point. I am adding the below code in Wordpress Plugin editor, thanks in advance for your help, much appreciated.

<div
 
class="rwp-users-score <?php if ( $is_UR ) echo 'rwp-ur' ?>"
 
<?php $tteg = "{{ reviewsOverall }}"; ?>
 
 <?php if ($tteg < 7): ?>

style='background-color: red;'

<?php elseif ($tteg >= 7): ?>

style='background-color: green;'

<?php else: ?>

style='background-color: black;'

<?php endif ?> >

1 Answer 1

2

PHP can't read JavaScript variables - that's because they are simply not available at the point that PHP is run.

So, in this case, you are better staying with pure JavaScript to accomplish what is really a UI challenge - it will also make your code cleaner and better organised.

Here is a basic ( untested ) solution:

<script>
    $( function() {
       
       color = 'red'; // default ##
       if ( '7' >= reviewsOverall ){
        
           color = 'blue';
           
       } elseif ( '7' < reviewsOverall ){
        
           color = 'pink';
           
       }

       $('.rwp-users-score').css('background-color', color );

    });
</script>

This script could be added inline on the template or added to a scripts.js file and included using wp_enqueue_script() - https://developer.wordpress.org/reference/functions/wp_enqueue_script/

8
  • can i add this in the wordpress plugin editor php file? switch won't do it for me, i need if else statement for ranges from [0;7) and [7;10] Dec 30, 2020 at 22:56
  • What is the "wordpress plugin editor php file" ? you can make also switch statements work, they accept operators.
    – Q Studio
    Dec 30, 2020 at 23:08
  • What is the "wordpress plugin editor php file" ? - its where you edit your plugin files in Wordpress, you don't know? :) Dec 31, 2020 at 8:54
  • Right, but, no - that's really the wrong place to be looking - if the plugin is your own, edit it in a IDE and track your changes - if it is not your plugin - don't edit it --- WP should really remove that feature as it goes against everything WP is trying to achieve.
    – Q Studio
    Dec 31, 2020 at 10:11
  • 1
    Thanks Q Studio, your answer is my solution, thank you so much. Dec 31, 2020 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.