Skip to content

Instantly share code, notes, and snippets.

@lgedeon
lgedeon / wordcount-limit.js
Created October 17, 2019 21:33
Stop publish if too many/few words.
$(document).on('submit', '#post', function() {
const coreWordCounter = new window.wp.utils.WordCounter();
const $mtSeoTitle = $( '#mt_seo_title' );
const mtWordCount = coreWordCounter.count( $mtSeoTitle.val() );
if ( 18 < mtWordCount ) {
alert( 'SEO Error: The title in your SEO Pack cannot exceed 18 words when included in Google News. Please try again.' );
$( this ).find( 'input[type="submit"]' ).removeClass( 'disabled' )
.end().find( '.spinner' ).hide();
return false;
}
@lgedeon
lgedeon / keybase.md
Last active September 13, 2019 14:37

Keybase proof

I hereby claim:

  • I am lgedeon on github.
  • I am lgedeon (https://keybase.io/lgedeon) on keybase.
  • I have a public key ASDrlnygwHYI2ysI1PDA8fRP6b2Izh9-gDZmrKpRyPVNoAo

To claim this, I am signing this object:

@lgedeon
lgedeon / taxonomy_regex_list.php
Created July 11, 2019 18:50
Build a regex pattern to detect taxonomy archive pages
<?php
$taxonomies = get_taxonomies( [], 'objects' );
$rewrites = wp_list_pluck( $taxonomies, 'rewrite' );
$slugs = array_filter( wp_list_pluck( $rewrites, 'slug' ) );
$archive_pattern = sprintf( '~/[%s]/~', implode( '|', $slugs ) );
@lgedeon
lgedeon / save_as_revision.php
Created May 7, 2019 14:49
Save content as the second most recent post revision while preserving the most recent as matching the current content of the post.
<?php
/**
* Save history of content changes to post revision.
*
* @param integer $post_id Post ID.
* @param string $content Content.
*
* @return bool
*/
function save_as_revision( $post_id, $content ) {
@lgedeon
lgedeon / is_cli.php
Created April 29, 2019 15:45
from class i-i3.php
<?php
/**
* Determines whether WP CLI is running.
*
* @return bool True if CLI, false otherwise.
*/
public function is_cli() {
return ( defined( 'WP_CLI' ) && true === WP_CLI );
}
<?php
$keys = array_reduce( $array, function( $carry, $inner_array ) {
if ( is_array( $inner_array ) ) {
return array_merge( $carry, array_keys( $inner_array ) );
}
return $carry;
}, [] );
@lgedeon
lgedeon / class-content-clean-cli.php
Created April 4, 2019 19:43
Discarded code that may someday be relevant.
<?php
/**
* Content Cleanup Filter Commands.
*/
class IOneContentClean extends WPCOM_VIP_CLI_Command {
/**
* Run selected the_content filters and save back to post content.
*
* ## OPTIONS
@lgedeon
lgedeon / csv_media_export.php
Created February 1, 2019 16:11
Export a CSV report of all media in a WordPress Site
<?php
/**
* Media Library Report class
*
* Adds a Media Library Report option to WP Admin / Tools / Export.
*/
class Media_Library_Report {
/**
* Set up all the hooks.
@lgedeon
lgedeon / Site Syncronizer
Created October 4, 2018 20:54
Outline for a WordPress plugin that is able to replicate any content from any site, any time.
Components:
# Change watcher - Hooks into post, meta, menu, option, taxonomy, etc. save events and records the type of content, the id and a timestamp of the most recent change. If the same content is changed again, just update the timestamp.
# Change feed generator - Expose feed of content updates. Include content ID numbers.
# Content feed generator - Expose paginated feeds of all content starting with the oldest content and an end-point listing all content feeds with the number of items in each, so that a site can choose to only consume the last x number of items or can pick up where it left off.
# Site watcher - Consume feed.Record timestamp of last update check. Record origin site's url, content type, content id and the id of where the content was assigned on the local site. Also track highest page number processed so it can start back where it left off.
# Content consumer - Add new content as needed.
# Change consumer - Update individual content items.
# UI - allow selection of content to import. Defaul
<?php
class Singlton {
// Singlton instance.
static $instance = null;
// Do not create multiples.
private function __construct() {
}