• Hello, I have been trying to apply an apply_filter like this for hours

    echo apply_filters( 'render_block', $blockContent, $block );

    ////////////////////////////////////////////////// / //////////////////

    add_filter('render_block', 'display_post_heading', 10, 2);
    show_post_header() function {
    global post $;
    $blocks = parse_blocks ($post->post_content);
    foreach ($blocks as $block) {
    if( 'core/heading' === $block['blockName'] ) {
    echo render_block($block);
    break;
    }
    }

    can someone guide me on the path of the light of worpress

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator t-p

    (@t-p)

    Moderator bcworkz

    (@bcworkz)

    It’s not clear to me what you want to accomplish. You would normally use apply_filters() where you want to allow others to modify something your code is doing without having to directly modify your code. render_block() already calls apply_filters() for “render_block”, so why would you also do so?

    If you want to modify how all blocks are rendered, you can add to the “render_block” filter to do so. It doesn’t make sense to apply_filters() for that. If you only wish to target a specific block, the “render_block_{$this->name}” filter would make more sense.
    https://developer.www.ads-software.com/reference/hooks/render_block_this-name/

    Thread Starter kmytort

    (@kmytort)

    and the {$this->name}” what is the name? the name of the osae filter would be like this? My problem is that I don’t understand how to put the apply_filter()

    echo apply_filters( "render_block_{$this->display_post_heading}", $blockContent, $block );
    can you guide me please, because nothing works for me

    Moderator bcworkz

    (@bcworkz)

    I could advise better if I understood what you wish to accomplish. Without such understanding, I suspect apply_filters() is unlikely to accomplish what you want. I’ve been modifying what WP does for many years and have never used it. OTOH, add_filter() is very frequently used.

    More on filter use is described in the Plugin Handbook.

    To use "render_block_{$this->name}", replace {$this->name} with the block object’s name property where you wish to filter its rendered content. Thus you only filter a specific block instead of all blocks.

    Thread Starter kmytort

    (@kmytort)

    Hello, thanks for answering, what I want is to take the paragraph or the columns or any block of the core in the_contet(). and so separate them or take these blocks in another html tag, I don’t want to customize them I just want to take the paragraph and put it somewhere else and<h1> <?php echo the_title()?></h1>, and that’s how I call the paragraph but I don’t know how to do it apply_filters( 'render_block', $blockContent, $block );
    you can help?

    Thread Starter kmytort

    (@kmytort)

    ///page.php ///
    <div>
    <?php
    //echo ‘

    • ‘ . $blockContent . ‘
    • ‘;
      apply_filters( ‘render_block’, $blockContent, $block );
      ?>
      <?php //echo display_post_paragraph();?>
      </div>

      ///functions.php ///

      function post_paragraph() {
       global $post;
      	$blocks = parse_blocks( $post->post_content );
      	foreach( $blocks as $block ) {
      		if( 'core/paragraph' === $block['blockName'] ) {
            echo render_block( $block);
            break;
          }
        }
      }
    Thread Starter kmytort

    (@kmytort)

    it’s supposed to work like this apply_filters( "render_block_{$this->post_paragraph}", $blockContent, $block );

    Moderator bcworkz

    (@bcworkz)

    If you want to move blocks about in content, “render_block” will not help because it only operates on one block at a time and has no knowledge of surrounding context.

    You could use “the_content” filter to do so though. In your filter callback use preg_replace() to find and extract the targeted blocks, or explode() the entire content into smaller elements. Insert extracted blocks elsewhere or rearrange as desired. Return the modified content.

    Add your callback to “the_content” filter with add_filter(). No need to use apply_filter() at all. Do use a larger $priority arg value to give other callbacks a chance to modify content before your code has a go at it. WP will call apply_filters() at the appropriate time, which will cause your added callback to execute.

    Thread Starter kmytort

    (@kmytort)

    Hello, thank you very much for your help, I managed to make this variable, but I only printed the text, my question is if I do it with a gallery?

    $ content = get_the_content () ; 
                    if (preg_match ('~ <p class = "info-text-block"> ([\ s \ S] +?) </p> ~ ', $ content, $ matches)) 
                echo '<P>' . $ matches [1] . '</p>' ;
    • This reply was modified 2 years, 9 months ago by bcworkz. Reason: code format fixed
    Thread Starter kmytort

    (@kmytort)

    Hello, again me, now I have the same problem but with the media-text block .

    As I can, call only the middle block – text ?

    Moderator bcworkz

    (@bcworkz)

    Getting the regexp right can be tricky. Try one of regexp “fiddle” sites like regexr.com. Paste in your content as a sample, then experiment with the regexp until it captures what you want and not what you don’t want.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘core blocks’ is closed to new replies.