nash-ye / wp-conditional-themes

A simple API to switch the themes on certain conditions.

Home Page:https://wordpress.org/plugins/wp-conditional-themes/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I switch to a theme based on role and page URI pattern?

beatelite opened this issue · comments

For example,

Show theme X if the viewer has a user role of "administrator", Contributor" and "editor" and the page URI pattern is page-name or page-name-2 or page-name-3"

Otherwise, show original theme.

Also, are there wildcards allowed for URI? so instead of saying page-name-1 and page-name-2 we could just say:

Show x theme for page-name/*

This would help in keeping us from having to type out all the page names to switch the theme on based on a certain pattern.

Let me know - great idea for a plugin.

hope I can get some suggestions soon. I'd really like to figure this out.

Thanks for any suggestions.

I am trying to figure this out. I'm attempting to use the is_page() tag to see if I can switch based on page id or name:

function my_conditional_themes_setup() {

    Conditional_Themes_Manager::register( 'mythemename', function() {

        return is_page( 111 );

    } );

}
add_action( 'plugins_loaded', 'my_conditional_themes_setup', 100 );

However, this doesn't work. I even tried the page name but that doesn't work either.

Does it work with is_page() ?

If it did work, I assume I could easily use:

function my_conditional_themes_setup() {

    //Switch to admin theme if the user has administrator role.
    Conditional_Themes_Manager::register( 'mythemename', function() {

       return current_user_can( 'administrator' ) && is_page( 'my-page-test' );
     } );
 }
add_action( 'plugins_loaded', 'my_conditional_themes_setup', 100 );

But it looks like switching on page isn't available?

Ok, I figured it out. Thought I'd post for any one else that is wondering how to do this. I created a plugin and used the below. Be sure to change the page names and user roles to whatever you wish.

//function to get the page name via request uri. Then count the '/' in the url. If greater than 2, parse the url by '/' and turn it into an array then assign the first URI value (our target) to the $url variable. otherwise, just remove the '/' from the url.
function getCurrentPageName() {
      //set the variables below to null for good measure
      $url = null;
      $uri = null;

        // gets full url: for example "http://www.domain.com/uri1/uri2"
        $url = $_SERVER['REQUEST_URI']; 


        //count the '/' in the url. If greater than 2, parse the url by '/' and turn it into an array so we can target what we want to.
        if ( substr_count($url, '/') > 2 ) {
          $uri = explode("/", $url);
          //assign the first URI value (our target) to the $url variable.
            $url = $uri[1]; // for example "/pages-all/edit/" -> split "/"'s into an array -> choose array index 1 which has our target value
        } else {
            //otherwise, just remove the '/' from the url
          $url = str_replace("/", "", $url); // "/pages-all/ -> "pages-all"
        }

        //return the value we want
        return $url;


}


function my_conditional_themes_setup() {
    //set the variables below to null for good measure
    $can = null;
    $currentPage = null;


    //use conditional theme switcher API to manage the target theme switching
    Conditional_Themes_Manager::register( 'MyTargetTheme', function() {

        $can = false;

        //part 1. Let's determine who the user is and if they have the roles we specify as conditions for theme switching
        if ( current_user_can( 'administrator' )  ) {
          $role = 'administrator';
        } else if ( current_user_can( 'author' )  ) {
          $role = 'author';
        } else {
          // user not logged in - do something here
        }

        // part 2. Let's determine what the name of the page being visited is from the getCurrentPageName function and if matches the page names we specify as conditions, then let's create a new variable to use below.
        if ( getCurrentPageName() === 'pages-name1' ) {
          $currentPage = 'page-name1';
        } else if ( getCurrentPageName() === 'page-name2' ) {
            $currentPage = 'page-name2';
        } else {
          // handle spelling mistakes in uri etc.
          // redirect home or to 404 theme etc.
            // wp_redirect( '/404' ); exit;

        }

        //let's make sure the $currentPage variable was updated from the above before run the test on whether it meets all our conditions
        if ( isset($currentPage) ) { 
          if ($currentPage === 'page-name1' && $role === 'administrator') {
            $can = true;
          } else if ( $currentPage === 'page-name1' && $role == 'author' ) {
            $can = true;
          } else if ( $currentPage === 'page-name2' && $role == 'administrator' ) {
            $can = true;
            //etc
          }
        } else {
          //do something here
        }

        // final step: Let's return a boolean so the theme switching api can make a decision.
        if ($can === true) {
          return true;
        } else {
          return false;
        }

    } );


}
add_action( 'plugins_loaded', 'my_conditional_themes_setup', 999 );