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

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

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.

1 Like

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.

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

1 Like