0

我已经创建了一个插件,现在想要创建一个包含表单的小部件,该表单将提交到我的插件短代码从前端的任何 wordpress 页面嵌入的页面。

我假设在我的小部件代码中,我需要检测包含短代码的页面/帖子或永久链接,并使我的表单的 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;
}

原始帖子在这里找到:http: //beerpla.net/2010/01/13/wordpress-plugin-development-how-to-include-css-and-javascript-conditionally-and-only-when-needed-by-the -帖子/

任何帮助是极大的赞赏。谢谢,拉斯

4

0 回答 0