The error message describes your problem quite clearly:
Notice: wp_deregister_script was called incorrectly.
Do not deregister thejquery
script in the administration area.
To target the front-end theme, use thewp_enqueue_scripts
hook.
You’re de-registering the script on the init
hook:
add_action('init', 'replace_jquery');
This hook runs for the back and front end, but the debugger isn’t smart enough to know that you’re using ! is_admin()
inside the function. Regardless, you should just do what the error recommends and use the wp_enqueue_scripts
hook:
add_action('wp_enqueue_scripts', 'replace_jquery');
The original script will not be enqueued yet on the init
hook, so attempting to de-register it on that hook won’t work, as it will only get re-registered by the time the wp_enqueue_scripts
hook runs.
Also, the way you’re going about this is likely to cause problems, for 2 reasons:
- You’re enqueuing jQuery 3.3.1, but WordPress uses 1.12.4. This means that WordPress itself and the vast majority of plugins are expecting 1.12.4 to be loaded, but 3.3.1 will be loaded. Code written expecting 1.X will not necessarily be compatible with 3.X, which could cause plugins to break.
- You’re enqueueing it with a different handle,
jquery2
. This means that any scripts that declarejquery
as a dependency will not load. This will break a great number of plugins.