Call custom JS function from PHP

I have enqueued external JS file for some of my WP Theme functions. I can call these functions by its name on buttons without problem. Now, I need to call one function based on custom WP_Query condition. I have find that if I use tags this code is working

<header class=”page-header”>
$allsearch = new WP_Query(“s=$s&showposts=0″);
$asfp = $allsearch->found_posts;?>
<h1 class=”page-title”><?php echo $asfp ?><?php printf( esc_html__( ‘ posts for: %s’, ‘webovkar’ ),
‘<span>’ . get_search_query() . ‘</span>’ ); ?></h1>
<?php if($asfp < 1) :?>

(function searchAgain(){
var e = document.querySelector(‘#search-section’).classList;

<?php endif; ?>

</header><!– .page-header–>

My question is: Is there better (cleaner) way just to call function by its name searchAgain(); from external JS file as on buttons instead injecting function as it is now ?

Thanks for any suggestions

Read more here:: Call custom JS function from PHP

Leave a Reply

Your email address will not be published. Required fields are marked *