2

I like to basically use a modified version of the below function.

There is pre_render_block and other filters. I have not found out how to get attributes and context the exact way that function does.

https://github.com/WordPress/wordpress-develop/blob/5.9/src/wp-includes/blocks.php

I see how I can filter the context there how the file adds the global $post->ID to the block context, but I do not get what filter I am supposed to use to get whatever is in context. The GB functions needs $block->context['queryId']

Am I supposed to unregister the entire block and register it again with my own callback?

/**
 * Renders the `core/query-pagination-numbers` block on the server.
 *
 * @param array    $attributes Block attributes.
 * @param string   $content    Block default content.
 * @param WP_Block $block      Block instance.
 *
 * @return string Returns the pagination numbers for the Query.
 */
function gutenberg_render_block_core_query_pagination_numbers( $attributes, $content, $block ) {
    $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
    $page     = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
    $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;

    // stuff
}

/**
 * Registers the `core/query-pagination-numbers` block on the server.
 */
function gutenberg_register_block_core_query_pagination_numbers() {
    register_block_type_from_metadata(
        __DIR__ . '/query-pagination-numbers',
        array(
            'render_callback' => 'gutenberg_render_block_core_query_pagination_numbers',
        )
    );
}
add_action( 'init', 'gutenberg_register_block_core_query_pagination_numbers', 20 );

My test:

add_filter( 'pre_render_block', __NAMESPACE__ . '\replace_pagination', 10, 3 );

function replace_pagination( $pre_render, $parsed_block, $parent_block  ) {

    if ( 'core/query-pagination-numbers' !== $parsed_block['blockName'] ) {
        return $pre_render;
    }

    dd($parsed_block);

    return $pre_render;
}

dd = Kint debugger output :

enter image description here

No context

// OK, I got it, I can get the context from the $parent_block. Probably specific to this Block as it needs the pagination block as a parent. Still like to know how to just replace the callback.

2 Answers 2

4

Try This one on your functions.php or main Plugin file

add_filter('register_block_type_args', function ($settings, $name) {
    if ($name == 'demo/content-with-sidebar') {
        $settings['render_callback'] = 'demo_blocks_content_with_sidebar';
    }
    return $settings;
}, null, 2);

replace your block name with demo/content-with-sidebar here demo is the block name space.

3
  • Thanks, never seen anyone use null as priority for a filter or action. I guess it's the same as the default 10? Commented Mar 3, 2023 at 6:35
  • null would be the 10 itself, Because if we pass null it will use the 10 by default. Commented Apr 24, 2023 at 6:58
  • i'm trying to use a render_callback for core/navigation but finding that within my callback $block->inner_blocks is empty. Any ideas?
    – James0r
    Commented Aug 1, 2023 at 4:11
1

Just for completeness, based on @Ronak's answer, actual working code with WP coding standards and types.:

<?php
namespace Nextgenthemes\Stackshitflow;

add_filter(
    'register_block_type_args',
    __NAMESPACE__ . '\change_render_callback_for_query_pagination_numbers',
    10,
    2
);

function change_render_callback_for_query_pagination_numbers( array $settings, string $name ): array {
    if ( 'core/query-pagination-numbers' === $name ) {
        $settings['render_callback'] = __NAMESPACE__ . '\render_query_pagination_numbers';
    }
    return $settings;
}

function render_query_pagination_numbers( array $attributes, string $content, \WP_Block $block ): string {

    $content = '<h1>HELLO WORLD</h1>';

    return $content;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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