admin管理员组文章数量:1130349
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 |1 Answer
Reset to default 0I'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?
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
htmldefined? 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$htmlis not known or defined in your filter function. try and change the code tofunction 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
1 Answer
Reset to default 0I'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
版权声明:本文标题:Video, embed, html block usage out of the_content 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1744722099a2112960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


htmldefined? did you mean to check$content? – Michael Commented Feb 22, 2020 at 21:31$content. the variable$htmlis not known or defined in your filter function. try and change the code tofunction swvideo( $html)and see what happens. – Michael Commented Feb 22, 2020 at 23:46function 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