admin管理员组

文章数量:1432001

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');

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');

Share Improve this question edited Feb 23, 2020 at 0:05 Ersin asked Feb 22, 2020 at 19:21 ErsinErsin 31 silver badge3 bronze badges 5
  • where is html defined? did you mean to check $content? – Michael Commented 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 Commented 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 Commented 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 Commented 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 Commented Feb 23, 2020 at 0:09
Add a comment  | 

1 Answer 1

Reset to default 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?

本文标签: videoembedhtml block usage out of thecontent