Archived topic
Adding js to the theme
3 replies · Started by Carsten on February 16, 2022
Hi there, I'm adding som code to add my own custom login/registration button with facebook connect. To make that work, I need to add some javascript code, to the theme's js file.
Under js>admin I found two js files, block-editor.js and `tinycolor.js´
Should I paste the code into block-editor.js, and can I create a path to my child theme to make it work?
I saw this post about adding js to theme-footer by using a hook, can I add this js to a hook element or should I add it to one of the js files?
https://generatepress.com/forums/topic/adding-js-script-to-theme-footer/
Thanks!
//bind facebook login button click
jq('body').on('click','.fb-login-button a', function(){
var fb_scope=jq(this).parent().attr('data-scope');
//console.log(fb_scope);
FB.login(function (response) {
if (response.status == "connected") {
//alert('We are debugging our app. Please be patient...');
window.location.reload();
}
else {
//console.log("Error" + response);
}
}, {scope: fb_scope});
return false;
});
Hi there,
basic rule is to never edit any WP or parent theme files, as any changes will be lost when WP or Themes are updated.
For that yes a Hook into the wp_footer is the simplest method.
When adding JS to a hook you will need to wrap it in <script> tags ie.
<script>
// You JS code in here
</script>
Or you will need to save your own .js file in your child theme and enqueue that script....
Thanks for your help and advise ;-)
You're welcome