Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 September 1, 2020 04:49
Stop WooCommerce ruining your formatting with paragraph p tags wpautop
<?php
//Wonky formatting on WooCommerce pages and only on WooCommerce pages? P tags where you don't want P tags?
function remove_autop_for_wc(){
remove_filter('woocommerce_short_description','wpautop');
}
add_action( 'init', 'remove_autop_for_wc', 999999 );
?>
@MogulChris
MogulChris / functions.php
Created August 26, 2020 23:52
WP Bakery / Visual Composer custom css on WooCommerce shop page
<?php
// The issue:
// You want to use WP Bakery for your WooCommerce shop page.
// You can enable WP Bakery for that page, but custom Design Options are not showing.
// The explanation:
// WP Bakery keeps those custom design changes as a CSS string, stored as post meta on the page you are using for your shop page.
// Unfortunately that CSS is not being output by your theme, hence your custom design changes are not working.
@MogulChris
MogulChris / smoothscroll.js
Created August 19, 2020 02:53
Another smooth scroll approach
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
offset = 100; //px, enough to clear the sticky nav menu
$('html,body').animate({ scrollTop: target.offset().top - offset }, 600);
return false;
}
}