Home Forums Gutenberg Different render_callback while editing post


Different render_callback while editing post

  • My project involves pages with a lot of post object relational fields, where a posts post_thumbnail and title are displayed in a grid.

    While previewing the page or generally on the frontend, the entire block element will be linked to the post.

    While editing in Gutenberg with preview = true, I’d like to not render the a href around the div that makes up the block, since it makes selecting any block almost impossible.

    I’ve tried adding

    $screen = get_current_screen();
    if ( $screen->parent_base != 'edit' ) {

    but Gutenberg doesn’t like it and stops saving the post with
    Failed to load resource: the server responded with a status of 500 () error.

    Is there an official way to have a different render_callback depending on whether we’re editing or previewing/viewing a post?

  • I used if(is_admin()) check in the block-render.php page to split code into frontend / backend output.

    Is there a better option?

  • Still haven’t had much luck with this so far.
    Conditionals using (id_admin()) or `$current_screen = get_current_screen();
    if ( method_exists( $current_screen, ‘is_block_editor’ ) && $current_screen->is_block_editor() ) {` just made the block editor stop being able to save the page.

  • You can also give the element a css rule in your custom editor css file:
    .yourelement { pointer-events: none; }
    Then the element is not clickable.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Different render_callback while editing post’ is closed to new replies.