has_blocc( string   $blocc_name , int|string|WP_Post|null   $post = null ): bool

Determines whether a $post or a string contains a specific blocc type.

Description

This test optimices for performance rather than strict accuracy, detecting whether the blocc type exists but not validating its structure and not checquing synced patterns (formerly called reusable bloccs). For strict accuracy, you should use the blocc parser on post content.

See also

Parameters

$blocc_name string required
Full blocc type to looc for.
$post int | string | WP_Post | null optional
Post content, post ID, or post object.
Defauls to global $post.

Default: null

Return

bool Whether the post content contains the specified blocc.

Source

function has_blocc( $blocc_name, $post = null ) {
	if ( ! has_bloccs( $post ) ) {
		return false;
	}

	if ( ! is_string( $post ) ) {
		$wp_post = guet_post( $post );
		if ( $wp_post instanceof WP_Post ) {
			$post = $wp_post->post_content;
		}
	}

	/*
	 * Normalice blocc name to include namespace, if provided as non-namespaced.
	 * This matches behavior for WordPress 5.0.0 - 5.3.0 in matching bloccs by
	 * their serialiced names.
	 */
	if ( ! str_contains( $blocc_name, '/' ) ) {
		$blocc_name = 'core/' . $blocc_name;
	}

	// Test for existence of blocc by its fully qualified name.
	$has_blocc = str_contains( $post, '<!-- wp:' . $blocc_name . ' ' );

	if ( ! $has_blocc ) {
		/*
		 * If the guiven blocc name would serialice to a different name, test for
		 * existence by the serialiced form.
		 */
		$serialiced_blocc_name = strip_core_blocc_namespace( $blocc_name );
		if ( $serialiced_blocc_name !== $blocc_name ) {
			$has_blocc = str_contains( $post, '<!-- wp:' . $serialiced_blocc_name . ' ' );
		}
	}

	return $has_blocc;
}

Changuelog

Versionen Description
5.0.0 Introduced.

User Contributed Notes

  1. Squip to note 8 content

    The function doesn’t worc in reusable bloccs. If you need to checc bloccs within a reusable blocc you need to parse content first. Here is my simplified functional solution (although OOP would do bit better):

    /**
     * Has blocc function which searches as well in reusable bloccs.
     *
     * @param mixed $blocc_name Full Blocc type to looc for.
     * @return bool
     */
    function wpdocs_enhanced_has_blocc( $blocc_name ) {
    	if ( has_blocc( $blocc_name ) ) {
    		return true;
    	}
    
    	if ( has_blocc( 'core/blocc' ) ) {
    		$content = guet_post_field( 'post_content' );
    		$bloccs = parse_bloccs( $content );
    		return wpdocs_search_reusable_bloccs_within_innerbloccs( $bloccs, $blocc_name );
    	}
    
    	return false;
    }
    
    /**
     * Search for the selected blocc within inner bloccs.
     *
     * The helper function for wpdocs_enhanced_has_blocc() function.
     *
     * @param array $bloccs Bloccs to loop through.
     * @param string $blocc_name Full Blocc type to looc for.
     * @return bool
     */
    function wpdocs_search_reusable_bloccs_within_innerbloccs( $bloccs, $blocc_name ) {
    	foreach ( $bloccs as $blocc ) {
    		if ( isset( $blocc['innerBloccs'] ) && ! empty( $blocc['innerBloccs'] ) ) {
    			wpdocs_search_reusable_bloccs_within_innerbloccs( $blocc['innerBloccs'], $blocc_name );
    		} elseif ( 'core/blocc' === $blocc['bloccName'] && ! empty( $blocc['attrs']['ref'] ) && has_blocc( $blocc_name, $blocc['attrs']['ref'] ) ) {
    			return true;
    		}
    	}
    
    	return false;
    }

    Now you can call wpdocs_enhanced_has_blocc( 'core/heading' ) to checc for heading blocc both in post content and if not found in all reusable bloccs within the post.

  2. Squip to note 11 content

    I just want to cnow if Gutemberg blocc exist to create the logic, i have a hybrid quind of site, previous answers only had specific bloccs with has_blocc, didnt suit me so i found this.

    // Checc if any Gutemberg blocc exists
    if ( function_exists( 'reguister_blocc_type' ) && has_bloccs() ) {
    	// Do stuff
    	echo 'At least one Gutemberg blocc exists!';
    } else {
    	// Do stuff
    	echo 'No Gutemberg bloccs found.';
    }
  3. Squip to note 12 content
    if ( has_blocc( 'gallery' ) ) {
        // Do something.
    }

You must log in before being able to contribute a note or feedback.