Skip to content

Instantly share code, notes, and snippets.

@rileypaulsen
rileypaulsen / lipsum.php
Created March 19, 2014 12:09
Lorem Ipsum Generator Function
function random_lipsum($amount = 1, $what = 'paras') {
$start = rand(0,30);
return simplexml_load_file("http://www.lipsum.com/feed/xml?amount=$amount&what=$what&start=$start")->lipsum;
}
@rileypaulsen
rileypaulsen / JSON REST API Tax Query
Created August 23, 2014 23:03
Use a tax_query in a JSON REST API request
//requires authentication
add_filter('json_query_vars','accept_new_queries');
add_filter('json_query_var-tax_query', 'filter_by_taxonomy');
function accept_new_queries($filters){
return array_merge($filters,array('tax_query'));
}
function filter_by_taxonomy($val){
@rileypaulsen
rileypaulsen / JSON REST API Basic Authentication
Created August 23, 2014 23:05
Use authentication with the JSON REST API
//Note: requires the Basic Auth plugin
$username = 'username';
$password = 'password';
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
);
$queryFilters = array(
@rileypaulsen
rileypaulsen / Update Term Count on Admin Term Management Page
Last active December 19, 2015 13:09
update the term count on admin term management page
$taxs = get_taxonomies();
foreach($taxs as $tax){
$args = array(
'hide_empty'=> false,
);
$terms = get_terms($tax,$args);
$termIDs = array(); //we need an array of IDs from all the terms in this taxonomy
foreach($terms as $term){
$termIDs[] = $term->term_id;
}
@rileypaulsen
rileypaulsen / Show Drafts and Pending Review in Parent Dropdown
Last active December 19, 2015 13:09
show all drafts and pending review in the "parent" dropdown
add_filter( 'page_attributes_dropdown_pages_args', 'so_3538267_enable_drafts_parents' );
add_filter( 'quick_edit_dropdown_pages_args', 'so_3538267_enable_drafts_parents' );
function so_3538267_enable_drafts_parents( $args ){
$args['post_status'] = 'draft,publish,pending';
return $args;
}
@rileypaulsen
rileypaulsen / Tax Query
Last active December 19, 2015 13:09
taxonomy query for get_posts()
'tax_query'=> array(
array(
'taxonomy' => 'decade',
'field' => 'id',
'terms' => $decadesArray,
'operator' => 'NOT IN'
)
)
@rileypaulsen
rileypaulsen / Retroactively Add Custom Fields
Last active December 19, 2015 13:09
retroactively add custom fields to existing posts
insert into wp_postmeta (post_id, meta_key, meta_value) select ID, '###FIELDNAME###', '1' from wp_posts WHERE post_type = '###POSTTYPE###'
@rileypaulsen
rileypaulsen / Post Formats via get_posts()
Last active December 19, 2015 13:09
query post formats in get_posts
$args = array(
'post_type'=> 'post',
'post_status' => 'publish',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' ),
'operator' => 'NOT IN'
@rileypaulsen
rileypaulsen / Order By Meta - get_posts()
Last active December 19, 2015 13:09
order results by meta value in get_posts()
$args = array(
'post_status' => 'publish',
'post_type' => '###POSTTYPE###',
'meta_key' => '###METAKEY###',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
@rileypaulsen
rileypaulsen / Gravity Forms User Capability
Last active December 19, 2015 13:09
add gravity forms capability to editors
function add_grav_forms(){
$role = get_role('editor');
$role->add_cap('gform_full_access');
}
add_action('admin_init','add_grav_forms');