Reputation: 117
Looking for help on inserting this code via functions.php or anywhere else in Wordpress. I tried this and it does not seem work... I cannot see it in the source code
<?php
function add_this_script_footer(){ ?>
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
(function() {
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://stats.domain.com/statsapp/";
_paq.push(["setTrackerUrl", u+"piwik.php"]);
_paq.push(["setSiteId", "445"]);
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
})();
</script> <?php }
add_action('wp_footer', 'add_this_script_footer'); ?>
Thank-you for your help.
`
Upvotes: 0
Views: 7905
Reputation: 150573
You can use wp_add_inline_script
if you want to add inline JavaScript code:
add_action( 'wp_enqueue_scripts', function() {
wp_register_script( 'foobar', '',);
wp_enqueue_script( 'foobar' );
wp_add_inline_script( 'foobar', "console.log('hello world');");
} );
If you want to make jQuery a dependency, then change the second line to:
wp_register_script( 'foobar', '', ['jquery'], '', true );
Upvotes: 1
Reputation: 591
The most flexible method is to use a custom function in your child themes functions file using the wp_enqueue_scripts
function and customize the parameters.
Dependant On jQuery
add_action( 'wp_enqueue_scripts', 'child_enqueue_scripts' );
function child_enqueue_scripts() {
if ( ! is_admin() ) {
wp_register_script( 'script-handle', get_bloginfo( 'stylesheet_directory' ) . '/your.js', array( 'jquery' ), '1.0.0', true );
wp_enqueue_script( 'script-handle' );
}
}
Once you register the script, you can use the script-handle in other code in your files without the need to register it again.
Set the 5th parameter $in_footer to true for execution in the footer if you want the script to be placed before the end tag. Default: false
This method enables you to use WordPress and theme specific hooks with conditional tags which is more flexible than adding the script in a template file.
Child Themes Versus Parent Themes
Change
get_bloginfo( 'stylesheet_directory' )
Or
get_stylesheet_directory_uri()
To
get_template_directory_uri()
When adding scripts in parent themes.
No Dependencies
Use this below example in your child themes functions file for scripts with no dependencies ( like Tracking scripts ) which don't rely on jQuery.
add_action( 'wp_enqueue_scripts', 'no_dependencies_enqueue_scripts' );
function no_dependencies_enqueue_scripts() {
if ( ! is_admin() ) {
wp_register_script( 'script-handle', get_stylesheet_directory_uri() . '/your.js', false, '1.0.0', true );
wp_enqueue_script( 'script-handle' );
}
}
Note: Both code examples assume your file containing your script is located in the root directory of your child theme. Change this path if you added the script files in a separate folder.
Upvotes: 2
Reputation: 1027
The "correct" way to do it would be to use wp_enqueue_script and put the code in its own js file.
So, take the tracking code and save it in a file like site-tracking.js.
Then in your functions.php file do something like:
add_action('wp_enqueue_scripts', 'my_custom_scripts');
function my_custom_scripts(){
/** here you can apply whatever logic you want to determine which pages
* your script gets included on, like wrap this in is_singular()
*/
wp_enqueue_script('tracking', get_template_directory_uri().'/js/site-tracking.js', array(), '', null, true);
}
This way your javascript gets properly enqueued, you can update it in a single, self-contained file and you and unregister as needed.
Upvotes: 2
Reputation: 202
You should add it to template file - either to the header.php or footer.php
Upvotes: 5