• Resolved bearshang

    (@bearshang)


    I want to modify static resource url host, I use style_loader_src and script_loader_src, But it can’t normal working, It give me notice error:

    
    <br />
    <b>Notice</b>:  Undefined index: scheme in <b>/home/wwwroot/trueniu-php/wp-content/plugins/trueniu-init/assets/admin/class-tn-admin-asset.php</b> on line <b>70</b><br />
    <br />
    <b>Notice</b>:  Undefined index: query in <b>/home/wwwroot/trueniu-php/wp-content/plugins/trueniu-init/assets/admin/class-tn-admin-asset.php</b> on line <b>70</b><br />
    

    It seems that something is blocking him. What is going on?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The code at line 70 of class-tn-admin-asset.php is expecting arguments in an array with certain elements, but the array keys ‘scheme’ and ‘query’ are not assigned values. You get such notices when you do something like

    $array = ['foo'=>'some value', 'bar'=>'another value',];
    echo "The foo is {$array['foo']}<br>\n"; // works
    echo "The scheme is {$array['scheme']}"; // error
    Thread Starter bearshang

    (@bearshang)

    @bcworkz

    Yes, I know, I just don’t understand why this error message appears.

    This is my code:

    
    add_filter('style_loader_src', 'modifying_resource_urls', 99, 2);
    add_filter('script_loader_src', 'modifying_resource_urls', 99, 2);
    
    function modifying_resource_urls( $src, $handle ) {
    	$parse_url = wp_parse_url( $src );
    
    	$exclude_host = [
    		'gw.alipayobjects.com',
    		'www.gstatic.com',
    	];
    
    	if ( ! in_array( $parse_url['host'], $exclude_host, true ) ) {
    		$cdn_host = 'cdn.example.cn';
    		$src      = $parse_url['scheme'] . '://' . $cdn_host . $parse_url['path'] . '?' . $parse_url['query'];
    	}
    
    	return esc_url( $src );
    }
    

    Under normal circumstances, the parameter $src is definitely a standard url.

    Is there anything I understand is wrong?

    • This reply was modified 7 years, 9 months ago by bearshang.
    • This reply was modified 7 years, 9 months ago by bearshang.
    Thread Starter bearshang

    (@bearshang)

    @bcworkz
    I solved him.

    When I echo $src, I found that there was an ” $src, So wp_parse_url won’t work.

    Although I don’t understand why there is an ” $Src, obviously this should be the root cause.

    Moderator bcworkz

    (@bcworkz)

    Ah, I see now. There shouldn’t be anything besides a proper URL in $src. If there is anything else, I would expect that your theme or a plugin is not properly enqueuing resources. Whatever path information exists (if any) in the bad data should indicate which module is causing the problem.

    If $src is so corrupted that there is no path to use as a clue, var_dump and examine the global $wp_scripts and $wp_styles arrays and locate the bad URL. The ‘handle’ key above the URL tells you the registered enqueue handle, which will also be in the source code of the offending module. Use grep or similar to find where it is in source if the handle or URL alone is not enough of a clue.

    Since you’ve solved this, maybe this does not matter, but invalid data could be a sign of other issues. It’s a good idea to resolve the root cause. The presence of ”$src is not the root cause, the code that caused it to be that way is the root cause.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘style_loader_src and script_loader_src not normal work、’ is closed to new replies.