0

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');
1
  • i thought '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 Commented Jul 12, 2017 at 9:18

2 Answers 2

1

Add JavaScript code:

<?php
    if ( is_front_page() ) {

        function jquery_cookie_enqueue_script() {
            wp_register_script('jquery-cookie', plugin_dir_url( __FILE__ ) . 'jquery.cookie.js', false, '1.0', true);
            wp_enqueue_script('jquery-cookie');
        }
        add_action('wp_enqueue_scripts', 'jquery_cookie_enqueue_script');


        function wpb_add_script() {
?>

    <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;
            }

            <?php
            $url=parse_url(get_bloginfo('url'));
            $url_new= rtrim($url['path'], '/').'/'
            ?>
            var url = <?php echo $url_new;?>
            jQuery.cookie('wwsgd_visits', count, { expires: 365, path: url });

            var repetition = <?php echo $wwsgd_settings['repetition'];?>
            if ( count <= repetition ) {
                jQuery(".wwsgd").show();
            }
        });
    </script>

<?php
        } // For function wpb_add_script()

        //For Header

        //add_action('wp_head', 'wpb_add_script');
        //For Footer
        add_action('wp_footer', 'wpb_add_script');
    }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this code shital. But when i include this code to my plugin file then jquery.cookies.js is not loading on the front page and even the script is not loading on front page.
please share your website link. and where is put jquery.cookies.js? in which folder?
Thanks shital for updated code but this code also not loading js files and scripts on the front page. I am using wordpress.org/plugins/what-would-seth-godin-do this plugin to show a different revolution slider for unique visitor. My website URL is spdemosite.com/landis on this url i am changing the slider based on cookie. For that purpose i am modifying the given plugin code to load the cookie only on the frontpage because in this plugin their is no option available for just load the cookie for the front page.
0

Try closing and opening the php tag:

function jquery_cookie_enqueue_script() {
    if ( is_front_page() ) {
        wp_enqueue_script('jquery-cookie', plugins_url( 'jquery.cookie.js',   __FILE__ ));
    ?>

        <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>

    <?php
    }
}
add_action('wp_enqueue_scripts', 'jquery_cookie_enqueue_script');

Comments

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.