最近、あるユーザーからWordPressのテキストウィジェットでoEmbedを有効化する方法を尋ねられました。 oEmbedとは、動的コンテンツを埋め込むために必要なHTMLを提供するよう、ブログがoEmbed対応サイトに依頼できるプロトコルです。簡単に言うと、ブログ投稿にYouTube動画のURLを貼り付けるだけで、WordPressが自動的に埋め込むことができます。手動でoEmbedの最大幅を調整したり、動的にoEmbedコンテンツの幅と高さを変更することができます。しかし、oEmbedを使ってテキストウィジェットにコンテンツを埋め込むことはできません。この投稿では、WordPress のテキストウィジェットで oEmbed を有効化する方法を紹介します。
すべて、テーマのfunctions.php
ファイルまたはサイト固有のプラグインに、次のコードを追加するだけです:
[cbk1]
これができたら、完了です。これでテキストウィジェットのoEmbedが有効化されました。外観 ” ウィジェットでテストできます。テキストウィジェットにYoutube動画のURLを追加して、マジックを見てください。
このコードが何をするのか知りたい人のために説明すると、単純にwidget_text
フィルターを追加し、テキストウィジェットがoEmbed用のショートコードを実行し、コンテンツを自動埋め込みできるようにします。
コードを扱うのが好きでない人のために、Text Widget oEmbedプラグインを有効化した。それは文字通り同じことを行います。プラグインには、上記で共有した2行のコードしかありません。
この投稿が、テキストウィジェットでoEmbedを有効化するのにお役に立てば幸いです。ご質問やコメントがありましたら、以下にコメントを残してお知らせください。
Syed Balkhi says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Jonathan says
Is this code not enough?
add_filter ('widget_text', array ($ wp_embed, 'autoembed'), 8);
Fernando says
Thanks for the tip but has a problem, that the media content doesn’t adapt to the sidebar width, something that we can control with the embed code and not with the plugin or the code.