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'] ) && !empty( $block['innerBlocks'] ) ) {
if ( has_h1_heading( $block['innerBlocks'] ) ) {
return true;
}
}
}
return false;
}
then call in a template as follows (e.g.):
<?php if ( has_h1_heading() === false ) : ?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<?php endif; ?>
—
Gaz correctly pointed out just checking the markup would be a lot faster than running through parse_blocks(); swap out the function above if speed is a concern:
function has_h1_heading( $post_content = '' ): bool {
if ( empty( $post_content ) ) {
global $post;
$post_content = $post->post_content;
}
if ( preg_match( '/wp:heading {.*?"level":1.*?}/i', $post_content ) ) {
return true;
}
return false;
}
Use as above.
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 ) {… read full entry »
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 =… read full entry »
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’ );… read full entry »
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 ); } }
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… read full entry »
/* 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’ );
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 )… read full entry »
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’ );
<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>