Want to run jQuery code in wordpress plugin on every wordpress site page

I am trying to make a wordpress plugin that will run a jQuery script when I activate plugin and my jquery code is below:

jQuery(document).ready(function($){

    jQuery(window).scroll(function() {

    if(jQuery(this).scrollTop() != 0) {
        jQuery("#toTop").fadeIn("slow");    
    } else {
        jQuery("#toTop").fadeOut("slow");
    }
});

jQuery("#toTop").click(function() {
    jQuery("body,html").animate({scrollTop:0},1000);
});


});

Please tell me wordpress plugin code which will run this query on every wordpress site page when I activate plugin.

Related posts

Leave a Reply

1 comment

  1. to insert js into the footer of every page, try;

    <?php
    function myJScript(){
    // echo js here, or output it like below..
    ?>
    <script>
    jQuery(document).ready(function($){
    
        jQuery(window).scroll(function() {
    
        if(jQuery(this).scrollTop() != 0) {
            jQuery("#toTop").fadeIn("slow");    
        } else {
            jQuery("#toTop").fadeOut("slow");
        }
      });
    
      jQuery("#toTop").click(function() {
        jQuery("body,html").animate({scrollTop:0},1000);
      });
    });
    </script>
    <?php
    } // end function
    
    // call function myJScript in the footer of every page
    add_action('wp_footer', 'myJScript');