• Resolved thezman

    (@thezman)


    Problem:
    When admin is using ‘Appearance Customize Screen’ via the theme sub menu Appearance->customize there appears to be no way to detect if “is_customiser” is true/false via the theme layer.

    Consider the following:
    Lets say an Admin is customizing a site that has sub pages, they change a color within the customer section that only applies to a subpage. We have no way to preview that change on any other page other than the “homepage”.

    Possible solution:
    create a boolean such as “is_customiser” where as we could easily include the additional layouts on the home page in a “preview detected mode”

    some tests and attempts:
    I tried this in Functions.php

    function mytheme_customizer_live_preview(){
            global $_customiser;
            $_customiser = TRUE; //<- this works
            echo "bar"; //<- this works, but dumps prior to head
    }
    add_action( 'customize_preview_init', 'mytheme_customizer_live_preview' );

    and then in header.php

    global $_customiser;
    if( $_customiser){ echo "foo"; //<- this works}

    And thats good and fine if I want to push a bunch of ODB into a var and have a really really long homepage…
    However this becomes quite ineffective with lets say a 10 template and 5 custom post_type multi taxonomy site…

    Simply put: we need a way to accomplish any or all of the following

    • A: better way to detect if the customizer is active…
      and if so, then show things that are not on the homepage.
    • B: make the customizer work on subpages,
      where as a user is in Appearance->customize and clicks on a subpage the customizer needs to know that and load options there too…
    • C: possibly create some new hook, or, allow us to feed information back to the customiser from inside our iFrame?

    Please feel free to suggest alternatives, I have reviewed the documentation but maybe there is another way I do not know of?

    thank you for your consideration and examination of this matter.

Viewing 1 replies (of 1 total)
  • Thread Starter thezman

    (@thezman)

    after about a day of hacking here is the path I took:

    1. I created a global var for the postID called psudo_postid
    2. created a function to detect if “is_customiser”
    3. I added a drop menu into the theme options panel that lets the admin choose any page/post/custom post_type and “simulate” it
    4. I added that same drop menu into the actual WordPress Customize panel under “Appearance=>Customize”
    5. mod all theme files to work outside the loop

    All page-name.php and single-name.php now contain this checker

    global $psudo_postid;
    if(!isset($psudo_postid)) {$postid = get_the_ID();} else{$postid = $psudo_postid;}

    created a switch in index.php and page-homepage.php that works like so:

    global $psudo_postid;
    	$psudo_postid = $theme_opt['page_to_sim'];
    
    	if(!$psudo_postid) {
    		include THEME_DIR.'/parts/_homepage.php';
    	} else {
    		// simulate another page
    		$sim_what = get_post_type( $psudo_postid );
    		$page_template = get_page_template_slug( $psudo_postid );
    
    		switch ($sim_what) {
    			case 'custom_post_type_one':
    			case 'custom_post_type_two':
    			case 'custom_post_type_three':
    				include THEME_DIR.'/single-'.$sim_what.'.php';
    			break;
    
    			case 'page':
    				$page_template = get_page_template_slug( $psudo_postid );
    				if(!$page_template) { // then its a default
    					include THEME_DIR.'/page.php';
    				} else{
    					include THEME_DIR.'/'.$page_template;	}
    			break;
    
    			case 'post':
    				include THEME_DIR.'/single.php';
    			break;
    
    		}
    	}

    Added this to functions.php

    function is_customizer_preview(){
    	global $is_customizer;
        $is_customizer = true;
    }
    add_action( 'customize_preview_init', 'is_customizer_preview' );

    As a result, the WordPress ‘Appearance Customize Screen’ reloads when the admin changes the “page_to_sim”.
    since everything is routed through index.php or page-homepage.php the WordPress Customize Screen reloads and shows that page

    End Result: WordPress Customize Screen now allows Admins to change settings and see them (color, font sizes, and anything else) on the sub pages, custom post types, archives, and the like.

    Unorthodox, yet effective.

    I thought I would note here a failed attempt which involved trying to add all custom post types into the pre_get_posts drop menu that an admin would use to set the homepage under “Settings->Reading->Front page displays:”.

    The idea is sound and works… however, it forces the admin to annoyingly “change back” the ‘Front page displays’ option to what it should be after testing. The way above allows a “sim” for the admin to preview ANY page without being forced to change their homepage just to preview an option on a subpage.

    function add_custom_post_dropdown( $pages ){
        $args = array( 'post_type' => 'custom_post_type_one');
        $items = get_posts($args);
        $pages = array_merge($pages, $items);
    
        $args = array( 'post_type' => 'custom_post_type_two');
        $items = get_posts($args);
        $pages = array_merge($pages, $items);
    
        $args = array( 'post_type' => 'custom_post_type_three');
        $items = get_posts($args);
        $pages = array_merge($pages, $items);
    
        return $pages;
    }
    add_filter( 'get_pages', 'add_custom_post_dropdown' );
    
    function enable_custom_post_home( $query ){
        if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
            $query->query_vars['post_type'] = array( 'page', 'custom_post_type_one', 'custom_post_type_two', 'custom_post_type_three' );
    }
    add_action( 'pre_get_posts', 'enable_custom_post_home' );

    I hope this post helps someone on their search:

    Keywords I searched in google looking for a solution:
    WordPress Appearance Customize theme subpages
    WordPress admin Customize subpages preview
    WordPress Customize screen only allows homepage
    WordPress Customize theme preview not working for sub pages
    WordPress admin Customize screen custom post_type
    WordPress admin Customize screen custom taxonomy
    WordPress Appearance=>Customize not showing sub pages
    WordPress Appearance=>Customize does not preview secondary clicks
    WordPress Appearance=>Customize does not preview tertiary clicks

Viewing 1 replies (of 1 total)
  • The topic ‘Need GLOBAL var to detect "If Appearance Customize Screen" is ON’ is closed to new replies.