block editor - Override RenderCallback from LatestPosts

admin2025-04-18  0

I am creating a wordpress website based on materialize css and i am trying to populate latest posts in my home page.

I did setup the Latest Posts in guttenberg editor. To apply the card effect, i need to edit the core files (which is not recommended). So i did change only the render_callback function and everything works fine.

Question: How can i override the render_callback function so that i dont touch the core files

wp-includes\blocks\latest-posts.php

function render_block_core_latest_posts( $attributes ) {
.
.
.

}

function register_block_core_latest_posts() {
    register_block_type(
core/latest-posts',
        array(
    'attributes'      => array(
.
.
.
),
'render_callback' => 'render_block_core_latest_posts', // original core file, commented this line
            'render_callback' => 'render_block_theme_latest_posts', // my edited code
);
}
add_action( 'init', 'register_block_core_latest_posts' );

functions.php

function render_block_theme_latest_posts(){
// My materialize css code for card
return 'hello world';
}

My wordpress version is 5.3

Note here, i dont want to change any functionality. Just the front end css.

I am creating a wordpress website based on materialize css and i am trying to populate latest posts in my home page.

I did setup the Latest Posts in guttenberg editor. To apply the card effect, i need to edit the core files (which is not recommended). So i did change only the render_callback function and everything works fine.

Question: How can i override the render_callback function so that i dont touch the core files

wp-includes\blocks\latest-posts.php

function render_block_core_latest_posts( $attributes ) {
.
.
.

}

function register_block_core_latest_posts() {
    register_block_type(
core/latest-posts',
        array(
    'attributes'      => array(
.
.
.
),
'render_callback' => 'render_block_core_latest_posts', // original core file, commented this line
            'render_callback' => 'render_block_theme_latest_posts', // my edited code
);
}
add_action( 'init', 'register_block_core_latest_posts' );

functions.php

function render_block_theme_latest_posts(){
// My materialize css code for card
return 'hello world';
}

My wordpress version is 5.3

Note here, i dont want to change any functionality. Just the front end css.

Share Improve this question asked Dec 1, 2019 at 8:54 Alaksandar Jesus GeneAlaksandar Jesus Gene 1359 bronze badges 2
  • Have you enqueued the CSS in frontend? – Kudratullah Commented Dec 1, 2019 at 9:06
  • Yes. All done.Its working fine. My doubt is this the only way to edit the core file or i can override. For example, in woocommerce, i can override the templates. – Alaksandar Jesus Gene Commented Dec 1, 2019 at 9:08
Add a comment  | 

1 Answer 1

Reset to default 2

You certainly can do (not recommended). I just dig into the source there's no filter/action hooks used in register_block_type() and it's related classes. The only way (WordPress 5.3) is to redefine the latest post block again within your theme or plugin. The best way will be creating your custom blocks.

// Remove the existing action
remove_action( 'init', 'register_block_core_latest_posts', 10 );
your_render_callback( $attributes ) {...}
function register_block_core_latest_posts_wpse353682() {
    register_block_type(
        'core/latest-posts',
        'attributes' = [...], // copy attributes from the original function...
        'render_callback' => 'your_render_callback',
    );
}
// Re-attach the block
add_action( 'init', 'register_block_core_latest_posts_wpse353682' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744951280a276351.html

最新回复(0)