I'm working from about 4 months on developing a Plugin. This plugin make in a page an entire app, using the powerful of Wordpress.

So, I've decided to create a serires of rewrite rules using command add_rewrite_rules with the hook "init", using add_action() function.

Then I've defined a series of query_vars to define some parameters inside the rewritten URIs.

All things works fine.

Now, I want to use the hooks "pre_option_stylesheet" and "pre_option_template", in this way: when I get a query_var("pagename") with a value (like "app"), so change theme to another one that is not the default theme.

So, if I go to www.example.com I can see website with TwentySixteen theme, but when I go to www.example.com/app, www.example.com/app/option, www.example.com/app/options and so on I want to see another theme.

So there's my code:

Class Core {

public $elements;
public $admin;
public $rules;
public $item;
public $template;
public $user;
public $ajax;

public function __construct()
    // Create Custom Post Types, Taxonomies, etc.
    $this->elements = new Elements;

    // Create Admin Menu and all interfaces on BackEnd
    // !!! This MUST BE RUN after items are created (Elements)
    $this->admin = new Admin;

    $this->user = new User($this);

    // this is the instance of the Class that define query_vars and rewrite_rules
    $this->rules = new Rules($this);

    $this->item = new Item($this);

    $this->ajax = new AJAX($this);

    add_filter( 'template_include', array( $this, "appTemplate") ); 

    // this not works, I cannot see query_vars in time
    // add_action("setup_theme", array($this, "elements") );

    add_filter("pre_option_template", array($this, "appTheme") );       

    add_filter("pre_option_stylesheet", array($this, "appTheme") );

    remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );

public function appTheme($current_theme)

    if ( $this->rules->getPagename() == "app" || $this->rules->getPagename() == "ajax" )  {
        // debugged-echo
        echo ":::::::::::::::::::::::::::::::" . $this->rules->getPagename();
        return "app";
        return $current_theme;

This is the instance with other class inside of it. The Class Rules is shown below:

Class Rules {

private $pagename;
private $instance;

public function __construct($instance)

    $this->instance = $instance;

    add_filter("query_vars", array( $this, "addQueryVars" ) );

    add_action("init", array( $this, "addTaskenRulesInit" ) );

    add_filter("parse_request", array( $this, "defineContentRules" ) );


public function getPagename()
    return $this->pagename;

public function addQueryVars($queryVars)

    array_push($queryVars, "tsk");
    array_push($queryVars, "tsk_id");           
    array_push($queryVars, "tsk_action");       
    array_push($queryVars, "tsk_section");
    array_push($queryVars, "tsk_demopage");

    return $queryVars;

public function defineContentRules()
    global $wp;

    // here I can see query_vars right defined based on rewrite_rules

        $this->pagename = $wp->query_vars["pagename"];
        $this->pagename = $wp->query_vars["name"];


        case "auth":

        case "signout":

        case 'ajax':

        case "app":         

In other words, when I show the app page (example.com/app), I see many times the debugged-echo of the Class Rules::appTheme, many many times, but the page show me the twentysixteen theme without its stylesheet.

if I call pre_option_template after the first Instance of the Class Core, I get the right theme, but I cannot see in that moment the query_vars...

  • Is there anybody who can help me? – Klode Aug 22 '16 at 12:55

Your Answer

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

Browse other questions tagged or ask your own question.