Processing oEmbed for Arbitrary Text

If we need to process the shortcode [embed] or auto-embedding a link in the text, then this text will need to be processed separately. Basic processing using the do_shortcodes() or apply_shortcodes() functions does not include oEmbed. By default, such processing is only done for the the_content hook.

So, we have 2 options:

Option 1

Simple, but may not be suitable due to the excessive load on the the_content hook - it usually has a bunch of other things that may be unnecessary.

$text = '
Some text to check custom shortcode adding.

[embed]https://my-youtube.com/watch?v=lWzMBLoLIAc[/embed]

https://my-youtube.com/watch?v=uDQwKtkXV-0
';

$text = apply_filters( 'the_content', $text );

echo $text;

Option 2: Point Approach

We only do what we need with the text:

$text = '
Some text to check custom shortcode adding.

[embed]https://my-youtube.com/watch?v=lWzMBLoLIAc[/embed]

https://my-youtube.com/watch?v=uDQwKtkXV-0
';

$text = $GLOBALS['wp_embed']->run_shortcode( $text ); //  shortcode
$text = $GLOBALS['wp_embed']->autoembed( $text );     // oEmbed URLs

//$text = apply_shortcodes( $text );

$text = wpautop( $text );

echo $text;

This Note embeded into: oEmbed in WordPress