Skip to content

Instantly share code, notes, and snippets.

View mattradford's full-sized avatar
👋

Matt Radford mattradford

👋
View GitHub Profile
@mattradford
mattradford / licence activation.php
Last active August 29, 2024 20:36
ACF5 Pro licence activation. Could be adapted for any other plugin that requires a licence key, but doesn't yet support defining it in wp-config. Fires on theme activation.
// Place this in wp-config
define( 'ACF_5_KEY', 'yourkeyhere' );
// Set ACF 5 license key on theme activation. Stick in your functions.php or equivalent.
function auto_set_license_keys() {
if ( ! get_option( 'acf_pro_license' ) && defined( 'ACF_5_KEY' ) ) {
$save = array(
'key' => ACF_5_KEY,
@mattradford
mattradford / body_class_descendants.php
Last active January 23, 2018 12:50
Body class on page and descendants
// Add class of "hub-$template_colour" to a page and its descendants
// Adapted from http://techtabby.com/how-to-add-body-class-to-page-and-descendants-wordpress/
function tend_parent_body_class( $classes ) {
if( is_page() ) {
$parents = get_post_ancestors( get_the_ID() );
$id = ($parents) ? $parents[count($parents)-1]: get_the_ID();
if ($id) {
if(get_field('colour_picker', $id )) {
$template_colour = get_field('colour_picker', $id);
$classes[] = 'hub-' . $template_colour;
@mattradford
mattradford / retina_logo.php
Created October 26, 2015 15:42
Retina logo with file added from a custom image field. Requires adjustment of retina image mixin and a double-size image.
<?php
if(get_field('website_logo','options')) {
$Array = get_field('website_logo','options');
$URL = $Array['url'];
$height = ($Array['height'])/2;
$width = ($Array['width'])/2;
echo '<a class="logo" href="'.esc_url(home_url('/')).'" style="background-image:url('.$URL.'); width:'.$width.'px; height:'.$height.'px;">'.get_bloginfo('name').'</a>';
} else {
@mattradford
mattradford / random_repeater.php
Created October 10, 2015 17:07
Super-easy get random ACF repeater row :) Improved to match HTML5 spec.
<?php
$repeater = get_field( 'testimonial','options' );
$rand = rand(0, (count($repeater) - 1));
echo '<blockquote><p>'.$repeater[$rand]['quote'].'</p>';
if (!empty($repeater[$rand]['citation'])) { echo '<cite>'.$repeater[$rand]['citation'].'</cite>';};
echo '</blockquote>';
?>
@mattradford
mattradford / random_repeater.php
Created October 10, 2015 17:02
Super-easy get random ACF repeater row :)
<div class="testimonials__content">
<?php
$repeater = get_field( 'testimonial','options' );
$rand = rand(0, (count($repeater) - 1));
echo $repeater[$rand]['quote'];
echo $repeater[$rand]['citation'];
?>
</div>
@mattradford
mattradford / acf_slick.php
Created September 18, 2015 14:12
ACF slick slider
<?php if( have_rows('billboard') ): ?>
<div class="billboard">
<?php while( have_rows('billboard') ): the_row();
$imageArray = get_sub_field('image');
$imageURL = $imageArray['url'];
?>
<div class="slide-item" style="background-image: url('<?php echo $imageURL;?>');" >
<h1><?php the_sub_field('title'); ?></h1>
</div>
@mattradford
mattradford / ga.php
Created September 2, 2015 18:59
Google Analytics
add_action('wp_footer', 'add_googleanalytics');
function add_googleanalytics() { ?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
@mattradford
mattradford / remove_emojii.php
Created September 2, 2015 18:59
Remove emoj
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
@mattradford
mattradford / product_array_sort.php
Created August 28, 2015 10:34
sort array of product variables
if(get_field('variations')) :
$product_variations = get_field('variations');
$product_variations_count = count( get_field( 'variations' ) );
echo $product_variations_count;
if($product_variations_count > 1) :
@mattradford
mattradford / featured image from gallery.php
Last active August 1, 2024 23:43
Set the featured image from the first entry in an ACF gallery field, saving only for a specified post type.