Category: General

any thing that doesn’t fit elsewhere

Use ‘is_customize_preview’ and not ‘is_admin’ with Theme Customization API

Recently I came across a bug, and then another bug, when trying to solve a problem in the Theme Customization API. The site I was working on is using jQuery UI tabs, and when viewing the preview in the customzer, the script ended up looping and reloading the tabs over and over. This is a known bug (ticket 23225), so I decided I would add a condition to not load the jQuery UI script while in the customizer, using is_admin(). This did not work, so I decided to investigate further.

Turns out, is_admin() returns ‘false’ when in the customizer. Specifically, it returns ‘true’ until the page preview loads, at which point it returns ‘false’.

Happily, there is a new WP function that determines specifically when you are in the customizer. It is called ‘is_customize_preview()‘, and also returns a boolean. So a simple true == is_customize_preview() returns true in the customizer, and false everywhere else, including in other parts of the dashboard.

Here is the test and the results for those who are interested:

function debug_to_console( $data ) {
if ( is_array( $data ) || is_object( $data ) ) {
echo( "" );
} else {
echo( "" );
}
}

function test_is_admin() {
if( is_admin() ) {
debug_to_console( 'true is admin');
} else {
debug_to_console( 'false is admin');
}
}

add_action('wp_loaded', 'test_is_admin');

function test_is_customizer() {
if( true == is_customize_preview() ) {
debug_to_console( 'true is customize');
} elseif (false == is_customize_preview() ) {
debug_to_console( 'false is customize');
}
}

add_action('wp_loaded', 'test_is_customizer');

// front-end page: is_admin == false, is_customize_preview == false;
// admin page: is_admin == true, is_customize_preview == false;
// customizer: is_admin == false, is_customize_preview == true

Note: I was going to report this as a bug, but turns out it had been reported and is not considered a bug. Fair enough, just need to get the word out so developers don’t waste time on this small variance.

WordCamp T.O. 2014

I attended my first WordCamp on the weekend and had a really great time. Though the talks are fairly short and somewhat introductory (at least in the Advanced Developer track which I was following) there is a lot of information given, and it is a good overview of where WordPress is headed. I now have a pretty good list of topics I need to delve into, and am already figuring out where I can apply some of the new knowledge in a practical way.

Some of the topics on my list:

Backbonejs – Since WP has now included Backbone in the core (for the admin area for now) I think I should get familiar with it. The demonstration was great, and it really made things look slick in the dashboard. Since it is bundled all that is required is to enqueue it in the usual way and have it there ready to do my bidding.

Angularjs – I have been fiddling with this a little anyway, but again, though not part of the core, a very powerful javascript framework that offers some great features useful to WP. The syntax seems simpler than Backbone, but then I haven’t really explored enough to know what I’m talking about just yet.

WP-CLI – I have looked at this before and passed it over, even though I love command line stuff. But so many of the developers at WordCamp swore by WP-CLI that I realise I better give it another look. Anything that improves workflow, speeds up development and helps eliminate the mundane is tops in my book.

Unit Testing – I have a bit of experience with unit testing in straight php environments, but never in WP. I didn’t even know that WP has unit tests built in, for both php and javascript. Very cool. Need to get this built into the flow as well.

Vagrant – I’ve played around with Vagrant after being introduced to it in my php meetup, but have not made it a regular part of my workflow. Generally I am in control of the server environments that I work with, so I haven’t seen it as being all that useful. However lately there have been a lot of problems related to FastCGI, Nginx, Plesk, etc. that I am reconsidering. I would like to at least experiment, and see if it does indeed improve the dev environment.

SASS – I have used SASS before and loved it, but have not touched it in awhile. It’s used a lot now, and significantly improves performence. Plus it’s pretty fun from what I remember, and seems to integrate well with other tools. Definately on the ‘brush up’ list.

All in all it was a great experience, well worth it. I would highly recommend attending a WordCamp when one comes to your town. And of course it is all recorded, so you can ‘attend’ anytime by going to WordPress TV.

ken in copper canyon

Remotely yours; testing a lifestyle

I am currently engaged in a lifestyle experiment of sorts. Something I have always had an interest in doing, but never found the ‘right’ time.

Well, this isn’t exactly the right time either, but circumstances made it so and therefore the experiment began. I am riding my motorcycle through Central America, and plan to stay down here awhile and do some projects for some clients. Sounds simple, really. And simplicity is what made me think it was possible in the first place.

The reality has been that moving around and working just don’t go together. At least not at the pace we’ve been moving. Continue reading