Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@phlbnks
phlbnks / update_htpasswd.sh
Created July 26, 2017 08:52
Bash script to help manage .htpasswd files
#!/bin/bash
#
# Manage .htpasswd files
# Store script name for use in output.
me=$( basename $0 )
# Utility function for exiting.
#!/bin/bash -e
#
# Description:
# This will deploy WordPress in the current directory.
# Without modification it:
# - will configure basic security:
# - remove initial user created
# - deploy 6G firewall in .htaccess
# - attempt to prevent user enumeration in .htaccess
# - protect sensitive files and disallow executables in /wp-uploads
@phlbnks
phlbnks / time_ago.php
Last active February 3, 2020 10:33
Filter WordPress date functions to return relative time strings.
/**
* Filter the_time and get_the_date to return a relative string.
* Inspired by http://www.jasonbobich.com/2011/04/10/a-better-way-to-add-time-ago-to-your-wordpress-theme/
* Based on code from https://buddypress.trac.wordpress.org/browser/tags/2.8.2/src/bp-core/bp-core-functions.php#L1118
*
* @param string $formatted The formatted time.
* @param string $format The time format used.
* @param object $the_post Optional - current Post object.
* @return string Modified formatted time.
*/
@phlbnks
phlbnks / time_machine.php
Last active May 20, 2018 03:28
Filter the *displayed* time of posts in WordPress to move them into the past/future.
/**
* Filter the_time function to show posts as being from another date/time.
*
* @param string $formatted The formatted time.
* @param string $format The time format used.
* @return string Modified formatted time.
*/
function cc_time_machine( $formatted, $format ) {
$offset = get_option( 'cc_time_offset' ); // Offset in days, can be negative.
if ( $offset ) {
@phlbnks
phlbnks / wp_helper.sh
Last active April 16, 2021 18:02
Utility script to help manage WordPress sites on an EasyEngine server (but not limited to EE)
#!/bin/bash
# Help / usage info.
USAGE=$'WordPress Helper script, built for EasyEngine but should be widely compatible.\nWithout any args it will search /var/www/ for WordPress sites, loop over them in alphabetical order and check for core and plugin updates.\nAlso accepts:\n\t--sites=[space seperated list of site paths relative to /var/www]\n\t--update=[plugins|wp|all].'
# Die function for exiting on errors.
die () {
echo "${1}, exitting..." >&2 ; echo ; exit 1
}
@phlbnks
phlbnks / cpt_term_list.php
Created December 21, 2016 15:27
Get a list of terms from a shared taxonomy that are used on a particular post type only
<?php ?>
<label for="filter-country">Country</label>
<select id="filter-country" class="form-control" name="country">
<option selected="" value="">All countries</option>
<?php
if ( false === ( $cptcountries = get_transient( 'cpt_countries' ) ) ) {
global $wpdb;
$cpt_countries = $wpdb->get_col( "select distinct term_taxonomy_id from $wpdb->term_relationships where object_id in ( select ID from $wpdb->posts where post_type='cpt' )" );
$cptcountries = get_terms( array( 'taxonomy' => 'country', 'include' => $cpt_countries ) );
if ( ! empty( $cptcountries ) && ! is_wp_error( $cptcountries ) ) {
@phlbnks
phlbnks / tinymce_garlicjs_compat.php
Created December 7, 2016 11:45
Make TinyMCE compatible with Garlic.js in Wordpress
/**
* Add Garlic.js compatability to TinyMCE on front-end
*
* @param array $settings TinyMCE settings array.
* @return array Modified TinyMCE settings array.
*/
function mytheme_tinymce_garlicjs_compat( $settings ) {
if ( ! is_admin() ) {
$settings['setup'] = "function(editor) {
editor.on('change keyup', function(e){
@phlbnks
phlbnks / remove_bws_captcha.php
Created December 6, 2016 10:20
Remove BWS captcha for whitelisted IPs
/**
* Check if a given ip is in a network https://gist.github.com/tott/7684443
* @param string $ip IP to check in IPV4 format eg. 127.0.0.1
* @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed
* @return boolean true if the ip is in this range / false if not.
*/
function ip_in_range( $ip, $range ) {
if ( strpos( $range, '/' ) == false ) {
$range .= '/32';
}
@phlbnks
phlbnks / mod_backgrounds.php
Last active September 7, 2016 19:25
Multiple modulus calculations for looping background colours
<?php
// $posts is some content to loop over and output. Perhaps 28 items.
// We want raindow coloured boxes so loop over 7 possible colours.
$posts_count = 0;
foreach ( $posts as $post ) :
$colour = '';
if ( $posts_count % 7 == 0 ) {
$colour = 'red';
} elseif ( $posts_count % 7 == 1 ) {
$colour = 'orange';
@phlbnks
phlbnks / mod_rows.php
Last active September 7, 2016 19:34
Modulus example to create rows in a grid
<div class="row">
<?php
// $posts is some content to loop over and output.
$posts_count = 0;
foreach ( $posts as $post ) :
echo $post;
if ( $posts_count % 3 == 2 ) : // Fires every 3rd item since we are counting from 0. ?>
</div><div class="row>
<?php endif;