The Internet Archive discovers and captures web pages through many different web crawls.
At any given time several distinct crawls are running, some for months, and some every day or longer.
View the web archive through the Wayback Machine.
The customizer is a framework for live-previewing any change to a WordPress site, and it is particularly useful for previewing visual changes. It has always shipped with a color control and the ability to preview custom colors with little effort, but the previewing experience has often been a bit slow. This post outlines a strategy for custom color options that leverages instant JS-based previewing in conjunction with selective refresh.
Demo of instant custom color previews in the customizer with the Linework theme.
Example Plugin: Custom Highlight Color
I’ll leverage the custom highlight color plugin as an example of this approach. I recommend downloading it from WordPress.org to follow along with, and I’ll provide a detailed walkthrough of the code with excerpts here.
This plugin only has one option – the highlight color. However, this approach can easily scale to additional options. As I’ll show later, it’s best to minimize your options and instead rely on code logic to generate or select variations as needed so that users can achieve custom results with minimal effort and decision-making. Finding the right balance of color options is an important step in the product design process, and one where even the past couple of default WordPress themes leaned a bit too far in the direction of options, in my opinion.
Adding Customizer Objects
The first step is to register your color options in the customizer via the PHP API. We can use the colors section provided by core, leaving the setting and control to be registered in your theme or plugin. The transport will be postMessage, indicating that we’ll preview the setting with JavaScript:
function custom_highlight_color_customize( $wp_customize ) {
$wp_customize->add_setting( 'custom_highlight_color', array(
'type' => 'option', // Change to theme_mod when using in a theme.
'default' => '#ff0',
'sanitize_callback' => 'sanitize_hex_color',
'transport' => 'postMessage',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'custom_highlight_color', array(
'section' => 'colors',
'label' => __( 'Highlight Color', 'custom-highlight-color' ),
) ) );
}
add_action( 'customize_register', 'custom_highlight_color_customize' );
The setting will use sanitize_hex_color, ensuring that a single secure hex color code is saved in the database for each option. For themes, you should set the type to theme_mod instead of option, and it’ll be stored with the rest of the theme-specific options automatically.
Color Patterns and Calculations
Next, we need to define the CSS that will display the custom color options. By putting this into a distinct function, the code can be leveraged in multiple preview and output contexts. When there’s a lot of CSS, I prefer to break this into a separate file to make it easier to maintain. You can even split distinct parts of the color CSS into different functions. For a simple example, here’s what Custom Highlight Color does:
function custom_highlight_color_css() {
require_once( 'color-calculations.php' ); // Load the color calculations library.
$background = get_option( 'custom_highlight_color', '#ff0' ); // The second argument is the default color when the theme mod is undefined.
// Set the text color to black or white, whichever provides higher contrast with the highlight color.
if ( custom_highlight_color_contrast_ratio( $background, '#000' ) > custom_highlight_color_contrast_ratio( $background, '#fff' ) ) {
$color = '#000';
} else {
$color = '#fff';
}
$css = '
::-moz-selection {
background: ' . $background . ';
color: ' . $color . ';
}
::selection {
background: ' . $background . ';
color: ' . $color . ';
}';
return $css;
}
You’ll notice that there are some color contrast checks here, which automatically set the text color to ensure that it contrasts with the highlight color. In this case it looks for the higher contrast between two colors, but typically you’ll want to ensure that the contrast is at least 4.5 between background and text colors. These checks leverage the color-calculations.php functions that I originally developed for the Fourteen Colors plugin. Feel free to take this file from the custom highlight color plugin and ship it with your project (make sure you update the function prefixes to match your plugin or theme).
In addition to the contrast check (which is done per the WCAG guidelines), the color calculations library has a handy function to “adjust” a color. You can use this to brighten or darken a color by a numeric amount, generating color variations automatically. The Fourteen Colors plugin uses this technique extensively.
Color Output
Now that our color patterns are defined, it’s easy to display the color CSS within style tags in wp_head. However, in the customizer preview, we also need an easy way to get the current colors in JavaScript. To do that, we’ll conditionally add the colors as data- attributes when is_customize_preview():
The colors should now display on the front end, but they won’t preview in the customizer just yet.
Instant Custoizer Previews with JavaScript
To preview the color changes instantly in the customizer, we first need to enqueue a JS file to manage color previews (if your theme already loads a customizer JS file, you can add to that in lieu of introducing another one):
In that file, we’ll follow the standard customizer preview conventions and bind to changes on our settings. For each of the colors, we’ll search for instances of the original color in the CSS, and replace them with the new color:
( function( $ ) {
wp.customize( 'custom_highlight_color', function( value ) {
value.bind( function( to ) {
// Update custom color CSS
var style = $( '#custom-highlight-color' ),
color = style.data( 'color' ),
css = style.html();
//css = css.replace( color, to );
css = css.split( color ).join( to ); // equivalent to css.replaceAll.
style.html( css )
.data( 'color', to );
} );
} );
} )( jQuery );
You’ll need to bind changes to each of your color settings if you have multiple color options. Now, you’ll be able to preview color changes instantly in the customizer. But what about the colors generated using color calculations in PHP?
Adding Selective Refresh for Secondary Colors
To supplement the instant JS previewing that updates the CSS with a search-and-replace of the color value, we need to add selective refresh support so that the CSS is re-rendered from PHP with our color calculations applied. The color patterns and calculations could be duplicated in JS, but that would become quite difficult to maintain and offers only a slight usability benefit. With selective refresh, we can leverage the existing function in PHP; it’s only a matter of adding a customizer partial that will be refreshed when one of the associated colors changes:
This code should be added within the same callback to the customize_register action that we added earlier. Multiple settings can be associated with a single partial; add the other setting ids to the array of settings.
Once that’s complete, you should be able to preview all of your custom colors in the customizer, with instant updates to base colors and updates to generated colors happening on a slight delay. Colors are safely stored in the database without the security issues of saving CSS there, and styles are applied on the front end as expected. I’m now leveraging this approach in all of my themes and plugins as I find it to provide a nice balance between user experience and code simplicity.
The custom highlight color plugin is a simple example, built specifically to be an example, but this approach easily scales to more colors as needed. For a more complex example, check out the code for the Fourteen Colors Plugin or the Figure/Ground theme (which runs this site and lets users customize every color).
For anyone still using the Twenty Fourteen theme with custom colors via the Fourteen Colors plugin, now would be a good time to try on a fresh coat of paint. I’ve just released version 1.4 of the plugin, which adds instant live-preview of base colors with all colors updating without a page refresh on a slight delay. This leverages the customizer’s selective refresh API added in 4.5 and is based on the same logic that powers the custom highlight color plugin. I’m leveraging this framework in all of my upcoming themes and will be posting a walkthrough of the code in the coming weeks. Here’s a quick visual demo of the new experience:
I’ve updated my Figure/Ground theme on WordPress.org (which also powers this blog) with a few nice enhancements:
All options in the customizer are now instantly live-previewed with postMessage.
Add support for selective refresh in the customizer for widgets, and generated colors.
There is now a social icon menu.
Redraw the background canvas when the page is resized to avoid pixelization.
Improve keyboard navigation (although this still needs additional work).
Update Genericons to version 3.4.1.
The new customization experience is the most notable enhancement. See every color change instantly as you play with it in the color picker, without any delay. Enjoy!
I finally got around to editing and posting the videos from last summer’s Chase the Music concert for Ayla & Jayden with the Boulder Cello Project. Here they are, including the world premiere of my composition Awe & Joy. Sheet music for all of the music from the concert is available for free on my sheet music library.
Special thanks to Clark Hodge of Chase the Music and Bill Shauck of the Boulder Cello Project for making this concert happen!
Three years ago, I created several interactive geometry apps while working for Saltire Software. As part of the process, I built the collections functionality for Euclid’s Muse (which I had created the previous year at Saltire), which includes the ability to download a collection of web-based applets as a standalone mobile app that can be processed through PhoneGap and published on app stores. The original intent was to publish a few of these apps on the app store and Google play myself, but I never got around to it. So, I decided to publish them as another site on celloexpressions.com. The five apps are:
I also created an index page based on the pseudo-random experiments page. I’m thinking about turning it into a simple WordPress theme with a fun background (animation off by default), anyone interested?
Featured images are native to WordPress core, allowing themes to represent posts and pages with images. But for many users, there are more important content formats than visuals. As a musician, I’ve explored different approaches to integrating WordPress’ audio functionality with post objects, most recently with the Sheet Music Library plugin.
I recently began exploring a new idea — a premium WordPress theme built specifically for musicians and musical groups — and I realized that WordPress needs a generic solution for the broad concept of associating audio files with posts. Post meta associated with specific custom post types or embedding audio directly in posts limits the opportunities for themes to display audio in unique ways and for plugins to create aggregated presentations of audio associated with different posts.
The Plugin
Featured Audio metabox in the editor.
Seeking to introduce a broad solution to the challenges of deeply integrating audio with core WordPress functionality, I’m introducing the Featured Audio plugin. Featured audio is perfect for musicians, podcasters, and anyone who publishes audio content on their site frequently. It works with every theme out of the box by displaying WordPress’ native (mediaelement.js) audio player at the top of the_content. Themes that style the audio player already will offer a customized experience for site visitors using featured audio.
Default featured audio display with the Twenty Fifteen theme.
I’d love to see more themes designed specifically to make audio a central component of sites’ content. If they all leverage this standardized featured audio plugin, users publishing audio content can get a wide variety of options for displaying their content and the content adapts seamlessly from theme to theme, just like publishers of visual content get with featured images.
The Importance of an API
To make featured audio as broadly extensible as possible, I spent a significant amount of time building public API functions into the plugin that facilitate custom implementations and integrations. Where it makes sense, these functions parallel the post_thumbnail/featured images API in WordPress core. However, the most notable difference is that theme support isn’t required. Taking a similar approach to the Featured Image in Content plugin (but also always showing the admin UI), the content is available regardless of theme support to allow a broader array of themes to be used. Theme support is used to indicate custom implementations, and turns off automatic front-end display of featured audio on a technical level.
A majority of sites probably don’t use audio at all, making the idea of featured audio probably inappropriate for WordPress core. But by building a core-like solution that themes and other plugins can extend, we can provide audio publishers with similar opportunities to publish freely with WordPress. And the development of themes and plugins that extend and customize the experience will further promote publishing audio with WordPress.
Infinite Possibilities
Once the base API is in place via the Featured Audio plugin, there are endless possibilities for themes and plugins to expand the potential of featured audio to offer tools for publishers. Plugins can expand the API to offer additional functions for custom theme display, or other tools such as widgets or shortcodes to aggregate featured audio across posts. The core playlist functionality is especially promising here.
Themes can do anything from moving where featured audio is displayed or adding it to archive views that don’t display the content to showcasing audio in unique and unexpected ways in the front-end UI. We’ve seen featured image headers, so why not featured audio headers, with the ability to set up a default audio file in the customizer. I’m excited to see what theme designers can come up with.
Feadured Audio Playlist widget in the Twenty Fifteen theme.
To offer a glimpse of the possibilities, I’ve built a few advanced features into the plugin. The API includes arguments for the main the_featured_audio() function to display the title and/or album art (which WordPress automatically pulls from MP3 files that have it) alongside the audio player for easy display of additional information in themes. The plugin also adds a Featured Audio Playlist widget, which displays a playlist with all of the featured audio associated with posts on the current view, such as a blog index, archive, or taxonomy page. The playlist output is also available directly via an API function for custom display in themes outside of the widget context. For example, a playlist could be displayed alongside a taxonomy title and description at the top of a taxonomy archive. Playlists are great because they allow visitors to listen to more of your content without repeated manual interactions, and in this scenario they’re automatically created and organized on-the-fly based on the existing organizational structure of a WordPress site.
Content Ownership and Hosting
One of my personal goals with featured audio is to encourage publishers to host their own audio content directly in their WordPress site. Third-party services such as Soundcloud offer advantages in distribution but when it comes to displaying content on sites, I frequently get the impression that users don’t realize that they can host and publish their audio content directly. WordPress is about democratizing publishing, and part of that mission is to give users complete control over their content. With better resources for publishing audio directly in WordPress, fewer users will feel a need to turn to third-party services. Long term, it would be awesome to see third party social and aggregation sources for audio content pulling data out of WordPress sites rather than the other way around, as it is currently.
Next Steps
Now that the plugin is live on the repository, publishers can start using featured audio with the basic experience on any theme out of the box. But the really exciting opportunities come with plugins and themes that extend the basic experience, and it’s up to you to start designing and building those projects. I’m hoping to build a premium musician-oriented theme that integrates with featured audio eventually, but the beauty of open source software is that anyone can create free or premium products that build on this functionality. I’m excited to see what the community can do together to improve the experience of publishing audio content with WordPress.
I’m launching another new site on celloexpressions.com this summer. Cello Expressions Photography could be considered a photo blog in many ways, but its primary purpose is to serve as a collection of visually stimulating and contextually significant imagery. Bringing my academic/professional interests to Cello Expressions for the first time, this site focuses on architecture, landscape architecture, and construction. Given my current internship at the USC Village project, expect a particular construction emphasis right now. Visually interesting textures and natural landscapes are also featured.
The site is using my Lucidus WordPress theme, resulting in a major focus on images and minimal UI. The theme will be publicly available soon, likely as a premium theme but possibly for free on WordPress.org depending on time. Note that due to the heavy use of images, it may take some time for the content to load, but once it does, it’s pretty cool!
This weekend I’m partnering with the Boulder Cello Project and Chase the Music to put on our 3rd annual concert for children in critical situations. This year’s concert is for a brother and sister and will feature a bold musical celebration of these two amazing kids.
My latest composition, Awe & Joy, scored for Flute, Horn, Percussion, and Cello Ensemble, will be premiered at the concert this Sunday, August 9, 2015 in Boulder, CO. The concert is free and we have plenty of room in the hall for guests. Details and RSVP here: https://www.eventbrite.com/e/concert-for-ayla-and-jayden-tickets-17769911283.
I’m working at the USC Village jobsite this summer as an intern for Hathaway Dinwiddie Construction Co. This is a huge project – 15 acre site, 6 buildings, over 1 million square feet – and USC has set up the usual webcams to monitor the construction progress. Unfortunately, theirs uses painfully outdated software that won’t even work in IE in compatibility mode unless you get really lucky. So I set up a page that pulls in the latest image from the camera and displays it full-screen, updating the image every second or as your connection allows so that you can see the progress.