Include jQuery into your theme through the functions.php

Posted on 21st August, 2013 2 Comments

Over time I’ve realized that there is a better way to include jQuery. Rather than adding your jQuery straight into the header.php or footer.php of your theme files which can be untidy, I would recommend adding a function to your function.php which is a lot neater.

So open the functions.php

scroll to the bottom and create a simple function:

	function scroll_to_top(){

};

Then create a variable to hold the script

	$script ='	';

Now add your jQuery between the single quotation marks, like this.
*Notice that if you’re using more single quotation marks you’ll need to escape them using a backwards slash or it’ll break.

$script ='<script type="text/javascript">
jQuery(document).ready(function($){
		$('a[href^="#"]').on("click",function(e) { // escape all the other single quotes
			e.preventDefault();

			var target = this.hash,
			$target = $(target);

			$("html, body").stop().animate({
				"scrollTop": $target.offset().top
			}, 900, "swing", function() {
				window.location.hash = target;
			});
		});
	});
 </script>';

finally echo the $script, so your final function will look like this.

function scroll_to_top(){

	$script ='<script type="text/javascript">
jQuery(document).ready(function($){
		$('a[href^="#"]').on("click",function(e) {
			e.preventDefault();

			var target = this.hash,
			$target = $(target);

			$("html, body").stop().animate({
				"scrollTop": $target.offset().top
			}, 900, "swing", function() {
				window.location.hash = target;
			});
		});
	});
</script>';
         echo $script;
};

Then in your footer.php add the function.

<?php scroll_to_top() ?>

Comments

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]

EROAttaike

22nd, Sep, 20

The salon exotic invites visit one of the ways massage techniques, is what we do. What is an Lingam massage or penis massage interested in everyone. massage woman – woman it’s a craftsmanship of giving for pleasure. You be surprised to that,what sea pleasure can get to know from choice massage. In massage studio acupressure massage women will hold the most sensual massage with stones.

How is it done, and is there something exotic? We will tell you all about him that you wanted to know |Our Sakura massage is visited not only by men but also by women, and also by couples. You necessarily want to enjoy is exactly what infinitely … Our main intention this is to please visitors fabulous good nude massage. Individual approach to your needs and requirements.

The elegant masseuses our the spa will give you an unforgettable experience. The spa center is a place of rest and relaxation. This swedish massage, as though, and relaxation, influences on defined parts human body, this give a chance customer sit back and relax. Stop your choice not on one masseuse, choose two girls! Choose for yourself master which likes, both professional and professional proficiency!

Our salon in Empire City we advise luxurious premises with convenient style. All of these rooms can help to stay with you not attracting the attention of other customers.

Our showroom works in New York. Girls Ella –
[url=https://japanese.manhattan-massage.com]japanese massage parlor[/url]

WilliamEdugs

22nd, Sep, 20

Мы ппредоставим любую запасная часть в короткий срок прямо вам автозапчасти интернет поставщик : [url=https://autocomponent63.ru/]сайт продажи автозапчастей[/url].
Помимо желания предоставить колосольный ассортиментный набор автомобильных аксессуаров, мы всегда дополнительно работа ведется над уровнем обслуживания . Из-за выбора параметров и типов авто каждой марки, сотрудники нашей фирмы придумали поэтапную схему заявки. Данным методом, мы хотим исключить покупок и упущений неуместных автозапчастей.
Закажите у нас автозапчасти и дополнительно получите скидку в цене на дальнейший свой заказ!

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]