Skip to content

Instantly share code, notes, and snippets.

View craigsimps's full-sized avatar

Craig Simpson craigsimps

View GitHub Profile
@craigsimps
craigsimps / functions.php
Created April 18, 2015 16:16
Add custom meta field to WooCommerce order email.
add_filter('woocommerce_email_order_meta_fields', 'dog_breed_checkout_field_order_meta_fields', 10, 3 );
function dog_breed_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['dog_breed_name'] = array(
'label' => __( 'Dog Breed' ),
'value' => get_post_meta( $order->id, 'dog_breed_name', true ),
);
return $fields;
}
function customize_cpt_archive_query( $query ) {
if ( is_post_type_archive( 'gemstone' ) ) {
$query->set( 'posts_per_page', -1 );
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
return;
}
}
add_action( 'pre_get_posts', 'customize_cpt_archive_query', 1 );
@craigsimps
craigsimps / functions.php
Last active August 29, 2015 14:25
Add a link to BrowseHappy to the header of your Genesis Child theme if the visitor is using Internet Explorer 8 or below.
add_action( 'genesis_before', 'craigsimps_browsehappy' );
/**
* Show the browsehappy link
* if browser is less than IE8
*/
function craigsimps_browsehappy() {
?>
<!--[if lt IE 8]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
.browserupgrade {
background: #ccc;
color: #000;
padding: 10px 0;
text-align: center;
}
@craigsimps
craigsimps / functions.php
Last active August 29, 2015 14:25
Add the Modernizr JS library for Internet Explorer 8 users and below.
// remove the existing loading of html5shiv
remove_action( 'wp_head', 'genesis_html5_ie_fix' );
add_action( 'wp_enqueue_scripts', 'craigsimps_enqueue_modernizr' );
/**
* Load the modernizr library, with built in html5shiv for users of IE8 or below.
*/
function craigsimps_enqueue_modernizr() {
global $wp_scripts;
wp_enqueue_script( 'craigsimps-modernizr', get_stylesheet_directory_uri() . '/assets/js/modernizr.min.js', array('jquery'), '2.8.3', true );
@craigsimps
craigsimps / functions.php
Last active December 8, 2023 10:47
Escape the output of an ACF Wysiwyg field, but allow for oEmbed video and shortcodes.
<?php
function return_escaped_acf_wysiwyg( $field, $post_id = false ) {
// if there's no post id defined, get id from current post.
if( !$post_id ) {
$post_id = (int) get_the_ID();
}
// get the kses'd content
@craigsimps
craigsimps / functions.php
Created August 11, 2015 15:04
Insert content after a defined number of paragraphs.
<?php
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes here</div>';
@craigsimps
craigsimps / functions.php
Last active August 29, 2015 14:27
Remove the screen reader text class from the search form. Genesis 2.2.
<?php
add_filter( 'genesis_search_form', 'prefix_unhide_search_label' );
function prefix_unhide_search_label( $markup ) {
return str_replace( ' screen-reader-text', '', $markup );
}
@craigsimps
craigsimps / functions.php
Created August 15, 2015 10:32
By default, get_field will often return an array of post objects. If you try to access this using get_post_meta, you will see only an array of post ids. This function allows you to input the field name, and returns an array of post objects.
<?php
function return_array_of_objects_from_acf_ids( $field_name ) {
$post_ids = get_post_meta( get_the_ID(), $field_name, true );
foreach ( $post_ids as $post_id ) {
$objects[] = get_post( $post_id );
}
@craigsimps
craigsimps / functions.php
Created August 19, 2015 13:00
Return an array of ACF fields.
<?php
function get_acf_meta( $post_id, array $meta_keys ) {
foreach ( $meta_keys as $meta_key ) {
$fields[$meta_key] = get_post_meta( $post_id, $meta_key, true );
}
return $fields;