我无法弄清楚我做错了什么!我希望函数返回一个带有true / false值的新创建的变量,我可以用它来查看是否有文件。
// 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
我已经尝试将$ checkname设置为true或false,在返回之前添加一个额外的行给它一个值... PHP'知道'有一个变量$ jqueryOK但是说它是未定义的。
我错过了什么?
更新
决定分享结果,因为这在Wordpress中经常被忽视 - 并且正在改变标题以反映任务。
// 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' );
感谢@u_mulder使我看得见。
答案 0 :(得分:2)
您应该将功能简化为:
// Check to make sure external files are available
function checklink ($link) {
return (bool)@fopen( $link,'r');
}
之后在你的代码中:
$link1Available = checklink($link1);
$link2Available = checklink($link2);
// etc
或作为数组:
$links = ['link1', 'link2', 'link3'];
$linksAvailable = [];
foreach ($links as $link) {
$linksAvailable[$link] = checklink($link);
}
答案 1 :(得分:1)
你在返回声明中有双重$$签名:
if( $try_url !== false ) { return $$checkname; }
答案 2 :(得分:1)
如果您只想对CDN加载的js文件进行本地后备,则可以使用在4.5中添加的wp_scripts()->add_inline_script()来添加js,以检查是否加载了库,请参阅有关wordpress.stackexchange here的详细答案。
简短版本:
您可以使用wp_scripts()->add_inline_script(),它将js脚本附加到排队的脚本中,不问任何问题,不检查任何代码问题(存在包装器功能wp_add_inline_script,该功能可以检查但不允许{ {1}}标签,甚至已编码)。
这是一个可行的示例
</script>
这会给你
function _enqueue_my_scripts() {
// the normal enqueue
wp_enqueue_script( 'jquery-cdn', 'https://code.jquery.com/jquery-3.3.1.min.js' );
// our custom code which will check for the existance of jQuery (in this case)
wp_scripts()->add_inline_script( 'jquery-cdn',
"window.jQuery || document.write('<script src=\"".get_template_directory_uri() ."/js/jQuery.js\">\\x3C/script>')", 'after');
}
add_action('wp_enqueue_scripts', '_enqueue_my_scripts', 100);
在您的HTML中。
根据您的需要替换“ window.jQuery”和“ /js/jQuery.js”,但不要触摸其余的行,因为这是为了将正确的行传递到HTML。