Categories
Code WordPress

Batch Update Multiple Git Repositories

This is a small shell script I use to batch update all of my git repositories that are children of a particular folder on my filesystem.

First create a single common folder to checkout all of your projects.  We’ll use vc.

mkdir ~/vc; cd ~/vc

Then place the following file inside the folder.

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
for FOLDER in $DIR/*/; do cd $FOLDER; echo "Updating " $FOLDER; git pull; cd $DIR; done
echo 'Done!'
view raw update-git.sh hosted with ❤ by GitHub

Now checkout your repositories into subfolder.

git clone https://github.com/stevenkword/WP-Present.git wp-present

You should now end up with a folder ~/vc/wp-present/ and we can test our update script.

sh ~/vc/update-git.sh

The script should traverse all of the subfolders and issue git pull commands in each.

Categories
Code WordPress

Use Wildcards with WP_User_Query to Search for WordPress Users

Information about how to use the wildcard user search in WP to properly populate things like JSON arrays for use with autocomplete JS.

WP_User_Query()

<?php
/**
* Search for users by name and return a JSON object of matches
*
* An example method that demonstrates how to use wildcards (*'s) when
* looking up users. This particular example would be used to create
* a new endpoint that would output a JSON array of returned users.
* Something like this would be useful for JS autocomplete queries.
*/
function skw_user_autocomplete( $search_term ) {
// Note the astrisks
$user_query = new WP_User_Query( array(
'search' => '*' . $search_term . '*',
) );
// Get the results from the query, returning the first user
$users = $user_query->get_results();
$user_ids = array();
foreach( $users as $user ) {
$user_ids[] = array(
'ID' => $user->data->ID,
'label' => $user->data->user_login,
'display_name' => $user->data->display_name
);
}
header( 'Content-Type: application/x-json' );
echo $json = json_encode( $user_ids );
die;
}
Categories
Code WordPress

WP CLI Batch Import

Sometimes you may receive your WordPress export XML files broken up into chunks.  When this happens, it can be cumbersome to import each file one-by-one.  Instead, try throwing everything into a sub-folder and running the following shell script:

# Activate the wordpress importer
wp plugin activate wordpress-importer --url=http://localhost/example.com/
# Iterate over all of the import files in a given folder.
for f in myfolder/*.xml; do wp import $f --authors=skip --skip=attachment --url=localhost/example.com/; done
Categories
Code WordPress

BuddyPress: Disable the Public Members Directory

The following code snippet can be used to disable access to the default BuddyPress Members Directory.  Simply add the following code to your theme’s functions.php file.

//Disable members directory
function disable_directory_members() {
 global $bp, $wp_query;
 $bp_is_directory = false;
 bp_core_redirect( $bp_root_domain );
}
add_action( 'bp_core_action_directory_members', 'disable_directory_members', 2 );

Update – 11/17/2011:  As of BuddyPress v1.5 this post this method is deprecated. You can now achieve this same result by unpublishing the ‘Members’ page from the ‘Pages menu in the admin area.

Categories
Code WordPress

BuddyPress: Remove a Sub Item from a Navigation Menu

This example demonstrates how to remove the Notifications item from the BuddyPress Settings default navigation menu. Simply add the code below to your theme’s functions.php file.

<?php
// Remove Settings->Notifications SubMenu
function skw_remove_notifications_subnav(){
global $bp;
if ( $bp->current_component == $bp->settings->slug ) {
bp_core_remove_subnav_item( $bp->settings->slug, 'notifications' );
}
}
add_action( 'wp', 'skw_remove_notifications_subnav', 2 );

Here is a similar example that could be used to remove the System Notices menu from the Messages section.

<?php
// Remove Messages->Notices
function skw_remove_notices_subnav() {
global $bp;
if ( $bp->current_component == $bp->messages->slug ) {
bp_core_remove_subnav_item( $bp->messages->slug, 'notices' );
}
}
add_action( 'wp', 'skw_remove_notices_subnav', 11 );