Skip to content

Instantly share code, notes, and snippets.

@tjhole
tjhole / ACF: Random image from Gallery
Last active January 26, 2020 02:21
ACF: Random image from Gallery
@tjhole
tjhole / WORDPRESS: Query Posts - Break into Rows
Created January 15, 2014 15:17
WORDPRESS: Query Posts - Break into Rows
<?php
$args = array(
'post_type' => 'posts',
'posts_per_page' => -1,
);
$total = 0;
query_posts($args);
while ( have_posts() ) : the_post();
@tjhole
tjhole / HTML - IE8 SUPPORT CDN
Created January 16, 2014 14:45
HTML - IE8 SUPPORT CDN
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.2/html5shiv.js"></script>
<script src="//s3.amazonaws.com/nwapi/nwmatcher/nwmatcher-1.2.5-min.js"></script>
<script src="//html5base.googlecode.com/svn-history/r38/trunk/js/selectivizr-1.0.3b.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.1.0/respond.min.js"></script>
<![endif]-->
@tjhole
tjhole / WORDPRESS: Full Menu
Created February 21, 2014 16:21
WORDPRESS: Full Menu
<?php
$defaults = array(
'theme_location' => '',
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
@tjhole
tjhole / WORDPRESS: Menu Short
Created February 21, 2014 16:22
WORDPRESS: Menu Short
<?php
$defaults = array(
'theme_location' => 'primary',
'container' => 'false',
'menu_class' => 'menu',
'menu_id' => 'nav',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
);
@tjhole
tjhole / ACF: Image (Interchange Foundation 5)
Created April 2, 2014 14:56
ACF: Image (Interchange Foundation 5)
<?php
$attachment_id = get_field('field_name');
$image_thumbnail = wp_get_attachment_image_src( $attachment_id, "thumbnail" );
$image_medium = wp_get_attachment_image_src( $attachment_id, "medium" );
$image_large = wp_get_attachment_image_src( $attachment_id, "large" );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img data-interchange="[<?php echo $image_thumbnail[0]; ?>, (default)], [<?php echo $image_medium[0]; ?>, (large)]"/>
@tjhole
tjhole / WORDPRESS: rename admin items
Created April 3, 2014 11:26
WORDPRESS: Rename admin items
add_filter('gettext', 'rename_admin_menu_items');
add_filter('ngettext', 'rename_admin_menu_items');
/**
* Replaces wp-admin menu item names
*
* @author Daan Kortenbach
*
* @param array $menu The menu array.
*
* @return $menu Menu array with replaced items.
@tjhole
tjhole / WORDPRESS: Redirect based on location using Cloudflare
Created April 4, 2014 10:27
WORDPRESS: Redirect based on location using Cloudflare
<?php
// Geo Redirect using Cloudflare
$activepage = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
$page_from = "www.domain.tld/";
$page_to = "www.newdomain.tld/";
?>
<!-- Country Code : <?php echo $country_code; ?> -->
<!-- Active URL : <?php echo $activepage; ?> -->
@tjhole
tjhole / PHP: String to ID
Created April 9, 2014 11:41
PHP: String to ID
function toID($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
@tjhole
tjhole / WORDPRESS: Create safe ID from ACF title
Created April 19, 2014 13:15
WORDPRESS: Create safe ID from ACF title
function safeID( $string, $separator = '_' )
{
$accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
$special_cases = array( '&' => 'and');
$string = mb_strtolower( trim( $string ), 'UTF-8' );
$string = str_replace( array_keys($special_cases), array_values( $special_cases), $string );
$string = preg_replace( $accents_regex, '$1', htmlentities( $string, ENT_QUOTES, 'UTF-8' ) );
$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);
$string = preg_replace("/[$separator]+/u", "$separator", $string);