Embed PHP code inside JavaScript? Good practice?

wordpress

#1

Hello,

I’m using PHP code inside my JavaScript code. Something like this:

function(){ 
    alert('<?php echo $var; ?>');
}

I’d like to know if this is a good practice and if it is valid as I’m going to submit my plugin to CodeCanyon with that code. Please guide.

Thank you.


#2

very bad practice…


#3

Is it acceptable if we use this coding style in our plugin? or will it be rejected?


#4

I’m pretty sure it will be rejected because they pay attention at code quality during review


#5

It’s an absolutely wrong idea to use server code inside frontend, do not do it!!!


#6

It’s not good practice, specially when there is a better solution.

For WP, you can use wp_localize_script. Look it up.

For non-WP there are other solutions, easiest being using AJAX.