Skip to content

Instantly share code, notes, and snippets.

View kevinwhoffman's full-sized avatar

Kevin W. Hoffman kevinwhoffman

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / plugin.php
Last active April 15, 2022 20:46 — forked from mathetos/plugin.php
Dependent Plugin Activation/Deactivation and Alert
<?php
/*
* Dependent Plugin Activation/Deactivation
*
* Sources:
* 1. https://pippinsplugins.com/checking-dependent-plugin-active/
* 2. http://10up.com/blog/2012/wordpress-plug-in-self-deactivation/
*
*/
/*
- I have a CPT named Galleries and within this CPT there are 6 individual galleries, all using the same format.
- I have used the Enhanced Media Library plugin to allow a category(ies) to be added each image uploaded.
- single-galleries.php - is a template used for displaying each of these individual galleries.
- Each individual gallery has aprox 4 categories in use that are assigned to the different images.
- There are aprox 24 categories for all of the images across the 6 individual galleries(1-2 per image)
Problem: code for "gallery-filter" below is currently echoing out ALL(~24) media categores.
Solution needed: for only those categories acutally used on the page (rememeber 6 diff galleries)
@kevinwhoffman
kevinwhoffman / wp-login-redirect.php
Last active October 22, 2015 08:54
WP Redirect Users After Login
<?php
/**
* Redirect users after login
*/
add_filter('login_redirect', 'vms_login_redirect', 10, 3);
function vms_login_redirect( $redirect_to, $request, $user ) {
if( !isset( $_REQUEST['redirect_to'] ) ) {
if ( current_user_can( 'manage_options' ) ) { // does not return true even for admins
return admin_url();
@kevinwhoffman
kevinwhoffman / wp-user-query.php
Last active October 19, 2015 02:33
WP_User_Query with Serialized Array
<?php
// Return users with a single language (English)
$args = array(
'meta_key' => 'wpcf-language-skill',
'meta_value' => 'English'
'meta_compare' => 'LIKE'
);