Skip to content

Instantly share code, notes, and snippets.

Phil Banks emirpprime

Block or report user

Report or block emirpprime

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@emirpprime
emirpprime / tinymce_garlicjs_compat.php
Created Dec 7, 2016
Make TinyMCE compatible with Garlic.js in Wordpress
View tinymce_garlicjs_compat.php
/**
* 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){
@emirpprime
emirpprime / cpt_term_list.php
Created Dec 21, 2016
Get a list of terms from a shared taxonomy that are used on a particular post type only
View cpt_term_list.php
<?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 ) ) {
@emirpprime
emirpprime / wp_helper.sh
Last active Aug 5, 2018
Utility script to help manage WordPress sites on an EasyEngine server (but not limited to EE)
View wp_helper.sh
#!/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
}
@emirpprime
emirpprime / time_machine.php
Last active May 20, 2018
Filter the *displayed* time of posts in WordPress to move them into the past/future.
View time_machine.php
/**
* 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 ) {
@emirpprime
emirpprime / time_ago.php
Last active Nov 30, 2018
Filter WordPress date functions to return relative time strings.
View time_ago.php
/**
* 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.
*/
View wp_cli.sh
#!/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
@emirpprime
emirpprime / update_htpasswd.sh
Created Jul 26, 2017
Bash script to help manage .htpasswd files
View update_htpasswd.sh
#!/bin/bash
#
# Manage .htpasswd files
# Store script name for use in output.
me=$( basename $0 )
# Utility function for exiting.
@emirpprime
emirpprime / update_htaccess.php
Last active Jul 26, 2017
Example snippet showing how to update the WordPress .htaccess file programatically
View update_htaccess.php
<?php
/**
* Update .htaccess to whitelist logo when modified in platform settings.
*/
function cc_logo_htaccess() {
// Fire only on Platform Settings options page.
$screen = get_current_screen();
if ( 'cc-configurator_page_cc-settings' === $screen->id && ( $_POST['acf']['field_576c2a5137029'] || $_POST['acf']['field_576c2a9e3702a'] ) ) {
// Store files in an array.
@emirpprime
emirpprime / products_with_category.sql
Last active Nov 20, 2019
Select Product name, SKU, price and category from WordPress / WooCommerce with MySQL query
View products_with_category.sql
SELECT
wp_posts.post_title AS Product,
wp_postmeta1.meta_value AS SKU,
wp_postmeta2.meta_value AS Price,
GROUP_CONCAT( wp_terms.name ORDER BY wp_terms.name SEPARATOR ', ' ) AS ProductCategories
FROM wp_posts
LEFT JOIN wp_postmeta wp_postmeta1
ON wp_postmeta1.post_id = wp_posts.ID
AND wp_postmeta1.meta_key = '_sku'
LEFT JOIN wp_postmeta wp_postmeta2
@emirpprime
emirpprime / exclude_posts_with_term.php
Last active Jan 31, 2018
Exclude posts with a term in a taxonomy from search
View exclude_posts_with_term.php
<?php
/**
* Exclude posts with term 'bananas' from Search.
*/
function cc_search_filter( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_search ) {
$query->set(
'tax_query',
array(
You can’t perform that action at this time.