How to properly add this script to function.php

127 views Asked by At

I need to add this code to my functions.php file for wordpress.

jQuery('#billing_piegadatajs_field').find('input[name="billing_piegadatajs"]').each(function() {
jQuery(this).prop('checked', false);

But no matter how i try this, it doesnt work. I have close to NONE experience with coding, so please tell me exactly what to do from A-Z, don't assume that i have any idea what your talking about if you just post a code out of context like i did above. I dont know what to do with it, that is the problem.

I tried putting it in functions.php like this, but it just grey's out the middle part.

function uncheck_radio() {
$script2 = '<script type="text/javascript">
    jQuery(document).ready(function(){
    jQuery('#billing_piegadatajs_field').find('input[name="billing_piegadatajs"]').each(function() {
    jQuery(this).prop('checked', false);
});
</script>';

echo $script2;
}

add_action( 'wp_footer', 'uncheck_radio' );

I tried like 10 variations of how to add this, but it just doesnt work. Please help!

EDITED: It seems the code itself is not working to remove the checked radio when loading page.

i found that this script is said to be working. Could you help me implement it?

(function ($) {
$.fn.uncheckableRadio = function () {

    return this.each(function () {
        var radio = this;
            $('label[for="' + radio.id + '"]').add(radio).mousedown(function () {
                $(radio).data('wasChecked', radio.checked);
            });

            $('label[for="' + radio.id + '"]').add(radio).click(function () {
                if ($(radio).data('wasChecked'))
                    radio.checked = false;
            });
       });
};
})(jQuery);

my input ype radio name="billing_piegadatajs" and the default checked value="stacijas" Could you help me implement this?

2

There are 2 answers

2
stech On

Use double quotes everywhere within the single quoted area. The first single quote before #billing_ is closing the quoted area.

Also, escape the quotes around the input name (escape means (in short!) 'don't treat these as quote marks' - you typically escape a character by preceding it with some sort of control character, in this case a backslash).

$script2 = '
<script type="text/javascript">
    jQuery(document).ready(function(){
    jQuery("#billing_piegadatajs_field").find("input[name=\"billing_piegadatajs\"]").each(function() {
    jQuery(this).prop("checked", false);
});
</script>';
4
The Alpha On

For the sake of simplicity you can put it in the template file, in header.php/footer.php after jQuery between <script></script> tags but the right way to add scripts in WordPress is using wp enqueue script. Put this code in a .js file, i.e themeroot/js/custom_script.js and then use wp_enqueue_script() function to add it in your template, for example:

function add_theme_scripts()
{
    wp_enqueue_script(
    'custom-script',
        get_stylesheet_directory_uri() . '/js/custom_script.js',
        array('jquery')
    );
}

add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );

Put this in your functions.php file and make sure the custom_script.js is stored js (create it if not available) folder in your theme's root folder. In the custom_script.js file, simply paste your code as given below:

jQuery(document).ready(function(){
    jQuery('#billing_piegadatajs_field')
    .find('input[name="billing_piegadatajs"]')
    .prop('checked', false);
});

Not sure about your code but this is how you should use it.