0

I have a javascript there is looking like this:

jQuery("ul[data-attribute_name=attribute_pa_afhentning-og-levering] li").click(function(){
    var selectedlevering = jQuery("ul[data-attribute_name=attribute_pa_afhentning-og-levering]").find("li.selected").attr("data-value");
    if (selectedlevering == "afhentning"){
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=afhentning] span").click();
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=afhentning]").hide(); // hide the "afhentning" option in "Packages" if you like
    }
});

I need to call that script from my functions.php in Wordpress. But how am I doing that?

Best regards

4
  • You can't directly call client-side javascript from PHP. Commented May 30, 2019 at 9:40
  • Send an AJAX request to a file that calls the functions you need. api.jquery.com/jquery.ajax Commented May 30, 2019 at 9:40
  • I hope you don't mean you really want to call JavaScript functions from PHP... Commented May 30, 2019 at 9:41
  • Thank you for the answers. It was just a guy who said that the javascript would not run, unless I called the script in the functions.php. I was thinking of just putting the script in style.js in my child theme. But I can see that it is not called: dev.onlinerelation.dk/produkt/stack Commented May 30, 2019 at 9:46

1 Answer 1

1

You can choose to add the custom script in the head section by adding the wp_head as the required action.

//----------------------------------------------------------------------
// Custom script in head section, add these lines in your functions.php
//----------------------------------------------------------------------

function custom_script_name(){
?>
<script>
// Your script here
</script>
<?php
}
add_action('wp_head', 'custom_script_name');

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you a lot for your comment. I have added the code now, but the script is not working. So that I have to figure out why now :-)
You can check if script hs been added to your site via page source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.