WordPress.org

Ready to get started?Download WordPress

Forums

Passing Javascript Cookie Values - Troubleshooting Needed (4 posts)

  1. siteducky
    Member
    Posted 1 year ago #

    I am having to convert a site to WordPress, that has a javascript cookie - all the javascript comes from a site built in Business Catalyst. To work with this I added a custom field shortcode plugin and am inserting my javascript using that.

    All the basic javascript calls seem to be working but that cookie - it appears to set, but it does not display on the following page. I am hoping someone expert in javascript and WP can help me out.

    I did review the issue with a javascript person and he said the cookie is setting, appears to be stored, console not throwing any errors - and we also tested for script call conflicts - so the only thing he could figure out was it might be the path and something to with how permalinks process the path.

    Here is the URL to the page where the cookie is set:
    http://sl-508-3.slc.westdc.net/~imacrea4/fort-myers/

    If you then click on one of the links in the list of communities you'll be taken to the page where the cookie is received.

    Here is a realtor site that is not on WP using the same script (with a slight variation only in variables) so you can see the content that should be called and the same script in place:
    http://www.ninalink.com/ft-myers-communities
    (Heritage Palms gives the best example)

    I know from my research this is not the perfect solution for cookies/wordpress. We are going to rebuild these properly, but this first one has to be out the door fast, and I didn't have my dev person to try to help me get it off the ground. I am willing to add in a function reference etc. but I am a designer, not a coder so would need hand holding! Therefore, I am hoping the answer is fairly simple and we can utilize this javascript cookie just to get launched.

    Thanks in advance for any pointers or suggestions!

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    Hi Ducky,

    Couple things, you can check if a cookie is set in your browsers so that there is no ambiguity. For example in Firefox latest it's Privacy > Remove Individual Cookies. You can even see the data being stored.

    Second, I'm not sure of your experience with WordPress so forgive me if I'm skipping over stuff or it's overly obvious, but including javascript in templating systems like WordPress is rarely as simple as just plugging it in between the <head> tags. Although that might work, it's an imperfect solution and sort of defeats the entire purpose of using something like WordPress.

    So, quick primer. WordPress has a serious of what are called action hooks and filters. You can add things on to each action call. For example, there is an action named wp_enqueue_scripts. Guess what it does? Exactly. You should hook your javascript includes onto this action call. As you get more familiar with WordPress, you can put conditionals in so that certain scripts are only included on pages where they are necessary. Example below:

    Go ahead and upload the javascript to your theme root (we'll call it my.js for the example). Then, in your functions.php add:

    add_action('wp_enqueue_scripts','enqueue_my_javascript');
    function enqueue_my_javascript() {
    wp_register_script('my.js','my.js');
    wp_enqueue_script('my.js');
    }

    Note that this is for front end javascript inclusion only. There is a separate hook for administrative js.

    Make sense? Happy to explain further.

    Some useful links:
    Getting started with WordPress (First article I read!)
    Actions/hooks list, explanation
    WordPress template heirarchy

  3. siteducky
    Member
    Posted 1 year ago #

    First, thanks for the response! And I am going to take you up on that "explaining further" :-)

    When you say upload your javascript to the root (my.js) - which javascript? There's where the cookie is set, there is where the cookie is supposed to display and then there is the existing .js file (which currently I am calling from just above the script because I haven't been able to insert it in header.php without breaking the theme).

  4. siteducky
    Member
    Posted 1 year ago #

    Also - my understanding from the javascript person who reviewed the issue (he was using firebug) was that the cookie was setting. I reviewed cookie information in Firefox, per your suggestion, and when I go to that page it doesn't show any cookies as being set. It also shows no errors unless I enable strict - then there are a bazillion, but none seem to relate to my particular js function.

Topic Closed

This topic has been closed to new replies.

About this Topic