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条)

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了