Skip to content

Instantly share code, notes, and snippets.

View kjbrum's full-sized avatar
🤘

Kyle Brumm kjbrum

🤘
View GitHub Profile
<?php
/**
* Get the ID of an attachment image using the file URL.
*
* @param string $url The URL of the image
* @return int $attachment The attachment image ID
*/
function wp_url_to_attachmentid( $url ) {
// Split the $url into two parts with the wp-content directory as the separator
$parsed_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url );
@kjbrum
kjbrum / get_all_strings_between.php
Last active May 8, 2018 19:18
Get all of the strings between 2 strings.
<?php
/**
* Get all of the strings between 2 strings.
*
* @param string $string The string to search
* @param string $start The string to start with
* @param string $end The string to end with
* @param boolean $case_sensitive Whether to make the search case sensitive or not
* @return array An array of all the string that were found between $start and $end
*/
@kjbrum
kjbrum / bash_functions.sh
Last active January 10, 2016 06:46
A collection of useful bash functions.
# Variables used
RED=$(tput setaf 1);
GREEN=$(tput setaf 2);
YELLOW=$(tput setaf 3);
WHITE=$(tput setaf 7);
# Throw an error
error() {
printf "${RED}Error:${WHITE} ${1}\n"
exit
@kjbrum
kjbrum / wp_get_first_category.php
Last active February 8, 2017 03:48
Get the first category of a post.
<?php
/**
* Get the first category of a post.
*
* @param int $id The id of a post
* @param int $id The field value to return
* @return mixed The category object or single field
*/
function wp_get_first_category( $id=null, $field='all' ) {
// Check if only a field value was supplied
@kjbrum
kjbrum / current_page_url_in.php
Last active December 10, 2015 01:08
Match the current URL against the supplied URL.
<?php
/**
* Match the current URL against the supplied URL.
*
* @param string $url The URL to be checked
* @return boolean
*/
function current_page_url_in( $url ) {
return ( strpos( $_SERVER['REQUEST_URI'], parse_url( $url, PHP_URL_PATH ) ) !== false );
}
@kjbrum
kjbrum / dir-to-filename.sh
Last active January 11, 2016 23:40
Rename all the files in a directory with their parent directory name.
#!/usr/bin/env bash
# Dir to Filename
# Rename all the files in a directory with their parent directory name.
# Copyright (C) Kyle Brumm <http://kylebrumm.com>
if [ "$1" = "-h" -o "$1" = "--help" ]; then cat <<EOF
Dir to Filename
@kjbrum
kjbrum / setup_script.sh
Last active January 12, 2016 00:13
Dev environment setup script.
#!/usr/bin/env bash
# Ask for the administrator password upfront.
sudo -v
# Keep-alive: update existing `sudo` time stamp until the script has finished.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
@kjbrum
kjbrum / array_to_csv.php
Created January 21, 2016 22:42
Convert an array to a CSV string.
<?php
/**
* Convert an array to a CSV string.
*
* @param array $array The array to be converted
* @return string The newly created string
*/
function array_to_csv( $array ) {
if(count( $array ) == 0 ) {
return null;
@kjbrum
kjbrum / str_replace_last.php
Created January 21, 2016 22:43
Replace the last occurrence of a string.
<?php
/**
* Replace the last occurrence of a string.
*
* @param string $search The string to search for
* @param string $replace The string to replace the search string
* @param string $subject The string to search and replace
* @return string The new string
*/
function str_replace_last( $search=null, $replace=null, $subject=null ) {
@kjbrum
kjbrum / wp-social-share-links.php
Last active November 13, 2017 00:27
Social share links