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?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744722102a4589969.html
html
defined? did you mean to check$content
? – Michael Commented Feb 22, 2020 at 21:31$content
. the variable$html
is 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