Skip to content

Instantly share code, notes, and snippets.

View hlashbrooke's full-sized avatar
🎲

Hugh Lashbrooke hlashbrooke

🎲
View GitHub Profile
@hlashbrooke
hlashbrooke / function.php
Created July 12, 2013 11:18
Simple way to generate a random string/password in PHP
<?php
function random_password( $length = 8 ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
$password = substr( str_shuffle( $chars ), 0, $length );
return $password;
}
?>
@hlashbrooke
hlashbrooke / function.php
Last active September 17, 2020 21:53
WooCommerce Product Vendors: Change vendor URL slug
add_filter( 'product_vendors_vendor_slug', 'change_vendor_url_slug' );
function change_vendor_url_slug( $slug ) {
$slug = 'new_url';
return $slug;
}
@hlashbrooke
hlashbrooke / function.php
Created February 28, 2014 08:41
WordPress: Add post types to the 'At a glance' widget
<?php
add_filter( 'dashboard_glance_items', 'custom_glance_items', 10, 1 );
function custom_glance_items( $items = array() ) {
$post_types = array( 'post_type_1', 'post_type_2' );
foreach( $post_types as $type ) {
if( ! post_type_exists( $type ) ) continue;
@hlashbrooke
hlashbrooke / function.php
Created October 16, 2014 12:23
PHP: Loop through each character in a string
<?php
$str = "String to loop through"
$strlen = strlen( $str );
for( $i = 0; $i <= $strlen; $i++ ) {
$char = substr( $str, $i, 1 );
// $char contains the current character, so do your processing here
}
?>
@hlashbrooke
hlashbrooke / functions.php
Last active June 26, 2020 21:33
Seriously Simple Podcasting: Hide episode details form beneath the audio player
add_filter( 'ssp_show_episode_details', '__return_false' );
@hlashbrooke
hlashbrooke / function.php
Last active May 30, 2020 15:39
WordPress: Check if user role exists
function role_exists( $role ) {
if( ! empty( $role ) ) {
return $GLOBALS['wp_roles']->is_role( $role );
}
return false;
}
@hlashbrooke
hlashbrooke / class1.php
Last active May 26, 2020 01:03
WordPress: Use checkbox term selection for non-hierarchical taxonomies - http://www.hughlashbrooke.com/wordpress-use-checkbox-term-selection-for-non-hierarchical-taxonomies/
<?php
class Tag_Checklist {
private $taxonomy;
private $post_type;
function __construct( $taxonomy, $post_type ) {
$this->taxonomy = $taxonomy;
$this->post_type = $post_type;
@hlashbrooke
hlashbrooke / functions.php
Last active May 26, 2020 01:03
WordPress: Create a custom metabox that works in exactly the same way as the existing 'Featured Image' box on the post edit screen. In this case the image is called 'Listing Image' (with all the function and variable names correlating to that), but you can change the strings to whatever you need them to be.
<?php
add_action( 'add_meta_boxes', 'listing_image_add_metabox' );
function listing_image_add_metabox () {
add_meta_box( 'listingimagediv', __( 'Listing Image', 'text-domain' ), 'listing_image_metabox', 'post', 'side', 'low');
}
function listing_image_metabox ( $post ) {
global $content_width, $_wp_additional_image_sizes;
$image_id = get_post_meta( $post->ID, '_listing_image_id', true );
@hlashbrooke
hlashbrooke / functions.php
Created October 12, 2018 12:52
Filtering items available in media library - taken from v1 of the WooCommerce Product Vendors extension.
/**
* Only show current vendor's media in the media library
* @param array $request Default request arguments
* @return array Modified request arguments
*/
add_filter( 'request', 'pv_restrict_media_library', 10, 1 );
function pv_restrict_media_library( $request = array() ) {
if( ! is_admin() ) {
return $request;
@hlashbrooke
hlashbrooke / functions.php
Last active March 28, 2020 03:56
Modify Bible version used by Bible Readings for Seriously Simple Podcasting
<?php
add_filter( 'ssp_bible_readings_version', 'my_bible_version', 10, 2 );
function my_bible_version ( $version, $episode_id ) {
$version = 'NKJV'; // All available versions are listed here: https://www.biblegateway.com/versions/
return $version;
}
?>