Ich möchte einen id="myid"
zum eingebetteten iframe-Shortcode hinzufügen: -
$video_url = get_post_meta($post_id, 'video_url',true);
//or
$video_url .= 'video url';
$check_embeds=$GLOBALS['wp_embed']->run_shortcode( '[embed]'. $video_url .'[/embed]' );
echo $check_embeds;
Dieser Code hilft mir beim Anzeigen von Videos über eine benutzerdefinierte Meta-Box unter Verwendung einer Video-URL. Und zusätzlich möchte ich hier eine ID zu iframe hinzufügen, zum Beispiel: - <iframe id="myid" src=""></iframe>
so. Kann mir jemand helfen, das Problem zu beheben?
1.- Fügen Sie dies zur Funktionsdatei Ihres untergeordneten Themas hinzu:
add_filter("embed_oembed_html", function( $html, $url, $attr ) {
if ( !empty( $attr['id'] ) ) {
$html = str_replace( "<iframe", sprintf( '<iframe id="%s"', $attr['id'] ), $html );
}
return $html;
}, 10, 3);
2.- Verwenden Sie nun [embed id="myid"]
, indem Sie das ID-Attribut im Shortcode anwenden.
$check_embeds=$GLOBALS['wp_embed']->run_shortcode(
'[embed id="myid"]'. $video_url .'[/embed]'
);
Hoffentlich hilft das.
Da Sie über die Verwendung eines <iframe>
zum Einbetten der Video-URL sprechen, habe ich mich für einen anderen Ansatz entschieden, anstatt den WordPress-Shortcode ' [embed]
zu verwenden, und meinen eigenen [iframe]
-Shortcode erstellt (fügen Sie diesen Ihrem functions.php
hinzu):
add_shortcode( 'iframe', 'wpse_237365_iframe_shortcode' );
function wpse_237365_iframe_shortcode( $iframe ) {
$tags = array( // Default values for some tags
'width' => '100%',
'height' => '450',
'frameborder' => '0'
);
foreach ( $tags as $default => $value ) { // Add new tags in array if they don't exist
if ( [email protected]_key_exists( $default, $iframe ) ) {
$iframe[$default] = $value;
}
}
$html = '<iframe';
foreach( $iframe as $attr => $value ) { // Insert tags and default values if none were set
if ( $value != '' ) {
$html .= ' ' . esc_attr( $attr ) . '="' . esc_attr( $value ) . '"';
}
}
$html .= '></iframe>';
return $html;
}
Mit dem oben angegebenen Code können Sie nicht nur Ihr id=
-Attribut hinzufügen, sondern auch alle anderen Attribute, die Sie hinzufügen möchten, wodurch Sie wesentlich flexibler werden.
Shortcode Beispiel 1
[iframe src="https://link.to/video" id="my-id-here" width="100%" height="500"]
Werde dir geben:
<iframe src="https://link.to/video" id="my-id-here" width="100%" height="500" frameborder="0"></iframe>
Shortcode Beispiel 2
Sie können bei Bedarf sogar Attribute erstellen:
[iframe src="https://link.to/video" scrolling="yes" custom-tag="test"]
Werde dir geben:
<iframe src="https://link.to/video" scrolling="yes" custom-tag="test" width="100%" height="450" frameborder="0"></iframe>