wp query - Override WordPress core post-template.php block template

admin2025-01-07  3

I need to modify the default markup generated using the render_block_core_post_template function located inside wp-includes > blocks > post-template.php.

I don't see any other way of modifying the markup so i can insert a custom field.

Is it possible to override this core template via a theme or plugin? If so, where to start.

function register_block_core_post_template() {
    register_block_type_from_metadata(
        __DIR__ . '/post-template',
        array(
            'render_callback'   => 'render_block_core_post_template',
            'skip_inner_blocks' => true,
        )
    );
}
add_action( 'init', 'register_block_core_post_template' );

I need to modify the default markup generated using the render_block_core_post_template function located inside wp-includes > blocks > post-template.php.

I don't see any other way of modifying the markup so i can insert a custom field.

Is it possible to override this core template via a theme or plugin? If so, where to start.

function register_block_core_post_template() {
    register_block_type_from_metadata(
        __DIR__ . '/post-template',
        array(
            'render_callback'   => 'render_block_core_post_template',
            'skip_inner_blocks' => true,
        )
    );
}
add_action( 'init', 'register_block_core_post_template' );
Share Improve this question asked Apr 18, 2022 at 16:57 Brad DaltonBrad Dalton 6,9672 gold badges36 silver badges47 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To insert a custom field:

I recommend using the Meta Field Block plugin developed just for this purpose

To alter core/post-template's DOM with PHP

Add a filter to render_block action e.g. in functions.php

// Replace post-template block with custom DOM
add_filter( 'render_block', 'MyPostTemplate' , 10, 2 );

function MyPostTemplate( $block_content, $block )
{
    // bypass if other block
    if ( 'core/post-template' !== $block['blockName'] ) return $block_content;

    // do something with $block_content (here we prepend a paragraph to each listed entry)
    $block_content=str_replace("<li ", "<p>Hello world!</p><li ", $block_content);

    return $block_content;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252600a58.html

最新回复(0)