Skip to content

Instantly share code, notes, and snippets.

@thoronas
thoronas / custom-date-query.php
Last active August 29, 2015 14:26
WP Query for events posts with upcoming dates
<?php
$current_date = date(Ymd);
$event_args = array(
'post_type' => 'event_post_type',
'meta_query' => array(
array(
'key' => 'date_field',
'value' => $current_date,
'compare' => '>='
)
@thoronas
thoronas / gist:2876f159404b7547d89b
Last active August 29, 2015 14:24
Function for uploading images from URL to WordPress. Thanks to Andrew Norcross for help with this.
<?php
function upload_external_image( $post_id, $img_url ) {
// load media handlers
require_once( ABSPATH . 'wp-admin' . '/includes/image.php' );
require_once( ABSPATH . 'wp-admin' . '/includes/file.php' );
require_once( ABSPATH . 'wp-admin' . '/includes/media.php' );
// create a temp file
$temp_file = download_url( $img_url );
@thoronas
thoronas / gist:3dbeb74d64193e039eab
Created May 31, 2015 20:38
WordPress: Run code only if user is logged in and an editor or admin
<?php
if( current_user_can('edit_others_pages') ) {
// do something
}
?>
@thoronas
thoronas / gist:11119681
Last active August 29, 2015 14:00
Get First category associated with a post
<?php
$post_categories = wp_get_post_categories( $post->ID );
// get first category id
$first_cat = get_category($post_categories[0]);
// get category term info.
$term_link = get_term_link( $first_cat->slug, $first_cat->taxonomy );
// do what you want with it.
echo "<a href='".$term_link."'>".$first_cat->name."</a>";
?>
@thoronas
thoronas / 0_reuse_code.js
Created March 7, 2014 17:48
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@thoronas
thoronas / gist:6394780
Created August 30, 2013 22:08
ACF Image function
<?php $image = wp_get_attachment_image_src(get_field('image'), 'medium'); ?>
<img src="<?php echo $image[0]; ?>">
@thoronas
thoronas / gist:5580467
Created May 14, 2013 23:13
Query posts from every term in a taxonomy
<?php
$terms = get_terms('taxonomy');
$count = count($terms);
//check if there are any terms
if ( $count > 0 ){
//loop through each term and query posts.
foreach($terms as $term){
@thoronas
thoronas / Gforms placeholder add on
Created April 16, 2013 21:41
Gravity Forms placeholder function.
//////// PLACEHOLDER TEXT //////////////
/* Add a custom field to the field editor (See editor screenshot) */
add_action("gform_field_standard_settings", "my_standard_settings", 10, 2);
function my_standard_settings($position, $form_id){
// Create settings on position 25 (right after Field Label)
if($position == 25){
@thoronas
thoronas / gist:5304261
Last active December 15, 2015 18:29
Code for using Flexslider with Advanced Custom Fields
<?php if(get_field('slider')): ?>
<script>
jQuery(window).load(function($) {
$('.flexslider').flexslider({
animation: "slide"
});
});