I want to include JavaScript code inside wp_enqueue_scripts in the plugin file and load it only for the front page.
Below is the nonworking code with some errors. I am not a PHP developer. How can I correct this code?
function jquery_cookie_enqueue_script() {
if ( is_front_page() ) {
wp_enqueue_script('jquery-cookie', plugins_url( 'jquery.cookie.js', __FILE__ ));
echo "
<script type='text/javascript'>
jQuery(document).ready(function() {
var count;
if ( !jQuery.cookie('wwsgd_visits') ) {
count = 1;
}
else {
count = parseInt(jQuery.cookie('wwsgd_visits'), 10) + 1;
}
jQuery.cookie('wwsgd_visits', count, { expires: 365, path: "<?php $url=parse_url(get_bloginfo('url')); echo rtrim($url['path'], '/').'/' ?>" });
if ( count <= <?php echo $wwsgd_settings['repetition'] ?> ) {
jQuery(".wwsgd").show();
}
});
</script>"
}
}
add_action('wp_enqueue_scripts', 'jquery_cookie_enqueue_script');
'wp_enqueue_scripts'was made to declare js files to add, not inline script, but i can be wrong.. I rather use'wp_footer'hook (just echo your script text like you did). Your code should work, but your script code may be placed a bit early in the page referring to standards. Look for Regolith's answer for correcting your syntax