0

I have tried hundreds ways but i couldn't do yet. I am beginner, please someone help me !

When admin use for add/upload/embed video any 3 of these blocks; video, core-embed and html i will show the output without use the_content.


function swvideo( $html) {

if ( $html ) {
    return $html;
}

$post = get_post( $post );
if ( ! $html ) {
    return $html;
}

if ( ! function_exists( 'has_blocks' ) ) {
    return $html;
}

if ( ! has_blocks( $post->post_content ) ) {
    return $html;
}

$pattern = "/<!--\ wp:html.*-->([\s\S]*?)<!--\ \/wp:html -->/i";
preg_match_all( $pattern, $post->post_content, $html );
if ( ! empty( $html[1] ) ) {
    $html = reset( $html[1] );
}

return $html;

}

add_filter( 'the_content', 'swvideo');

5
  • where is html defined? did you mean to check $content?
    – Michael
    Feb 22, 2020 at 21:31
  • Isn't HTML Block structure html ? I mean when we use embed block i have used "core-embed/youtube". Than i tought that if i use html block i must use $html. Am i wrong ?
    – Ersin
    Feb 22, 2020 at 21:38
  • you are using a filter, and the only thing that this filter function gets given is, in your case, the variable $content. the variable $html is not known or defined in your filter function. try and change the code to function swvideo( $html) and see what happens.
    – Michael
    Feb 22, 2020 at 23:46
  • I tried it before many times with many variation but nothing happened. function swvideo( $html) { if ( $html ) { return $html; } $post = get_post( $post ); if ( ! $html ) { return $html; } if ( ! function_exists( 'has_blocks' ) ) { return $html; } if ( ! has_blocks( $post->post_content ) ) { return $html; } $pattern = "/<!--\ wp:html.*-->([\s\S]*?)<!--\ \/wp:html -->/i"; preg_match_all( $pattern, $post->post_content, $html ); if ( ! empty( $html[1] ) ) { $html = reset( $html[1] ); } return $html; } add_filter( 'the_content', 'swvideo');
    – Ersin
    Feb 23, 2020 at 0:02
  • And these are in my single.php; ` <?php if(has_block('core/video') || has_block('core-embed/youtube') || has_block('core/html')) { the_content(); } ?> ` but nothing happening. All post content seems. I just want video seems here.
    – Ersin
    Feb 23, 2020 at 0:09

1 Answer 1

0

I've added some comments to clarify a problem with your code:

function swvideo( $html) {

  if ( $html ) { // if $html == true
    return $html;
  }

  $post = get_post( $post ); // irrelevant
  if ( ! $html ) { // if $html != true
    return $html;
  }
  ...

As you can see, nothing past this point will be executed, because the input $html must either be equivalent to true, or not equivalent to true.

I can't tell clearly what you're trying to accomplish, but maybe advanced custom fields would help?

1
  • Thank you for your help, I just want that a video will seems in predefined place. So i thought that there are 3 ways; 1-) video embed block 2-) video block 3-) html block. How can we do that out of the_content ? Or how we can filter the_content and show only video contents in predefined place ?
    – Ersin
    Feb 23, 2020 at 0:47

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.