WordPress.org

Ready to get started?Download WordPress

Forums

Toolbar Theme Switcher
[resolved] Allow anonymous users to switch themes (16 posts)

  1. goto10
    Member
    Posted 1 year ago #

    Hi Rarst,

    Great plugin!

    I'm creating a site to showcase various themes, and I'd like to power it with your plugin.

    I have enabled the toolbar for all users with the following hook:
    add_filter( 'show_admin_bar', '__return_true' , 1000 );

    I have enabled all users to switch themes using the Toolbar Theme Switcher with:
    add_filter( 'tts_can_switch_themes', '__return_true' );

    This is all well and good, but when non-logged in users try and switch themes, wp-ajax.php is (correctly) returning 0.

    To make this work, I've added the following hook to toolbar-theme-switcher.php's init method, right under the action for wp_ajax_tts_set_theme:
    add_action( 'wp_ajax_nopriv_tts_set_theme', array( __CLASS__, 'set_theme' ) );

    I was wondering if you might add this to the plugin?

    Cheers!

    http://wordpress.org/extend/plugins/toolbar-theme-switcher/

  2. goto10
    Member
    Posted 1 year ago #

    Little correction: wp-ajax.php should be admin-ajax.php.

  3. Andrey "Rarst" Savchenko
    Member
    Plugin Author

    Posted 1 year ago #

    The initial use case for this plugin was for-developer and with solid security to be used on live sites.

    While it is flexible enough to be used as public switcher (as you discovered), it's been left in current "mild challenge" state so far.

    I am considering implementing public mode natively, but not without interface to go with it - so that non-developers are comfortable using it and have clear feedback. Which in turn means more code (and localization) to be written.

    So this is likely getting implemented as version 2 or something, when I have mood and chunk of time I feel like spending on such improvements.

  4. goto10
    Member
    Posted 1 year ago #

    OK, cool. Thanks for the reply. I'll continue to use my dirty little hack for now. :)

  5. Andrey "Rarst" Savchenko
    Member
    Plugin Author

    Posted 1 year ago #

    Also note that you don't need to modify plugin's code to achieve this! Just use class name explicitly instead of __CLASS__ and you can add it as any hook from your own code:

    add_action( 'wp_ajax_nopriv_tts_set_theme', array( 'Toolbar_Theme_Switcher', 'set_theme' ) );

  6. goto10
    Member
    Posted 1 year ago #

    Ahh, brilliant! That's great. I don't feel so dirty now. Thanks, again!!

  7. griff701
    Member
    Posted 1 year ago #

    Could you explain, in words of 1 syllable, as if to a very dim person, with extremely limited coding knowledge and maybe the brain of a senile person of advancing years, where the lines of code in the original post go, and what they replace?

    Thats exactly what I'm trying to achieve. I have a site, with two themes setup, and I'd like anyone thats logged in to be able to change themes.

    This seems almost achievable - but ever so slightly out of my grasp!

    Thank you for a great plugin, and for your time.

  8. goto10
    Member
    Posted 1 year ago #

    griff701,

    The code below should do the trick for you. Add it to a file named toolbar-theme-switcher.php and place it within your plugins directory. Then, activate the plugin Toolbar Theme Switcher Mods.

    Disclaimer: Use at your own risk.

    <?php
    /*
    Plugin Name: Toolbar Theme Switcher Mods
    Plugin URI:
    Description: Allow any (logged in) user to switch themes using the Toolbar Theme Switcher Plugin.
    Version: 0.1
    Author: goto10
    Author URI:
    License:
    */
    
    /**
     * Customize some functionality of the Toolbar Theme Switcher Plugin.
     */
    function tsh_init() {
      // Bail immediately if TTS is not installed.
      if ( ! class_exists( 'Toolbar_Theme_Switcher' ) )
        return;
    
      // Bail if user is not logged in.
      if ( ! is_user_logged_in() )
        return;
    
      // Give all users the ability to switch themes via the Toolbar Theme Switcher plugin.
      add_filter( 'tts_can_switch_themes', '__return_true' );
    
      // Allow anonymous users to switch the theme by hooking TTS's set_theme method to wp_ajax_nopriv_tts_set_theme.
      // Link: http://wordpress.org/support/topic/allow-anonymous-users-to-switch-themes
      add_action( 'wp_ajax_nopriv_tts_set_theme', array( 'Toolbar_Theme_Switcher', 'set_theme' ) );
    
      // Customize label for currently selected theme.
      add_filter( 'tts_root_title', 'tsh_tts_root_title' );
    }
    add_action( 'plugins_loaded', 'tsh_init' );
    
    /**
     * Callback function used to change the title of the currently selected theme.
     */
    function tsh_tts_root_title( $title ) {
      return str_replace( 'Theme:', 'Now Showing:', $title );
    }

    (edit: changed tabs to spaces)

  9. griff701
    Member
    Posted 1 year ago #

    That, is absolutely fantastic !

    It works perfectly. I tried it in the normal plugins directory and nothing happened, but then in the mu-plugins it works flawlessly.

    You've made my week, thank you very, very much for your time and help :D

    Again, thank you!

  10. goto10
    Member
    Posted 1 year ago #

    You're welcome, griff701! Main credit goes to Rarst for making this great plugin to begin with.

    A couple of things: A better name for this little mod plugin might be toolbar-theme-switcher-mods.php, since the original plugin is already named toolbar-theme-switcher.php.

    Did you activate the Toolbar Theme Switcher Mods plugin after copying it over to the plugins directory? I'm not sure why you had to use the mu-plugins directory. It works for me in the regular plugins folder.

  11. goto10
    Member
    Posted 1 year ago #

    You're welcome, griff701! Main credit goes to Rarst for making this great plugin to begin with.

    A couple of things: A better name for this little mod plugin might be toolbar-theme-switcher-mods.php, since the original plugin is already named toolbar-theme-switcher.php.

    Did you activate the Toolbar Theme Switcher Mods plugin after copying it over to the plugins directory? I'm not sure why you had to use the mu-plugins directory. It works for me in the regular plugins folder.

  12. griff701
    Member
    Posted 1 year ago #

    I've renamed it, moved it into the normal plugins directory and it works great.

    My confession - I was rushing before leaving for work when I dropped it into the normal plugin folder this morning ....... and I didn't activate it. <doh!>

    Thank you once again mate - can't tell you how chuffed I am.

    Steve.

  13. goto10
    Member
    Posted 1 year ago #

    griff701,

    I'm so happy it worked for you! I tried to respond earlier with a suggestion to rename the plugin file (since the name I suggested is what the original TSS is called), and also to double check activation, but wordpress.org has been acting up for me. Props again to Rarst for this great plugin!

  14. griff701
    Member
    Posted 1 year ago #

    Yes, Many thanks to Rarst as well !

  15. allergicvegetarian
    Member
    Posted 1 year ago #

    Thank you Rarst, as this is what I needed. I'm using your plug-in with your code you posted to accommodate people with special color processing needs. My website needs to be accessible to all people who need different theme colours, and adding every name by hand would quickly become overwhelming on a site you hope to reach thousands of people with.

  16. allergicvegetarian
    Member
    Posted 1 year ago #

    I thought that by deleting a line of code...

    // Bail if user is not logged in.
      if ( ! is_user_logged_in() )
        return;

    that users not logged in would be able to switch themes too, as I have disabled membership. I guess I'll have to allow membership. It does work when I allow membership, as I just tried it.

    Thanks! And, if you do ever fix it so random people to my site can just switch themes to one of the accessible themes, I'll gladly update! Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.