Move JS from customizer to file

Hello again guys

Before we start I’d like to explain thay my JS knowledge is nonexistent.

But to the subject. I have this code in my Customizer > Custom JS:

jQuery(function($){

$(".widget.widget_my_keywords_widget ul").text(function () {
return $(this).text().replace(“no terms found for this post”, “Ten artykuł nie zawiera przypisów.”);
});

$(".csl-entry.hanging-indent").text(function () {
return $(this).text().replace(“Retrieved from”, “Pozyskane z”);
});

});

jQuery(‘document’).ready(function($){
$(".x-sidebar .widget .h-widget").css(“background-color”, “”);
});

First two replace certain strings hardcoded into plugins that I use and third is (temporarily) a placeholder for changing inline !important styles that some plugin makers decided to use (nonsense I know).

Anyway, everything works fine as it is thanks to some kind people who gave me snippets to do those things.

BUT

I’d like to keep this code in my child theme instead of a customizer. I created scripts.js file in my child theme folder (next to functions.php and style.css so all custom stuff is in the same place) and copy-pasted that exact code from my customizer there. Now how do I make it work (preferably exactly the same way it worked in the customizer)?

P.S. How can I use code-block on new forums? I can’t find option for that and using blockquote instead is ugly…

Hi,

You can use wordpress wp_footer hook to add your js code.

eg. Add the code below in your child theme’s functions.php file.


function my_js_code() {
?>
jQuery(function($){

$(".widget.widget_my_keywords_widget ul").text(function () {
return $(this).text().replace("no terms found for this post", "Ten artykuł nie zawiera przypisów.");
});

$(".csl-entry.hanging-indent").text(function () {
return $(this).text().replace("Retrieved from", "Pozyskane z");
});

});

jQuery('document').ready(function($){
$(".x-sidebar .widget .h-widget").css("background-color", "");
});
<?php
}

add_action( 'wp_footer', 'my_js_code', 10 );

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.