Blog

  • Thickbox.js: a modal API hidden in WordPress core

    Yesterday, I needed to create a modal, but I didn’t want to create a modal.

    “Surely, this is already in WordPress.”Corey Salzano

    So, I went looking for a modal in WordPress core, and I was delighted when I found one. Thickbox is JavaScript library that was created more than one decade ago and began shipping with WordPress in 2008.

    Why Thickbox in WordPress is great

    1. It’s a modal API built into core
    2. It’s a simple white box with an optional title, close button, and no offensive styling
    3. You don’t have to build it

    Perhaps Also Not Great

    I quickly noticed one shortcoming of the implementation. There is no way to specify the height and width of the modal using percentages. The values are required to be supplied in pixels, unless you use this JavaScript that enables the use of percentages:

    Thickbox’s fate as a part of WordPress core is questionable. Here’s a six year discussion of whether it should still be included, and WordPress core also bundles jQuery UI Dialog via the ‘jquery-ui-dialog’ script handle.

    Even if WordPress core drops this library in a future version, a plugin could be written to maintain compatibility. Today, I prefer thickbox because its implementation is so simple.

  • How to find WordPress theme’s stylesheet handle

    A few moments ago, I wanted to append an inline style to the theme’s stylesheet from within a plugin. This requires knowing the current theme’s stylesheet handle, so here is a function that finds that string.

    private function find_theme_stylesheet_handle() {
    	global $wp_styles;
    
    	foreach( $wp_styles->registered as $handle => $style_obj ) {
    		if( $style_obj->src === get_template_directory_uri() . '/style.css' ) {
    			return $handle;
    		}
    	}
    	return null;
    }