get_attribute example


function render_block_core_image( $attributes$content ) {

    $processor = new WP_HTML_Tag_Processor( $content );
    $processor->next_tag( 'img' );

    if ( $processor->get_attribute( 'src' ) === null ) {
        return '';
    }

    if ( isset( $attributes['data-id'] ) ) {
        // Add the data-id="$id" attribute to the img element         // to provide backwards compatibility for the Gallery Block,         // which now wraps Image Blocks within innerBlocks.         // The data-id attribute is added in a core/gallery `render_block_data` hook.         $processor->set_attribute( 'data-id', $attributes['data-id'] );
    }

    
/** * The first chunk of innerContent contains the block markup up until the inner blocks start. * This targets the opening tag of the inner blocks wrapper, which is the last tag in that chunk. */
    $inner_content_classnames = '';

    if ( isset( $block['innerContent'][0] ) && 'string' === gettype( $block['innerContent'][0] ) && count( $block['innerContent'] ) > 1 ) {
        $tags            = new WP_HTML_Tag_Processor( $block['innerContent'][0] );
        $last_classnames = '';
        while ( $tags->next_tag() ) {
            $last_classnames = $tags->get_attribute( 'class' );
        }

        $inner_content_classnames = (string) $last_classnames;
    }

    $content = $content_with_outer_classnames ? new WP_HTML_Tag_Processor( $content_with_outer_classnames ) : new WP_HTML_Tag_Processor( $block_content );

    if ( $inner_content_classnames ) {
        $content->next_tag( array( 'class_name' => $inner_content_classnames ) );
        foreach ( $class_names as $class_name ) {
            $content->add_class( $class_name );
        }
if ( in_the_loop() ) {
            update_post_thumbnail_cache();
        }
        $current_featured_image = get_the_post_thumbnail_url();
        if ( ! $current_featured_image ) {
            return $content;
        }

        $processor = new WP_HTML_Tag_Processor( $content );
        $processor->next_tag();

        $styles         = $processor->get_attribute( 'style' );
        $merged_styles  = ! empty( $styles ) ? $styles . ';' : '';
        $merged_styles .= 'background-image:url(' . esc_url( $current_featured_image ) . ');';

        $processor->set_attribute( 'style', $merged_styles );
        $content = $processor->get_updated_html();
    }

    return $content;
}

/** * Registers the `core/cover` block renderer on server. */
if ( ! $attributes['isLink'] ) {
        // Remove the link.         $custom_logo = preg_replace( '#<a.*?>(.*?)</a>#i', '\1', $custom_logo );
    }

    if ( $attributes['isLink'] && '_blank' === $attributes['linkTarget'] ) {
        // Add the link target after the rel="home".         // Add an aria-label for informing that the page opens in a new tab.         $processor = new WP_HTML_Tag_Processor( $custom_logo );
        $processor->next_tag( 'a' );
        if ( 'home' === $processor->get_attribute( 'rel' ) ) {
            $processor->set_attribute( 'aria-label', __( '(Home link, opens in a new tab)' ) );
            $processor->set_attribute( 'target', $attributes['linkTarget'] );
        }
        $custom_logo = $processor->get_updated_html();
    }

    $classnames = array();
    if ( empty( $attributes['width'] ) ) {
        $classnames[] = 'is-default-size';
    }

    
Home | Imprint | This part of the site doesn't use cookies.