Add to e.g. functions.php function has_h1_heading( $blocks = array() ): bool { if ( empty( $blocks ) ) { global $post; $blocks = parse_blocks( $post->post_content ); } foreach ( $blocks as $block ) { if ( $block[‘blockName’] == ‘core/heading’ && $block[‘attrs’][‘level’] == 1 ) { return true; } // ooh recursion if ( isset( $block[‘innerBlocks’] […]
Archives: Snippets
Lazy Load YouTube Embed Gutenberg Block
Save as e.g. youtube_facade.php in the WordPress plugins directory and activate. <?php /** Plugin Name: Lazy Load YouTube Embed Block Plugin URI: Description: Filters the gutenberg core/embed youtube block content to load a facade until the block is clicked Version: 1.0 Author: Jem Turner Author URI: https://jemturner.co.uk **/ add_filter( ‘render_block_core/embed’, function( $block_content, $block ) { […]
Get first image from Gutenberg-enabled WordPress post
Place in theme functions.php or similar function get_first_image( $post_content, $size = ‘thumbnail’ ) { preg_match( “/wp:image {.*?\”id\”:(\d+),/is”, $post_content, $matches ); if ( !empty( $matches[1] ) ) { return wp_get_attachment_image( (int)$matches[1], $size ); } return ”; } Can be used as a fallback for e.g. if the post featured image is not set, e.g. $image = […]
Remove time select in WooCommerce Local Pickup Time Select when basket contains only virtual products or shipping is selected
Place in theme functions.php or similar function jt_cart_has_physical_items() { $has_physical = false; foreach ( WC()->cart->get_cart() as $cart_item ) { if ( !$cart_item[‘data’]->is_virtual( ) ) { $has_physical = true; break; // no point continuing once we’ve found one physical item } } return $has_physical; } function jt_customer_picked_shipping() { $has_shipping = false; $chosen_methods = WC()->session->get( ‘chosen_shipping_methods’ ); […]
WordPress Multisite Cron Replacement
Based on https://sabrinazeidan.com/fixing-wordpress-multisite-cron/ <?php if ( !defined( ‘ABSPATH’ ) ) { require_once __DIR__ . ‘/wp-load.php’; } if ( is_multisite() ) { $sites = get_sites(); foreach ( $sites as $site ) { wp_remote_get( get_site_url() . $site->path . ‘wp-cron.php?doing_wp_cron’ ); sleep( 3 ); } }
Basic No JavaScript Honeypot for WordPress Comments
if ( !class_exists( ‘jemsCommentHoneypot’ ) ) { class jemsCommentHoneypot { function __construct() { add_filter( ‘comment_form_default_fields’, array( $this, ‘add_honeypot_field_to_form’ ) ); add_filter( ‘preprocess_comment’, array( $this, ‘process_honeypot_field’ ), 1 ); } /* * adds our honeypot to the comment field defaults, wrapped in noscript; the likelihood of a genuine commenter * seeing this these days is slim […]
Make Gutenberg Editor Full Width
/* make gutenberg editor full width */ function editor_full_width_gutenberg() { echo ‘<style> body.gutenberg-editor-page .editor-post-title__block, body.gutenberg-editor-page .editor-default-block-appender, body.gutenberg-editor-page .editor-block-list__block { max-width: none !important; } .block-editor__container .wp-block { max-width: none !important; } </style>’; } add_action( ‘admin_head’, ‘editor_full_width_gutenberg’ );
WordPress Custom General Options
class jtThemeAdmin { public $settings = array( array( ‘name’ => ‘jt-twitter’, ‘label’ => ‘Twitter URL’ ), array( ‘name’ => ‘jt-linkedin’, ‘label’ => ‘Linkedin URL’ ) ); public function __construct() { add_action( ‘admin_init’, array( $this, ‘register_custom_settings’ ) ); add_action( ‘admin_init’, array( $this, ‘register_setting_fields’ ) ); } public function register_custom_settings( ) { foreach( $this->settings as $setting ) […]
Remove jQuery migrate if no dependencies found
function remove_jquery_migrate( $scripts ) { if ( !is_admin() && isset( $scripts->registered[‘jquery’] ) ) { $script = $scripts->registered[‘jquery’]; if ( $script->deps ) { $script->deps = array_diff( $script->deps, array( ‘jquery-migrate’ ) ); } } } add_action( ‘wp_default_scripts’, ‘remove_jquery_migrate’ );
Page # of X pagination
<div class=”pagination”> <?php previous_posts_link( ‘< Previous Page’ ); ?> <span>Page <?php echo ( get_query_var(‘paged’) ) ? get_query_var( ‘paged’ ) : 1; ?> of <?php echo ( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; ?></span> <?php next_posts_link( ‘Next Page >’, ” ); ?> </div>