Support » Plugin: WP-PostRatings » Problem with cookies and multisite

  • Resolved jbrule


    The rated check does not work correctly when deployed to a directory based multisite install.

    FYI My multisite install is deployed to /charlie/ and the subsites live at /charlie/****/

    For example the site with path /charlie/test-josh/

    Is setting the cookie as so
    Set-Cookie rated_6138=1; expires=Sat, 15-Nov-2014 02:16:34 GMT; path=/charlie/

    The problem with this is that it will act as if the voter has already voted for the post with an id of 6138 on all subsites that are deployed under /charlie/ .

    It appears that as implemented the cookie + IP option does not negate with problem either as the cookie is checked first and then the IP is checked only if the cookie check returns false.

    To resolve the issue I suggest setting the cookie path to the path with is returned by site_url();

    The following is the problem code from 1.65. The COOKIEPATH below returns the root path of the multisite install.
    $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], time() + 30000000, COOKIEPATH);

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Lester Chan


    Thanks for the quick turnaround. Unfortunately the change is not working for my install. The SITECOOKIEPATH global is still just returning /charlie/

    I did have success doing the following.

    I added a filter call to the plugin to allow me to override and resolve the cookie path in my functions.php file

    The following change was made to line 622 in the wp-postratings.php file
    $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], time() + 30000000, apply_filters('wp_postratings_cookiepath',SITECOOKIEPATH));

    Then in my theme functions.php file I added

    function wp_postratings_set_cookie_path($the_cookie_path){
    return preg_replace( '|https?://[^/]+|i', '', get_option( 'siteurl' ) . '/' );

    Plugin Author Lester Chan


    @jbrule i checked the WP core code, it seems that SITECOOKIEPATH is used for most of the cookies set by WP itself

    Yes it is but it appears all of the those cookies have their paths set in relation to the SITE (in wp_sites) as opposed to the BLOG (in wp_blogs). The use of the cookie in the plugin is still broken in this case.

    See wpmu_current_site() in and you can see how the path is being set to the value from wp_site. ms_cookie_constants( ) uses that value for the SITECOOKIEPATH in a multisite install. The SITECOOKIEPATH is not the siteurl (which it needs to be for this plugin to work correctly).

    I see three options to make this work correctly

    1. Set the cookie path value using preg_replace( '|https?://[^/]+|i', '', get_option( 'siteurl' )
    2. Add the blog_id to the cookies key so you get rated_12_6138 instead of rated_6138 (less desirable as you are unnecessarily passing around cookies in a subdirectory install.)
    3. Implement a filter to allow overriding the path for those installs that do not work with the SITECOOKIEPATH. (probably wouldn’t hurt implementing this regardless).

    Plugin Author Lester Chan


Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Problem with cookies and multisite’ is closed to new replies.