Skip to content

Instantly share code, notes, and snippets.

@lgedeon
lgedeon / meta-reset.php
Created August 15, 2017 01:01
Delete all post_meta.
<?php
class Meta_Reset {
function __construct() {
add_action( 'parse_request', [ $this, 'parse_request' ] );
}
/**
* Usage: mydomain.com/?meta_reset=1
*/
function parse_request() {
@lgedeon
lgedeon / disable-media.js
Created July 17, 2017 12:43
Experimental code (not yet working) to make items in the media library non-selectable.
//wp.media.view.Modal.prototype: open, ready
( function ($) {
var $attachments = $('.attachments').children(),
blocked = [ 1, 2, 12, 4593 ];
wp.media.events.on( 'open', function( arguments ) {
alert('event fired')
});
@lgedeon
lgedeon / filter.php
Created July 6, 2017 12:06
Another unused method to filter a flat list.
/**
* Get a list of posts that use an attachment that have been published or other status.
*
* @param integer $attachment_id Attachment post id.
* @param array $statuses Array of statuses to check for.
*
* @return string
*/
function get_attachment_usages( $attachment_id, $statuses = array( 'publish' ) ) {
$post_ids = [];
@lgedeon
lgedeon / sort_and_filter.php
Created July 6, 2017 11:08
Shows an interesting approach to converting a flat list into a hierarchy and also grabbing a value based on a max key.
/**
* Get a list of posts that use an attachment.
*
* @param integer $attachment_id Attachment post id.
*
* @return string
*/
function get_attachment_usages( $attachment_id ) {
$posts = [];
$statuses = [];
@lgedeon
lgedeon / customize-notification.php
Created September 1, 2016 17:19
Concept for handling multiple notification templates. We went with a generic template solution, but saving this in case it is needed later.
<?php
/**
* Send a template for handling notifications that other plugins can hook into.
*/
add_action( 'customize_controls_print_footer_scripts', function () {
?>
<script type="text/html" id="tmpl-customize-unified-notifications">
<ul>
<# _.each( data.notifications, function( notification ) { #>
<li class="notice notice-{{ notification.type || 'info' }} {{ data.altNotice ? 'notice-alt' : '' }}" data-code="{{ notification.code }}" data-type="{{ notification.type }}">
@lgedeon
lgedeon / gobutton.php
Last active November 26, 2018 13:13
The Go Button can do anything with a click of a button! Adds a button to the WordPress Dashboard, that fires an action hook "gobutton_clicked". Add code in your theme or plugin to respond to that action and it will fire any time someone clicks.
<?php
/**
Plugin Name: Go Button
Plugin URI: https://gist.github.com/lgedeon/8b3cb0825a0c10b4178374f389d42e71
Description: The Go Button can do anything with a click of a button! Adds a button to the WordPress Dashboard, that fires an action hook "gobutton_clicked". Add code in your theme or plugin to respond to that action and it will fire any time someone clicks.
Version: 0.3
Author: lgedeon
Author URI: https://github.com/lgedeon
License: GPLv2 or later
Text Domain: gobutton
@lgedeon
lgedeon / time the hooks.php
Created March 17, 2016 18:18
Rough code to help find long running processes
<?php
function time_the_hooks( $hook = null ) {
static $time = array( 0, '', array() );
if ( in_array( $hook, array('gettext') ) ) { return; }
ob_clean();
// if ( microtime(true) - $time[0] > 0.0001 ) {
if ( 'get_' === $time[1] ) {
echo (int) ( ( microtime( true ) - $time[0] ) * 100000 );
@lgedeon
lgedeon / widget-title-class.php
Created November 29, 2015 01:06
Target specific widgets by title
<?php
function helper__dynamic_sidebar_params__title_class ( $instance, $widget, $args ) {
if ( isset( $instance['title'] ) ) {
$new_class = implode( '_', array( 'widget', $widget->id_base, sanitize_title( $instance['title'] ) ) ) . " ";
$args['before_widget'] = str_replace( 'class="', 'class="' . $new_class, $args['before_widget'] );
// For lack of a proper filter, let's just do this ourselves - will have to keep up to date with WP, though.
$was_cache_addition_suspended = wp_suspend_cache_addition();
if ( $widget->is_preview() && ! $was_cache_addition_suspended ) {
wp_suspend_cache_addition( true );
@lgedeon
lgedeon / replace-click.js
Created November 24, 2015 02:33
Replace any click event with your own whether it has already been bound or not.
jQuery(document).ready(function ($) {
$('.link').off("click").on("click", function (e) {
// add your click handling here
e.stopPropagation();
return false;
});
});
@lgedeon
lgedeon / filter__acf_update_value.php
Created November 12, 2015 01:38
Sample code for filtering an ACF (Advanced Custom Field plugin) field on update.
<?php
function filter__acf_update_value( $value, $post_id, $field ) {
$post_type = get_post_type( $post_id );
$meta = wp_get_attachment_metadata($value);
if ( intval( $meta['width'] ) < 300 || intval( $meta['height'] ) < 300 ) {
$value = null;
do_action( 'simple_admin_notice', "Selected image (.../{$meta['file']}) was less than 300 x 300. Please select a different image.", 'error' );
}