Skip to content

Instantly share code, notes, and snippets.

View kevinwhoffman's full-sized avatar

Kevin W. Hoffman kevinwhoffman

View GitHub Profile
@kevinwhoffman
kevinwhoffman / author-shortcode.php
Created December 10, 2015 22:24
Author Shortcode
<?php
function get_the_author_display_name() {
return get_the_author_meta( 'display_name' );
}
add_shortcode( 'author', 'get_the_author_display_name' );
@kevinwhoffman
kevinwhoffman / responsive-images-notes.md
Last active May 18, 2020 13:31
WordPress 4.4 Responsive Images Notes

Notes and Resources for Responsive Images in WordPress 4.4

Following the addition of responsive images support in WordPress 4.4, there has been a lot of confusion over what WordPress does "automatically" and what is left up to the developer. I've gathered the following notes from chatting with members of the core team and also my own investigation of the 4.4 source. Here's what I've discovered:

  • Following the 4.4 update, WordPress automatically serves the following images responsively:

    • Content images added via the main content editor will now be served with srcset markup. This is accomplished by filtering the_content() prior to display, which means it applies to existing posts and new posts going forward.
    • Featured images called by the_post_thumbnail() also get srcset markup without any additional steps required.
  • Images that are referenced from custom fields will not be served responsively unless the theme calls the image via wp_get_attachment_image() OR constructst the responsive m

@kevinwhoffman
kevinwhoffman / acf-lat-lng-fields.php
Created January 22, 2016 01:21
Split ACF Google Map Field Data Into Separate Custom Fields
<?php
/**
* In order for Google Maps Builder Pro to work with ACF map fields,
* lat and lng are saved as separate fields in the function below.
*/
function acf_save_lat_lng( $post_id ) {
// get value of ACF map field
@kevinwhoffman
kevinwhoffman / acf-update-post-thumbnail.php
Created January 30, 2016 16:40
Update post thumbnail with value of ACF Image field
<?php
function acf_update_post_thumbnail( $post_id ) {
$thumbnail_id = get_post_meta( $post_id, 'acf_image_field' );
set_post_thumbnail( $post_id, $thumbnail_id );
}
// run after ACF saves the $_POST['acf'] data
add_action( 'acf/save_post', 'acf_update_post_thumbnail', 20 );
@kevinwhoffman
kevinwhoffman / register-taxonomy.php
Last active February 8, 2016 20:16
Taxonomy w/ Hierarchical URLs
<?php
$labels = array(
'name' => 'Regions',
'label' => 'Regions'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'label' => 'Regions',
@kevinwhoffman
kevinwhoffman / prepopulate-gf.php
Last active February 17, 2016 01:45
Pre-populate Gravity Form Field Based on Query Parameter
<?php
/**
* Given a URL like http://example.com/?id=1 this function will pre-populate a Gravity Form field called 'fname'.
* For more info, see GF docs: https://www.gravityhelp.com/documentation/article/gform_field_value_parameter_name/
*/
add_filter( 'gform_field_value_fname', 'populate_fname' );
function populate_fname() {
$id = $_GET['id'];
@kevinwhoffman
kevinwhoffman / talk-submission.md
Created July 8, 2016 03:37
Demystifying Responsive Images in WordPress

Demystifying Responsive Images in WordPress

Whether you are an advanced WordPress theme developer or simply editing content from the dashboard, there is one question that everyone asks at one point or another:

What size should my images be?

This already complex question takes on a whole new dynamic with the recent addition of responsive images in WordPress 4.4. While the basic concept of responsive images may be easy to grasp, a true understanding of the inner workings is far more elusive. In this talk, we will demystify responsive images and learn how to strike a proper balance between image performance and image quality within any context.

Below is a brief overview of the main points we will touch upon throughout this talk:

@kevinwhoffman
kevinwhoffman / editor-styles-from-customizer.php
Last active July 14, 2016 06:15
Build editor stylesheet from customizer settings
<?php
// Place in functions.php of Twenty Sixteen to see editor background take on color of customizer background.
/**
* Build editor stylesheet from customizer settings upon customizer save.
*/
function kwh_build_stylesheet() {
$upload_dir = wp_upload_dir();
$stylesheet = $upload_dir['basedir'] . '/kwh-editor-style.css';
$styles = '';
@kevinwhoffman
kevinwhoffman / add-styles-to-visual-editor.php
Last active January 30, 2022 14:15
Add customizer styles to visual editor
<?php
/**
* Adds styles from customizer to head of TinyMCE iframe.
* These styles are added before all other TinyMCE stylesheets.
* h/t Otto.
*/
function kwh_add_editor_style( $mceInit ) {
// This example works with Twenty Sixteen.
$background_color = get_theme_mod( 'background_color' );
$styles = '.mce-content-body { background-color: #' . $background_color . '; }';
@kevinwhoffman
kevinwhoffman / acf-seo-image.php
Last active November 8, 2020 18:29 — forked from bahia0019/acf-seo-image.php
For ACF users, creates an easy to use function that wraps your image in HTML5 <figure> adds <figcaption>. Also adds Schema markup in JSON LD. Parameters are 1. ACF field name, 2. Size, 3. Figure CSS class, 4. Figcaption CSS class.
function fsc_figure( $image, $size, $imageclass, $captionclass ){
/**
* Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
*
* @param string $image The ACF field name (i.e. 'your_photo_name').
* @param string $size Thumbnail size (i.e. 'Thumbnail', 'Medium', 'Large')
* @param string $imageclass The Figure class you want to use (ex: 'my-figure')
* @param string $captionclass The Figcaption class you want to use (ex: 'caption-blue')
*/