Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Return posts with maximum age of one year.
*
* Usage:
* add_filter( 'posts_where', 'wp_query_filter_max_one_year_old' );
* $my_query = new WP_Query( array( ... ) );
* remove_filter( 'posts_where', 'wp_query_filter_max_one_year_old' );
*
* @param string $where
<?php
$age_filter = function ( $where = '' ) use ( $options ) {
$where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-' . $options[ 'max_post_age' ] ) ) . "'";
return $where;
};
add_filter( 'posts_where', $age_filter );
$query = new WP_Query( $args );
remove_filter( 'posts_where', $age_filter );
<?php
/**
* Return posts with maximum age of one year.
*
* Usage:
* add_filter( 'posts_where', 'wp_query_filter_max_one_year_old' );
* $my_query = new WP_Query( array( ... ) );
* remove_filter( 'posts_where', 'wp_query_filter_max_one_year_old' );
*
* @param string $where
<?php
/**
* My Plugin
*
* @uses add_filter() for 'the_content': make love not content
* @uses add_filter() for 'get_the_excerpt': clearly announce all excerpts
*/
class my_plugin
{
function __construct() {
<?php
register_activation_hook( __FILE__, 'add_last_activity_for_all_users' );
function add_last_activity_for_all_users() {
global $wpdb;
$sql = $wpdb->prepare( "
SELECT
u.ID
FROM
$wpdb->users AS u
<?php
/**
* Return array containing all capabilities that user has access to.
* If he is an admin, the list will contain all available capability IDs.
*
* @param $user_id integer
* @return array
*/
function dtpr_get_all_user_capabilities( $user_id ) {
global $wpdb;
@cyberwani
cyberwani / limit.js
Created December 28, 2012 09:30 — forked from eteubert/limit.js
jQuery(function($) {
// configure
var comment_input = $( '#commentform textarea' );
var submit_button = $( '#commentform .form-submit' );
var comment_limit_chars = 1000;
// stop editing here
// display how many characters are left
$( '<div class="comment_limit_info"><span>' + comment_limit_chars + '</span> characters left</div>' ).insertAfter( comment_input );
<?php
// Alternative 1
function my_postbox_fun() {
# code...
}
postbox( 'My Awesome Postbox', 'my_postbox_fun' );
// Alternative 2
class my_example_class
{
@cyberwani
cyberwani / ack
Created December 28, 2012 09:34 — forked from eteubert/ack
ericteubert: ~/Sites/wordpress-single
➜ ack widget_text
wp-admin/includes/schema.php
307: 'widget_text' => array(),
wp-content/plugins/ikonum/class/class.custom-field-template.php
101: add_filter('widget_text', 'do_shortcode');
wp-includes/default-widgets.php
371: $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
<?php
class content_parser {
public function __construct() {
// apply parser to all posts
add_filter( 'the_content', array( __CLASS__, 'parse' ) );
}
public static function parse( $text ) {
global $post;