简体   繁体   中英

How to write JavaScript code inside PHP for a WordPress plugin

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');

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');
    }
?>

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM