0
$('.class1').append( '<span class="load-more"></span>' );
    var button = $('.class1.load-more');

the above is a code from the .js file of WP infinite scroll Wordpress plugin.

Can we somehow put a PHP condition inside a js file:

If( logical condition 1 )  {
    $('.class1').append( '<span class="load-more"></span>' );
    var button = $('.class1.load-more');
} elseif( logical condition 2 )  {
    $('.class2').append( '<span class="load-more"></span>' );
    var button = $('.class2 .load-more');
}

P.S.: PHP logic should go inside a js file not script<></script> tags.

7
  • What is the difference between client-side and server-side programming? Commented Feb 11, 2018 at 16:32
  • I think it's not duplicate here she is not dealing with code written in HTML file in <script><./script> please under her question correctly instead of labeling it as a duplicate. Her code is in .js file where she wants a PHP logic based some JS lines. Commented Feb 11, 2018 at 16:33
  • 1
    @somethingnow the problem is still the same. PHP is server side code. PHP can be used to compose any kind of file (html, css, js) server side, and then send it to the client. But it is a server side php file that holds the php condition and then builds a JS file out of it. Commented Feb 11, 2018 at 16:37
  • @t.niese suggest a solution to her. Commented Feb 11, 2018 at 16:40
  • Executing PHP code inside a .js file Commented Feb 11, 2018 at 16:41

1 Answer 1

0

define a other js file example condition.js

----- condition.js

<?php 
echo 'condition ..... ;
?>

---- condition.js

// condution.js output 
var lc = [ function(){return false;} , function(){ return x *y; }  ];

.

if( lc[0]() )  {
    $('.class1').append( '<span class="load-more"></span>' );
    var button = $('.class1.load-more');
} elseif( lc[1]() )  {
    $('.class2').append( '<span class="load-more"></span>' );
    var button = $('.class2 .load-more');
}

---- OR -----

// condution.js output
var lc = [ true , 0 ]; 

After

if( lc[0] )  {
    $('.class1').append( '<span class="load-more"></span>' );
    var button = $('.class1.load-more');
} elseif( lc[1] )  {
    $('.class2').append( '<span class="load-more"></span>' );
    var button = $('.class2 .load-more');
}
Sign up to request clarification or add additional context in comments.

5 Comments

Plese do not write solution in script<></script> tags.
examined again.
I need to take some time to implement this then I will write back to you.
theme customzier select logic will work like this : <?php if ( grid == get_theme_mod( 'show_authorbox_1' ) ) : ?> @Hasan you now knwo aht logic is there for PHP. (I gave an example)
You must tell condition to javascript. JavaScript run only <script> tags and *<script src=... > ** tags. You can create a .js file and include <script src=newfile.js> or you can use <?php echo <script> .... ?> ... other side javascript can not run condition or any data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.