douerlin4366 2018-06-02 16:40
浏览 60
已采纳

Wordpress使用本地回退加载外部脚本

I can't figure out what I'm doing wrong! I want the function to return a freshly-created variable with a true/false value that I can use to see if we've got the file or not.

// Check to make sure external files are available
function checklink ( $link, $checkname ) { 
    $try_url = @fopen( $link,'r' );
    if( $try_url !== false ) { return $$checkname; } 
}

var_dump( checklink( 'https://code.jquery.com/jquery-3.3.1.min.js', 'jqueryOK' ) ); // NULL

I've tried setting $checkname to true or false, adding an extra line to give it a value before return ... PHP 'knows' there is a variable $jqueryOK but says it's undefined.

What am I missing?

UPDATE

Decided to share the outcome, as this is often an overlooked thing in Wordpress - and am changing the title to reflect the task.

// Check to make sure external files are available
function checklink ($link) { 
return( bool )@fopen( $link, 'r' );
}

function thatwomanuk_external_scripts()
{
if ($GLOBALS['pagenow'] != 'wp-login.php' && !is_admin()) {

// jquery
    $link = 'https://code.jquery.com/jquery-3.3.1.min.js';
    if( checklink( $link ) ) { // true - otherwise, Wordpress will load its own     
        wp_deregister_script('jquery'); // remove jQuery v1
        wp_register_script('jquery', $link, array(), '3.3.1', true); // add jQuery v3
        wp_enqueue_script('jquery');
        wp_script_add_data( 'jquery', array( 'integrity', 'crossorigin' ), array( 'sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=', 'anonymous' ) );
    }   
// google fonts
    $link = 'https://fonts.googleapis.com/css?family=Raleway:300,400,400i,500|Ubuntu:300,400,400i,500&subset=latin-ext';
    $fallback = get_template_directory_uri() . '/fonts/thatwoman-fonts-min.css';
    if( checklink( $link ) ) {  
        wp_enqueue_style( 'custom-google-fonts', $link, false ); 
    } else {    
        wp_enqueue_style( 'custom-google-fonts', $fallback, false ); 
    }   
// touch events library
    $link = 'https://cdnjs.cloudflare.com/ajax/libs/jquery-touch-events/1.0.5/jquery.mobile-events.js';
    $fallback = get_template_directory_uri() . '/js/lib/jquery.mobile-events.min.js';
    if( checklink( $link ) ) {          
        wp_register_script('thatwoman-touch-events', $link, array( 'jquery' ), '1.0.5', true);
    } else {            
        wp_register_script('thatwoman-touch-events', $fallback, array( 'jquery' ), '1.0.5', true);
    }
    wp_enqueue_script('thatwoman-touch-events');
}
}
add_action( 'wp_enqueue_scripts', 'thatwomanuk_external_scripts' );

Thanks to @u_mulder for making me see sense.

  • 写回答

3条回答 默认 最新

  • dongtu0363 2018-06-02 17:20
    关注

    You should simplify your function to:

    // Check to make sure external files are available
    function checklink ($link) { 
        return (bool)@fopen( $link,'r');
    }
    

    After that in your code:

    $link1Available = checklink($link1);
    $link2Available = checklink($link2);
    // etc
    

    Or as an array:

    $links = ['link1', 'link2', 'link3'];
    $linksAvailable = [];
    foreach ($links as $link) {
        $linksAvailable[$link] = checklink($link);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料