wordpress:仅在出现简码的地方加载javascript

问题描述

| 有一个有趣的难题。我需要为我的插件加载大约8个javascript文件和相同数量的样式。仅在运行我的简码的地方才需要这些。 我试图用print_styles和print_scripts加载它们,但是它们不能正确呈现,而且这样做会破坏xhtml验证。因此,目前它们加载在每个页面上,并且由于需要的文件数量而无法像这样保留它。 在另一个项目中,我在插件的index.PHP文件中编写了一个函数,该函数显示当前页面,在其中搜索我的短代码,如果找到,则将打印脚本,但这是一个丑陋的hack。 有人有任何建议或解决方案吗? 任何帮助,将不胜感激, 问候, 大提     

解决方法

        使用简码每页动态加载脚本和样式 优点 每次调用简码时都不会搜索所有帖子。 仅在页面上显示简码时,才可以动态添加样式和脚本。 不要使用正则表达式,因为它们往往比“ 0”或“ 1”慢。如果需要提取args,则应使用上面提到的简码正则表达式。 减少文件通话 代码说明 仅当帖子不是修订版且与指定的
post_type
匹配时,才使用
save_post
钩在页面上查找简码。 除非条目已经存在,否则使用
add_option()
将找到的帖子ID保存为数组,并且将autoload设置为yes。然后它将使用
update_option()
。 使用挂钩
wp_enqueue_scripts
调用我们的
add_scripts_and_styles()
函数。 然后,该函数调用
get_option()
检索我们的页面ID数组。如果当前的
$page_id
$option_id_array
中,那么它将添加脚本和样式。 请注意:我从OOP命名空间类转换了代码,所以我可能错过了一些东西。如果我愿意,请在评论中让我知道。 代码示例:查找简码出现
function find_shortcode_occurences($shortcode,$post_type = \'page\')
{
    $found_ids = array();
    $args         = array(
        \'post_type\'   => $post_type,\'post_status\' => \'publish\',\'posts_per_page\' => -1,);
    $query_result = new WP_Query($args);
    foreach ($query_result->posts as $post) {
        if (false !== strpos($post->post_content,$shortcode)) {
            $found_ids[] = $post->ID;
        }
    }
    return $found_ids;
}

function save_option_shortcode_post_id_array( $post_id ) 
{
    if ( wp_is_post_revision( $post_id ) OR \'page\' != get_post_type( $post_id )) {
        return;
    }
    $option_name = \'yourprefix-yourshortcode\';
    $id_array = find_shortcode_occurences($option_name);
    $autoload = \'yes\';
    if (false == add_option($option_name,$id_array,\'\',$autoload)) update_option($option_name,$id_array);
}

add_action(\'save_post\',\'save_option_shortcode_id_array\' );
代码示例:简码动态包含脚本和样式
function yourshortcode_add_scripts_and_styles() {
    $page_id = get_the_ID();
    $option_id_array = get_option(\'yourprefix-yourshortcode\');
    if (in_array($page_id,$option_id_array)) {
        wp_enqueue_script( $handle,$src,$deps,$ver,$footer = true );
        wp_enqueue_style( $handle,$deps);
    }
}

add_action(\'wp_enqueue_scripts\',\'yourshortcode_add_scripts_and_styles\');
    ,        回答我自己的问题...我是第一次写它。您必须搜索每个页面以检查您的短代码是否正在使用。必须在加载页面数据时并且在显示页面之前执行此操作。对我来说,这是对系统的完全矫kill过正,但不幸的是,它确实是这样。我从以下位置获得此信息: get_shortcode_regex 和 老胡言乱语 所以首先:
add_action(\'template_redirect\',\'wp_my_shortcode_head\');
然后:
function wp_my_shortcode_head(){
  global $posts;
  $pattern = get_shortcode_regex(); 
  preg_match(\'/\'.$pattern.\'/s\',$posts[0]->post_content,$matches); 
  if (is_array($matches) && $matches[2] == \'YOURSHORTCODE\') { 
        //shortcode is being used 
  }
}
将\'YOURSHORTCODE \'替换为您的简码名称,然后将wp_enqueue_scripts添加到使用//简码的位置。     ,        我在这里阅读了一个解决方案:http://scribu.net/wordpress/conditional-script-loading-revisited.html 基本上,如果使用wordpress 3.3,则可以将脚本加入到短代码功能中。
function my_shortcode($atts){
    wp_enqueue_script( \'my-script\',plugins_url( \'plugin_name/js/script.js\' ),array(\'jquery\'),NULL,true);

    // if you add a css it will be added to the footer
//wp_enqueue_style( \'my-css\',plugins_url( \'plugin_name/css/style.css\' ) );

    //the rest of shortcode functionality
}
    ,        只需在这里阅读本教程即可:http://scribu.net/wordpress/optimal-script-loading.html 似乎是最好的方法。
add_action(\'init\',\'register_my_script\');
add_action(\'wp_footer\',\'print_my_script\');

function register_my_script() {
    wp_register_script(\'my-script\',plugins_url(\'my-script.js\',__FILE__),\'1.0\',true);
}

function print_my_script() {
    global $add_my_script;

    if ( ! $add_my_script )
        return;

    wp_print_scripts(\'my-script\');
}
  在这种情况下,仅当$ add_my_script脚本进入队列时,   在页面呈现期间的某个时间点设置了global。
add_shortcode(\'myshortcode\',\'my_shortcode_handler\');

function my_shortcode_handler($atts) {
    global $add_my_script;

    $add_my_script = true;

    // actual shortcode handling here
}
  因此,如果在以下任何位置找到[myshortcode ...],则会添加脚本   当前页面上的帖子。     ,        这些脚本要加载多少页?维护页面数组并仅在当前页面位于数组中时才加载脚本/样式表是否可行? 否则,如果不扫描代码,则无法执行此操作,因为WP甚至在进入页面加载之前都不知道短代码的存在。     ,        BraedenP是正确的,我很确定在wp_enqueue_scripts /加载样式表时,无法检测到短代码的使用。 有什么理由必须在8个文件中执行此操作?只是效率更高,然后将其加载到每个页面上可能不是问题。 您可以考虑只在需要时执行某些样式的PHP样式表解决方案。一个css.php文件可能类似于:
<?php
header(\"content-type: text/css\");
/* You can require the blog header to refer to WP variables and make queries */
//require \'../../../wp-blog-header.php\';
$css = \'\';
$css .= file_get_contents(\'style.css\');
/* Consider using GET variables or querying a variable in the WP database to determine which stylesheets should be loaded. You could add an option to the backend that allows a stylesheet to be turned on or off. */
if($condition1 == TRUE) $css .= file_get_contents(\'condition1.css\');
if($condition2 == TRUE) $css .= file_get_contents(\'condition2.css\');
?>
更少的脚本和更少的样式表意味着更少的http请求和更快的加载时间。     ,如果帖子/页面具有短代码,则加载脚本和样式 最好的解决方案是,仅当当前帖子或页面的内容中包含短代码时,才将文件加载到页面标题中。这正是以下功能的作用:
function flip_register_frontend_assets() 
{
//在这里注册您的脚本和样式
wp_register_style(\'pp_font\',\'plugin_styles.css\',null,\'all\');

global $post;  
//检查您的内容是否包含简码
if(isset($post->post_content) && has_shortcode( $post->post_content,\'your-
shortcode\')){  
//在此处排队您的脚本和样式
 wp_enqueue_style( \'pp_font\');

 }
 }
只需将此功能放在您的一个插件文件中,就可以了。 您将需要用要搜索的短代码替换[your-shortcode],并且还需要用样式表名称替换plugin_styles.css。     ,        您可以仅使用此代码来检查是否在页面内容或侧边栏小部件中实现了短代码。
<?php

if ( shortcode_exists( \'gallery\' ) ) {
    // The [gallery] short code exists.
}

?>