Are inline javascript in function.php for themeforest themes allowed?

wordpress

#1

I am new in themeforest. i have a code in function.php

function test_function(){

<script>
jQuery('.test').on('click',function(){
 // do something
})
</script>

}

Can i do this ?? If not how to do it without being soft reject.
Thanks in advance


#2

use wp_add_inline_script
https://developer.wordpress.org/reference/functions/wp_add_inline_script/

BTW, you probably know this, but just to be clear: you should use inline JS only if it is really necessary, like some variables generated via PHP and stuff like that. All static code should be in .js file.


#3

Thanks for the answer. I tried that and wp_add_inline_script() doesn’t wok for me. So from your answer it is possible to use inline js. thanks for that.


#4

Yes it is possible, via wp_add_inline_script. If it doesn’t work for you, then you are very likely doing something wrong.