0

I have a site that has a membership. Currently, we allow customization of preset themes. This uses cookies, however I want my users to have the same theme across all of their devices. I want to be able to save the cookies to a users account. There has to be somehow to do this. The plugin I use for theme switching is Theme Switcher by Ryan Boren.

  • 1
    What is your question exactly? How to set cookies in the context of WordPress? What have you tried thus far? Have you tried contacting the plugin author using their support methods? – Howdy_McGee Feb 15 '17 at 21:44
  • @Howdy_McGee | I want to be able to save the cookie that this plugin creates into a users account. This would allow the user to have the same theme across all devices. – user113118 Feb 16 '17 at 21:49
  • If you save data on the account, it's not called cookie anymore. A cookie is saved on the device itself. – Jack Johansson Feb 17 '17 at 14:24
  • @JackJohansson | I am aware of that, however what I want to do is to allow the user to have the same theme across all their devices. The cookie would be saved to the account, then it will create a cookie with the user meta. – user113118 Feb 20 '17 at 1:20
0

You can can read the cookie and check if the current user is logged in using get_current_user_id(). If they are just save the cookie value to the user_meta.

$cookie_name = '';

if( isset( $_COOKIE[$cookie_name] ) ) {
    $user_id = get_current_user_id();

    if( $user_id ) {
        $meta_key = $cookie_name;
        $meta_value = $_COOKIE[$cookie_name];
        update_user_meta( $user_id, $meta_key, $meta_value );
    }
}
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Feb 17 '17 at 14:14
0

Adding upon @stillatmylinux's answer,

You can can read the cookie and check if the current user is logged in using get_current_user_id(). If they are just save the cookie value to the user_meta.

This will get the cookie for wptheme, get the user ID and save it to the user meta:

<?php $cookie_name = 'wptheme';

if( isset( $_COOKIE[$cookie_name] ) ) {
    $user_id = get_current_user_id();

    if( $user_id ) {
        $meta_key = $cookie_name;
        $meta_value = $_COOKIE[$cookie_name];
        update_user_meta( $user_id, $meta_key, $meta_value );
    }
} ?>

This will get the user meta for the one user and echo it into a javascript cookie code:

<script>document.cookie = "wptheme=<?php $user_id = get_current_user_id(); $key = 'wptheme'; $single = true; $themecookie = get_user_meta( $user_id, $key, $single ); echo $themecookie; ?>";</script>

For any other cookie, just change the wptheme to whatever the cookie name is. This code does have it's problems though. For example, after you login, the default theme will present itself, then you have to refresh. Also, be careful with multiple sessions and themes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy