Skip to content

Instantly share code, notes, and snippets.

View aaronranard's full-sized avatar

Aaron Ranard aaronranard

View GitHub Profile
@aaronranard
aaronranard / is_child.php
Created November 26, 2013 18:35
WordPress: is a child page
/**
* Child page conditional
* @ Accept's page ID, page slug or page title as parameters
*/
function is_child( $parent = '' ) {
global $post;
$parent_obj = get_page( $post->post_parent, ARRAY_A );
$parent = (string) $parent;
$parent_array = (array) $parent;
@aaronranard
aaronranard / search_functions.php
Created September 20, 2013 21:04
WordPress - Blank Search to go to search.php
//For blank searches to use search.php
add_filter( 'request', 'my_request_filter' );
function my_request_filter( $query_vars ) {
if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) {
$query_vars['s'] = " ";
}
return $query_vars;
}
@aaronranard
aaronranard / repeater.php
Last active December 22, 2015 18:39
WordPress: Repeater Field Loop
<?php if(have_rows('repeater_field_name')): ?>
<ul>
<?php while( have_rows('repeater_field_name')): the_row(); ?>
<li><?php echo get_sub_field('image'); ?></li>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endwhile; ?>
</ul>
<?php endif; ?>
@aaronranard
aaronranard / edit-custom-category-list.php
Created July 23, 2013 20:04
WordPress: Edit Category / Post List
//Edit the Category List
add_filter( 'manage_edit-post_type_columns', 'set_custom_edit_post_type_columns' );
// Add to admin_init function
add_filter( 'manage_post_type_custom_column', 'manage_post_type_columns', 10, 3);
function manage_post_type_columns($out, $column_name, $id) {
switch ($column_name) {
case 'icon_image':
// Output custom post_type field here
@aaronranard
aaronranard / address-to-lat-long.php
Created July 23, 2013 15:13 — forked from bradp/gist:4999343
WordPress: Address to Latitude / Longitude
<?php
function brrad_geocode($street_address,$city,$state){
$street_address = str_replace(" ", "+", $street_address); //google doesn't like spaces in urls, but who does?
$city = str_replace(" ", "+", $city);
$state = str_replace(" ", "+", $state);
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$street_address,+$city,+$state&sensor=false";
$google_api_response = wp_remote_get( $url );
@aaronranard
aaronranard / feature.php
Last active December 15, 2015 18:39
WordPress: Get the Featured Image
<?php if (has_post_thumbnail( $project->ID )) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $project->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" alt="" />
<?php endif; ?>
@aaronranard
aaronranard / Mobile_Detect.php
Created March 29, 2013 15:32
PHP: Mobile Detect
<?php
/**
* MIT License
* ===========
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
@aaronranard
aaronranard / get-earliest-relative.php
Last active November 19, 2015 19:24
PHP: WordPress return top most parent ID
<?php
/**
* [Gets the top most parent of a post. If post is top most parent, returns ID]
* @return int [ID of top most parent]
*/
function get_earliest_relative($post){
if ($post->post_parent){
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
@aaronranard
aaronranard / acf-select.php
Created November 10, 2015 22:00
PHP: WordPress ACF output select field
/*
* Displaying a single value's Label
*/
$field = get_field_object('field_name');
$value = get_field('field_name');
$label = $field['choices'][ $value ];
@aaronranard
aaronranard / remove-acf-repeater-row.php
Last active October 23, 2015 22:23
acf's delete_sub_field call http://support.advancedcustomfields.com/forums/topic/remove-sub_field/ only makes the value null it doesn't actually delete the row. This does.
<?php
/*
* deleteSubField
*
* This function will delete a value of a sub field entirely and replace the rows correctly.
* ACF's built in delete_sub_field only sets the value to null
*
* @param $field_key (string) the field key of the top level custom field
* @param $repeater_key (string) the field key of the repeater element
* @param $post_id (int) the post_id of which the repeater is stored in