Skip to content

Instantly share code, notes, and snippets.

@CatEntangler
CatEntangler / CustomWordPressCrons.php
Created March 12, 2018 19:12
Sometimes you need more than hourly WordPress events.
<?php
/*
* Not only can you reuse this throughout your application for added flexibility but
* it gives you added flexibility for scheduling your stuff.
* The default WordPress cron intervals are, as of this writing:
* - Hourly
* - Twice Daily
* - Daily
*/
@CatEntangler
CatEntangler / GetAttachedMediaMinusFeaturedImage.php
Created March 5, 2018 22:27
Get the attached media to a post minus the featured image
function GetAttachedMediaMinusFeaturedImage( INT $postId ) {
$featuredImage = get_post_thumbnail_id( $postId );
$attached_media = get_attached_media( NULL, $postId );
unset( $attached_media[ $featuredImage ] );
return $attached_media;
}
function GetSentenceList( ARRAY $things, STRING $operator = "and", BOOL $oxfordComma = TRUE ) {
if( empty( $things ) ) {
return FALSE;
}
$oxford = ( $oxfordComma )
? ', '
: ' ';
@CatEntangler
CatEntangler / _frontlog.php
Created February 26, 2018 03:08
WordPress dashboard front-end logging
/*
* This usually goes in an mu-plugins file but for now here it is on its own
* This will pretty print anything to the screen (so you can safely have WP_DEBUG_DISPLAY FALSE
* The styles are specific to the default dashboard layouts
*/
if( ! function_exists( '_frontlog' ) ) {
function _frontlog( $message ) {
echo "<pre style='background-color:white;margin-left:159px;border:1px solid black;padding:15px;'>";
@CatEntangler
CatEntangler / _log.php
Created February 26, 2018 03:06
Log anything to error log with pretty printing
if( ! function_exists('_log') ) {
/**
* @description Takes any message and sends it to the error_log. Due to how php's serialize works (will add binary characters) and that error_log will truncate when it hits a binary character a str_replace is used.
*
* @param $message mixed anything you want to print out
*/
function _log( $message ) {
error_log( str_replace( "\0", "[NULL]", print_r( $message, TRUE ) ) );
@CatEntangler
CatEntangler / elasticpress_posttype_weighting.php
Last active December 2, 2020 16:40
weighting by post_type using ElasticPress
function SetSearchArgs( $formattedArgs, $args = [] ) {
if( isset( $args[ 'post_type' ] ) && ( count( $args[ 'post_type' ] ) > 1 || gettype( $args[ 'post_type' ] ) === 'string' ) ) {
if( gettype( $args[ 'post_type' ] ) === 'array' ) {
$existing_query = $formattedArgs[ 'query' ];
unset( $formattedArgs[ 'query' ] );
SELECT main.lead_id
FROM wp_rg_lead_detail as main
WHERE main.form_id = 1
AND main.lead_id NOT IN (
SELECT wp_rg_lead_detail.lead_id
FROM wp_rg_lead_detail
WHERE wp_rg_lead_detail.form_id = 1
AND wp_rg_lead_detail.field_number = 102
AND wp_rg_lead_detail.value <> ''
)
@CatEntangler
CatEntangler / post-category-results.sql
Created January 19, 2018 14:24
WordPress: Return category result count from product post search
/*
This query looks into post fields and returns an ordered list of results based on the attached categories to the posts searched.
This can be used to help weight results or to potentially show links to categories instead of the posts themselves.
Can be modified to add additional filters. Still not sure how to tie this into pre-get-posts but working on it.
+------------+---------+--------------+
| name | term_id | result_count |
+------------+---------+--------------+
| Category A | 15 | 12 |
| Category C | 17 | 12 |
@CatEntangler
CatEntangler / anonymize_wp_useremails.sql
Last active December 2, 2020 16:39
Anonymize wordpress user email addresses for cloning purposes. Exclude specific domain names to keep admin/selected users intact. Prefix so that addresses are still unique.
SET @domains='domain1.com|domain2.com';
UPDATE wp_users prod_users,
( select ID FROM wp_users WHERE user_email NOT REGEXP @domains) clone_users
SET prod_users.user_email = CONCAT('DC_', prod_users.ID, '@example.org')
WHERE prod_users.user_email NOT REGEXP @domains;

Keybase proof

I hereby claim:

  • I am majemedia on github.
  • I am majemedia (https://keybase.io/majemedia) on keybase.
  • I have a public key whose fingerprint is 902E 807B BAB1 2597 F0DC 4C52 8FBE 28CE 868F 7531

To claim this, I am signing this object: