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.