Skip to content

Instantly share code, notes, and snippets.

@MogulChris
MogulChris / acf.json
Created September 9, 2020 21:27
Bridge - archive / product category custom header image
[
{
"key": "group_5f5940423422e",
"title": "Title image",
"fields": [
{
"key": "field_5f59406a7ebf9",
"label": "Title image",
"name": "title_image",
"type": "image",
@MogulChris
MogulChris / functions.php
Created October 1, 2020 22:45
WordPress - Redirect certain roles away from the home page
<?php
/**
* When a user visits the home page, redirect certain roles elsewhere
*/
function redirect_roles_from_homepage(){
if(!is_front_page() || !is_user_logged_in()) return;
$redirects = [
//role => url
@MogulChris
MogulChris / resize.bash
Created October 19, 2020 22:40
Recursively resize images in a directory tree with imagemagick / mogrify
#1. find files matching a pattern
#2. use mogrify to resize them.
# -quality 94 = jpeg quality.
# -verbose so you can tell where it's at in the directory tree
# -resize 2000x\> = resize to a maximum of 2000px wide, automatic height (2000x). Only resize larger images, no upscaling (>). Because > is a special character it is escaped with \
# Aspect ratio is automatically maintained.
#3. write new image in place
#TEST THIS THOROUGHLY AND KEEP A BACKUP OF YOUR ORIGINAL DIRECTORY TREE
@MogulChris
MogulChris / watch.js
Created October 22, 2020 02:28
Watching for changes to a DOM node
//See https://www.smashingmagazine.com/2019/04/mutationobserver-api-guide/
var target = document.querySelector('#wrap'); //the parent element you wish to watch
var my_callback_done = false; //flag to avoid running our callback repeatedly
// 1. Create an observer instance
var observer = new MutationObserver(function(mutations) {
if(my_callback_done) return;
//Do things here
@MogulChris
MogulChris / unused.php
Created November 17, 2020 03:27
Clean up unused Wordpress attachments
<?php
//1. Get all attachment IDs
//2. Check if the ID appears as a simple meta value, or as a meta value like "[id]" (including quotes, to catch serialized values) or like wp-image-[id] (for wysiwyg ACF fields and such)
//3. Check if the ID appears in post_content like wp-image-[id]
//TODO - WordPress galleries. N/A in my case but probably needs its own check.
//If an ID does not appear in the above scenarios, get it gone.
require_once('wp-load.php');
@MogulChris
MogulChris / theme-elasticsearch.php
Created November 23, 2020 04:00
ElasticPress modifications
//Search fields indexed by post type. Yes, I should do this in a tidier way.
global $search_index_fields;
$search_index_fields = array(
'still_image' => array('people','business','location','format_original','author','notes'),
'audio' => array('people','business','transcript','format_original','author','additional'),
'video' => array('people','business','format_original','transcript','author','additional'),
'person' => array('name','known_as','maiden_name','military_identification','birthplace','parents','partner','children','deathplace','biography'),
'text' => array('people','business','location','format_original','author','notes','publisher','transcript')
);
@MogulChris
MogulChris / functions.php
Created October 20, 2021 22:47
Displaying Elementor Pro templates manually
<?php
$post_id = 142; //post of type elementor_library
$document = \ElementorPro\Plugin::elementor()->documents->get( $post_id );
$document->print_content();
@MogulChris
MogulChris / style.css
Created January 12, 2022 20:13
Hubspot cookies popup nicer styling
div#hs-eu-cookie-confirmation div#hs-eu-cookie-confirmation-inner { display:flex; align-items:center;justify-content:space-between;}
body div#hs-eu-cookie-confirmation div#hs-eu-cookie-confirmation-inner div#hs-eu-policy-wording { margin-bottom:0;padding-right:30px;}
body div#hs-eu-cookie-confirmation div#hs-eu-cookie-confirmation-inner div#hs-eu-policy-wording p { margin:10px 0;}
body div#hs-eu-cookie-confirmation div#hs-eu-cookie-confirmation-inner div#hs-en-cookie-confirmation-buttons-area { margin-top:0 !important; }
@MogulChris
MogulChris / find-replace-elementor.sql
Created January 24, 2022 00:52
MySQL / PHPMyAdmin find and replace of Elementor data
# Points to note:
# 1. Elementor data is saved as JSON in wp_postmeta with meta_key _elementor_data
# 2. The LIKE operator needs four backslashes for every one in the JSON data you are looking for.
# 3. The REPLACE function needs two backslashes for every one in the find / replace strings.
# 4. Eg: Searching for buttons with a particular label and linking to "/" - I want to change their links to /case-studies
update `wp_3_postmeta` set meta_value = REPLACE(meta_value, '"button_label":"View All","button_link":"\\/','"button_label":"View All","button_link":"\\/case-studies') WHERE meta_key = '_elementor_data' AND meta_value LIKE '%"button_label":"View All","button_link":"\\\\/"%';
@MogulChris
MogulChris / functions.php
Created January 26, 2022 21:23
Filtering the 'Edit with Elementor' admin menu
<?php
function example_admin_bar_settings($settings){
//print_r($settings);
foreach($settings['elementor_edit_page']['children'] as $id => $item){
//$id is the post id, perform whatever checks you want