Skip to content

Instantly share code, notes, and snippets.

View mjangda's full-sized avatar

Mohammad Jangda mjangda

View GitHub Profile
@mjangda
mjangda / zones-json-feed.php
Created August 13, 2012 19:43
Output a JSON Feed of posts in a zone
<?php
// This will be accessible at /feed/zones-json/
add_feed( 'zones-json', 'x_render_zones_json_feed' );
function x_render_zones_json_feed() {
if ( function_exists( 'z_get_zone_query' ) ) {
$query = z_get_zone_query( 'zone-name' );
header('Content-type: application/json');
@mjangda
mjangda / php-shuffle.php
Created July 26, 2012 18:18
Shuffle a batch of posts in PHP instead of using orderby => rand with WP_Query
<?php
$testimonial = false;
$testimonials_query = new WP_Query( array( 'fields' => 'ids', 'posts_per_page' => 20 ) );
if ( ! empty ( $testimonials_query->posts ) ) {
$testimonials = $testimonials_query->posts;
shuffle( $testimonials );
$testimonial_id = array_shift( $testimonials );
$testimonial = get_post( $testimonial_id );
@mjangda
mjangda / rsync-svn.sh
Created July 19, 2012 15:59
Bash script to sync to svn repos using rsync.
rsync -rv --delete --exclude='.svn' /from /to
cd /to
svn status | grep '^!' | awk '{print $2}' | xargs svn rm
svn status | grep '^?' | awk '{print $2}' | xargs svn add
@mjangda
mjangda / remove-post-terms.php
Created July 2, 2012 09:12
Remove multiple terms from a post
<?php
/**
* Helper function since this function doesn't exist in core
*/
function remove_post_terms( $post_id, $terms_to_remove, $taxonomy ) {
$terms_to_remove = array_map( 'intval', $terms_to_remove );
array_filter( $terms_to_remove, function( $term ) { return ! empty( $term ); } );
// Get the existing terms and only keep the ones we don't want removed
@mjangda
mjangda / parent-child-css.php
Last active October 6, 2015 10:17
Properly queueing parent and child style.css, props Viper007Bond
<?php
add_action( 'wp_enqueue_scripts', function() {
wp_register_style( 'parent-theme', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-theme', get_stylesheet_uri(), array( 'parent-theme' ) );
} );
@mjangda
mjangda / add-origin.php
Created June 20, 2012 09:17
Adding additional origins to the WordPress Origin API
<?php
add_filter( 'allowed_http_origins', 'my_add_origins' );
function my_add_origins( $origins ) {
$origins[] = 'http://www.example.com'; // this will add www.example.com to the list of allowed origins when send_origin_headers() is called
return $origins;
}
@mjangda
mjangda / dump-rewrite-rules.php
Created June 15, 2012 17:43
Dump a list of your rewrite rules.
<?php
function dump_rewrite_rules() {
$tmp_file = '/tmp/rewrite_rule_dump.txt'; // define filename here if needed
if ( !touch( $tmp_file ) || !is_writeable( $tmp_file ) ) {
error_log( 'dump_rewrite_rules: The file ' . $tmp_file . ' is not writable. Please check the permissions.' );
return;
}
global $wp_rewrite;
$rules = var_export( $wp_rewrite, true );
@mjangda
mjangda / gist:2911563
Created June 11, 2012 17:47
Proper parent-child queueing of scripts
<?php
add_action( 'wp_enqueue_scripts', 'my_parent_register_scripts', 1 );
function my_parent_register_scripts() {
// register scripts here
}
add_action( 'wp_enqueue_scripts', 'my_parent_enqueue_scripts' );
@mjangda
mjangda / enqueue-parent-child-scripts.php
Created June 11, 2012 17:42
Best way to enqueue parent-child stylesheets (instead of using @import)
<?php
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
function my_enqueue_scripts() {
wp_register_style( 'parent-theme', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-theme', get_stylesheet_uri(), array( 'parent-theme' ) );
}
@mjangda
mjangda / zoninator-current-user-can-edit-filter.diff
Created May 31, 2012 14:38
Add a filter to to allow limiting access by zone.
Index: zoninator.php
===================================================================
--- zoninator.php (revision 67407)
+++ zoninator.php (working copy)
@@ -164,10 +164,11 @@
case 'insert':
case 'update':
+ $zone_id = $this->_get_post_var( 'zone_id', 0, 'absint' );
+