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.

1 Answer 1

0

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.

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.