プラグインを作成しましたが、フロント エンドの任意のワードプレス ページからプラグイン ショートコードが埋め込まれたページに送信されるフォームを含むウィジェットを作成したいと考えています。
ウィジェットコード内で、ショートコードを含むページ/投稿またはパーマリンクを検出し、フォームの action="" にする必要があると思います。
誰もこれを行う方法を知っていますか?
見つかったショートコードに基づいて js/css を条件付きでロードするためのこのコードを見つけましたが、ウィジェットでの使用に適応する方法がわかりません。
add_filter('the_posts', 'conditionally_add_scripts_and_styles'); // the_posts gets triggered before wp_head
function conditionally_add_scripts_and_styles($posts){
if (empty($posts)) return $posts;
$shortcode_found = false; // use this flag to see if styles and scripts need to be enqueued
foreach ($posts as $post) {
if (stripos($post->post_content, '[code]') !== false) {
$shortcode_found = true; // bingo!
break;
}
}
if ($shortcode_found) {
// enqueue here
wp_enqueue_style('my-style', '/style.css');
wp_enqueue_script('my-script', '/script.js');
}
return $posts;
}
どんな助けでも大歓迎です。ありがとう、ラス