Skip to content

Instantly share code, notes, and snippets.

@lgedeon
lgedeon / syntax_safe_include.php
Last active October 11, 2015 03:10
You can't try/catch a syntax error. You should never have a syntax error. But if you are writing a framework and suggesting that inexperienced users make minor modifications (power to the people!), it would be nice of you to provide a bit of a safety net. Here's how.
<?php
function helper_syntax_safe_include ( $file, $reset = false ) {
$key = sanitize_key( $file );
if ( file_exists( $file) ) {
// found it, no more hunting
} elseif ( file_exists( trailingslashit( __DIR__ ) . ltrim( $file, '/' ) ) ) {
$file = trailingslashit( __DIR__ ) . ltrim( $file, '/' );
} elseif ( file_exists( trailingslashit( get_template_directory() ) . ltrim( $file, '/' ) ) ) {
$file = trailingslashit( get_template_directory() ) . ltrim( $file, '/' );
@lgedeon
lgedeon / better-broken-class.php
Created September 30, 2015 01:16
Replace a method in a global object. Not working yet. Just a concept that I discovered I didn't actually need.
class Better_Broken_Class extends Broken_Class {
private $parent = null;
function __construct ( $parent ) {
$this->parent = $parent;
}
function __call ( $name, $arguments ) {
if ( 'function_to_replace' === $name ) {
// do other stuff and return
@lgedeon
lgedeon / find-all-short-codes.php
Created September 18, 2015 17:52
Find all shortcodes and few other things that look like shortcodes
function fasc_find_shortcode() {
if ( ! isset( $_GET['findshortcode'] ) ) {
return;
}
global $wpdb;
$rows = $wpdb->get_results("SELECT ID, post_content FROM {$wpdb->posts} WHERE post_content LIKE '%[%'", ARRAY_N);
@lgedeon
lgedeon / remove_filter_in_class.php
Last active August 29, 2015 14:27
Setup correctly, an action hook or filter defined in a class is as easy to remove as any other hook. But if not setup correctly and not your code, try this.
<?php
function remove_filter_in_class( $tag, $function_to_remove, $priority, $class ) {
if ( isset( $GLOBALS['wp_filter'][ $tag ][ $priority ] ) ) {
foreach ( $GLOBALS['wp_filter'][ $tag ][ $priority ] as $key => $function ) {
if ( is_array( $function['function'] )
&& $function_to_remove === array_pop( $function['function'] )
&& $class === get_class( array_pop( $function['function'] ) )
) {
return remove_filter( $tag, $key, $priority );
}
@lgedeon
lgedeon / gist:bafe380038e5573e2c36
Last active August 29, 2015 14:24
Quickly kill all spam and pending comments in WordPress (caveat emptor)
function kill_pending_and_spam() {
$currentScreen = get_current_screen();
if ( 'edit-comments' == $currentScreen->id ) {
global $wpdb;
$query = $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_approved = 0", '' );
$wpdb->query( $query );
$query = $wpdb->prepare( "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam'", '' );
$wpdb->query( $query );
@lgedeon
lgedeon / cpt-template
Created November 15, 2014 03:13
Custom Post Type Template
<?php
namespace My\Name\Space;
function action__init() {
$namespace = "translation-namespace";
$single = __( 'Custom Post Type', $namespace );
$plural = __( 'Custom Post Types', $namespace );
register_post_type( 'namespace_custom_post_type',
array(
@lgedeon
lgedeon / taxonomy-template
Last active August 29, 2015 14:09
Taxonomy Template
<?php
namespace My\Name\Space;
function action__init() {
$namespace = "translation-namespace";
$single = __( 'Tag', $namespace );
$plural = __( 'Tags', $namespace );
register_taxonomy( 'name-space-tag', array( 'post', 'page' ), array(
'labels' => array(
@lgedeon
lgedeon / gist:39d29dde00a11e3c2a0e
Created November 11, 2014 01:05
link two custom post types together using a taxonomy as a go between
/*===========================================================================
LINK EVENT LOCATION TAXONOMY AND POST TYPE
===========================================================================*/
function action__transition_post_status( $new_status, $old_status, $post ) {
// make sure we actually have an event object
if ( null === ( $post = get_post( $post ) ) || 'event_loc' !== $post->post_type ) {
return;
@lgedeon
lgedeon / pure js only toggle
Created May 7, 2014 14:23
toggle class by id on div without any other classes (maximum compatibility limited use case)
function namespace_divname_toggle(){
this.className=("active"==this.className.substr(0, 6))?"":"active";
}
document.querySelector('#divname').addEventListener('click', namespace_divname_toggle )
@lgedeon
lgedeon / wp_remote_post files
Created December 20, 2013 16:22
wip maybe
$response = wp_remote_post(
"https://api.freewheel.tv/services/upload/bvi.xml",
array(
'method' => 'POST',
'timeout' => 30,
'redirection' => 5,
'httpversion' => '1.0',
'headers' => array(
'X-FreeWheelToken' => 'MYAPIKEY',
'Expect' => ????