当我们安装主题和插件时,都会向…
通常,最佳做法是仅在需要时才为WordPress主题加载样式和脚本,以保持网站快速加载并避免任何JS错误。为此,有大量的内置条件语句来检查帖子是否使用特定模板,例如主页,搜索页面,管理页面等,因此您只能在页面上加载样式和脚本要求他们,但是,没有任何内置的方法可以测试帖子中的内容是否包含简码。

许多人只是使用wp_enqueue函数将其所有短代码的所有javascript和CSS加载到其标头中,但这并不是最好的方法。在开发站点时,应始终追求速度/性能。
今天,我从PippinsPlugins的好友那里获得了一个非常方便的WordPress函数(该Web站点是插件教程中WordPress插件的最佳网站),它使您可以检查帖子中是否存在某些短代码以及是否确实加载了脚本。
简码检查功能
只需复制和编辑(将YOUR_SHORTCODE更改为要检查的短代码的名称)下面的函数,以测试帖子中是否存在短代码,并在需要时加载脚本/样式!
function check_for_shortcode($posts) { if ( empty($posts) ) return $posts; // false because we have to search through the posts first $found = false; // search through each post foreach ($posts as $post) { // check the post content for the short code if ( stripos($post->post_content, '[YOUR_SHORTCODE') ) // we have found a post with the short code $found = true; // stop the search break; } if ($found){ // $url contains the path to your plugin folder $url = plugin_dir_url( __FILE__ ); wp_enqueue_style( 'my_login_Stylesheet',$url.'plugin_styles.css' ); } return $posts; } // perform the check when the_posts() function is called add_action('the_posts', 'check_for_shortcode');

微信扫描二维码联系我们!
我们在微信上24小时期待你的声音
提供外贸路由器设备产品,轻松翻墙,解答:WP主题推荐,WP网站建设,Google SEO,百度SEO,专业服务器环境搭建等!
需要提供WordPress主题/插件的汉化服务可以随时联系我们!另外成品WordPress网站以及半成品WordPress网站建设,海外Google SEO优化托管服务,百度SEO优化托管服务,Centos/Debian服务器WP专用环境搭建,WP缓存服务器搭建,我们都是你的首选,拥有多年WP开源程序服务经验,我们一直在坚持客户体验,没有最好,只有更好!