Skip to content

Instantly share code, notes, and snippets.

<?php
function exclude_cats() {
if( ! current_user_can( 'manage_options' ) ) {
add_filter( 'list_terms_exclusions', 'exclude_args', 10, 2 );
}
}
function exclude_args( $exclusions, $args ) {
$current_user = wp_get_current_user();
@Clorith
Clorith / gist:7675143
Created November 27, 2013 12:50
wp_nav_menu_items filter example
<?php
function theme_menu_append( $items ) {
$prepend = "";
$append = "";
$prepend .= "<li>First item always!</li>";
$append .= "<li>This is the 2nd to last item</li>";
$append .= "<li>This is the very last item</li>";
<?php
$gallery = get_post_gallery( get_the_ID(), false );
echo 'IDs: ' . $gallery['ids'];
foreach( $gallery['src'] AS $src )
{
echo $src;
}
function force_utf8( $string ) {
$utf8 = "";
$string = str_split( $string );
for ( $i = 0; $i < count( $string ); $i++ ) {
$utf8 .= mb_convert_encoding( $string[$i], "UTF-8", mb_detect_encoding( $string[$i] ) );
}
return $utf8;
}
$fetched = '21082013';
date_default_timezone_set( 'UTC' );
$date = DateTime::createFromFormat( 'dmY', $fetched );
echo $date->format( 'Y-m-d' );
@Clorith
Clorith / gist:6218138
Created August 13, 2013 05:26
Quickie on using forms in WordPress
function my_post_check()
{
if ( isset( $_POST['myslug-name'] ) )
{
Do form stuff here
}
}
add_action( 'init', 'my_post_check' );
@Clorith
Clorith / gist:6152213
Created August 4, 2013 22:30
WordPress hook and function to display various posts and custom post types on author pages
/*
* Function for modifying the main query
*/
function modify_the_loop( $query ) {
// Check that this is the main query, not a secondary one, and trigger if it's the author page'
if ( $query->is_main_query() && $query->is_author() )
{
$query->set( 'post_type', array( 'post', 'page', 'CPT', 'CPT2' ) );
}
}
<?php
function theme_custom_post_type() {
$labels = array(
'name' => _x( 'Clues', 'post type general name' ),
'singular_name' => _x( 'Clue', 'post type singular name' ),
'add_new' => _x( 'Add', 'book' ),
'add_new_item' => __( 'Add clue' ),
'menu_name' => __( 'Clues' )
);
$args = array(
@Clorith
Clorith / gist:5701123
Created June 3, 2013 20:29
wordpress custom rewrite rules
<?php
function theme_rewrites( $wp_rewrite ) {
$rules = array(
'coordinates/([^/]+)/?$' => 'index.php?pagename=coordinates&coords=$matches[1]',
);
$wp_rewrite->rules = array_merge( $rules, $wp_rewrite->rules );
}
add_action( 'generate_rewrite_rules', 'theme_rewrites' );
jQuery(document).ready(function($){
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('#myMediaArea').click(function(e) {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
var id = button.attr('id').replace('_button', '');
_custom_media = true;