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.

  • Have you enqueued the CSS in frontend? – Kudratullah Dec 1 '19 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 Dec 1 '19 at 9:08
0

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' );
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.