Skip to content

Instantly share code, notes, and snippets.

View kovshenin's full-sized avatar

Konstantin Kovshenin kovshenin

View GitHub Profile
<?php
function my_pre_user_query( $query ) {
$where = get_posts_by_author_sql( 'post' ) . " AND MONTH(post_date) = " . date( 'm', strtotime( '-1 month' ) );
$query->query_from = str_replace( get_posts_by_author_sql( 'post' ), $where, $query->query_from );
$query->query_where .= " AND post_count > 0 ";
$query->query_limit .= " LIMIT 5 ";
}
function my_magic_loop() {
@kovshenin
kovshenin / something.php
Created July 10, 2012 07:17
Yes, you can use printf and sprintf in WordPress too!
<?php
// Dirty, easy to miss a ' or " or .
echo '<a href="' . get_permalink() . '" class="link">' . get_the_title() . '</a>';
// Clean, easier to read
printf( '<a href="%s" class="link">%s</a>', get_permalink(), get_the_title() );
// Almost as clean, and more secure, maybe a little paranoic :)
printf( '<a href="%s" class="link">%s</a>', esc_url( get_permalink() ), esc_html( get_the_title() ) );
@kovshenin
kovshenin / escape-ngg.php
Created July 16, 2012 13:22
Converts NextGen Galleries to native WordPress Galleries. Read code for instructions.
<?php
/**
* Moved to: https://github.com/kovshenin/escape-ngg
*/
@kovshenin
kovshenin / fix-paypal-csv-dates.py
Created July 17, 2012 04:51
Rewrite m/d/y dates to Y-m-d in a PayPal CSV export
"""
This script will rewrite the date column in a PayPal CSV
export from m/d/y to Y-m-d format.
Usage:
# python fix-paypal-csv-dates.py input_file.csv > output_file.csv
"""
import csv, sys
from time import strptime, strftime
filename = sys.argv[1]
@kovshenin
kovshenin / shortcodes-test.php
Created July 19, 2012 14:05
Here's why you should not use dashes in WordPress shortcodes
<?php
// Shortcodes
add_shortcode( 'my', function() { return 'my'; } );
add_shortcode( 'my-shortcode', function() { return 'my-shortcode'; } );
add_shortcode( 'my_shortcode', function() { return 'my_shortcode'; } );
/**
* Results:
*
* [my] - my
<?php
$content = '';
$shortcodes = array( 'one', 'two', 'three', 'four' );
foreach ( $shortcodes as $shortcode ) {
add_shortcode( $shortcode, function() use ( $shortcode ) { return $shortcode; } );
$content .= sprintf( ' Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. [%s]', $shortcode );
}
$content = str_repeat( $content, 10 );
@kovshenin
kovshenin / plugin.php
Created July 27, 2012 12:45
Register some bogus post types with icons.
<?php
/**
* Plugin Name: Register Some Post Types
*/
add_action( 'init', 'my_register_post_types' );
function my_register_post_types() {
register_post_type( 'cpt-1', array(
'label' => 'Image',
'public' => true,
'show_in_menu' => true,
@kovshenin
kovshenin / plugin.php
Created August 17, 2012 07:53
Featured Image Size in Metabox
<?php
/**
* Plugin Name: Featured Image Size in Metabox
* Description: Adds 123x456 to the metabox title in featured image.
*/
add_action( 'do_meta_boxes', 'my_do_meta_boxes' );
function my_do_meta_boxes( $post_type ) {
global $wp_meta_boxes;
if ( ! current_theme_supports( 'post-thumbnails', $post_type ) || ! post_type_supports( $post_type, 'thumbnail' ) )
@kovshenin
kovshenin / plugin.php
Created October 4, 2012 13:19
Remove posts from a certain category
<?php
/**
* Plugin Name: Hide posts from category on home page
*/
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
// Category IDs to exclude
$exclude_categories = array( 1, 2, 3 );
if ( $query->is_home() )
<?php
$a = 15;
add_filter( 'my_filter', function() use ( $a ) {
return "I'm a filter and here's a: $a";
} );
$s = apply_filters( 'my_filter', '' );
echo $s;