脚本已加载但仅在用户登录时执行
Script is loaded but only executed when User is logged in
我 运行 遇到了一个问题,我的脚本已加载但仅在我登录时才执行。用户的角色无关紧要。
它上周运行良好,据我所知,此后网站没有任何变化。
为了对此进行测试,我尝试执行以下代码:
console.log('??????????')
文件中没有其他代码。
这是我的结果:
未登录:
I can find it in the 'Network' tab of the Chrome console
But there are no '??????' in the console
以普通用户身份登录
I can still find it in the 'Network' tab
There are '??????' in the console
脚本如何入队:
function custom_scripts() {
$rand = rand(1, 99999999);
wp_enqueue_script(
'custom', /* Name für das Script */
get_stylesheet_directory_uri() . '/custom.js', /* Pfad */
array( 'jquery' ), /* jQuery ist erforderlich */
false,
true
);
wp_enqueue_script( 'js4u', get_stylesheet_directory_uri() . '/js4u/js4u.min.js', array( 'jquery'), $rand, "all");
if( is_page(8) ) {
wp_enqueue_script( 'create-listing', get_stylesheet_directory_uri().'/js4u/create-listing.js',array(), $rand, "all");
}
if( is_page(13) ) {
wp_enqueue_script( 'login-page', get_stylesheet_directory_uri().'/js4u/login-page.js',array(), $rand, "all");
}
if( is_page(14) ) {
wp_enqueue_script( 'register-page', get_stylesheet_directory_uri().'/js4u/register-page.js',array(), $rand, "all");
}
wp_enqueue_script( 'primary-menu', get_stylesheet_directory_uri().'/js4u/primary-menu.js',array(), $rand, "all");
}
add_action( 'wp_enqueue_scripts', 'custom_scripts', 999 );
如果我将代码直接粘贴到控制台,它会按预期工作。
我在 Chrome、Firefox 和 Edge 中试过,结果相同。
隐身模式和普通模式也没有区别。
这是唯一出现此问题的文件,其他文件仍然可以正常工作。
希望你能帮助我:D
更新:警报和 console.log 似乎在除首页以外的所有页面上都有效。其余代码仍然没有做任何事情。
应该执行的代码:
function fn() {
let loginButton = document.querySelectorAll('#login-button-link')[1].parentNode;
let usernameMenuItem = document.getElementById('menu-item-username');
let firmaEintragen = document.getElementsByClassName('menu-item-8199')[1];
let favorites = document.querySelectorAll('#menu-item-my-favorites')[1];
/**
* Entfernt den Eintrag 'Favorites' aus dem Username Dropdown im Header
*/
if (usernameMenuItem){
favorites.parentNode.removeChild(favorites);
console.log('Username Dropdown angepasst');
}
/**
* Entfernt den "Firma Eintragen" Button wenn der Login-Button sichtbar ist, der User also nicht eingeloggt ist.
*/
let loginStyle = loginButton.style.display;
if (!(loginStyle === 'none')) {
firmaEintragen.style.display = 'none';
console.log('Firma Eintragen entfernt');
}
if (loginStyle === 'none') {
loginStyle = 'table-cell';
console.log('Login geändert')
}
}
fn();
现在一切正常。
非常感谢@jasie
已将 $rand
转换为带有 $rand = strval(rand(1, 99999999));
的字符串。
已将 $in_footer
更改为 true
。
启用缓存插件 WP Rocket 并清除缓存几次。
functions.php中的代码:
function custom_scripts() {
$rand = strval(rand(1, 99999999));
wp_enqueue_script(
'custom', /* Name für das Script */
get_stylesheet_directory_uri() . '/custom.js', /* Pfad */
array( 'jquery' ), /* jQuery ist erforderlich */
false,
true
);
wp_enqueue_script( 'js4u', get_stylesheet_directory_uri() . '/js4u/js4u.min.js', array( 'jquery'), $rand, true);
if( is_page(8) ) {
wp_enqueue_script( 'create-listing', get_stylesheet_directory_uri().'/js4u/create-listing.js',array(), $rand, true);
}
if( is_page(13) ) {
wp_enqueue_script( 'login-page', get_stylesheet_directory_uri().'/js4u/login-page.js',array(), $rand, true);
}
if( is_page(14) ) {
wp_enqueue_script( 'register-page', get_stylesheet_directory_uri().'/js4u/register-page.js',array(), $rand, true);
}
wp_enqueue_script( 'primary-menu', get_stylesheet_directory_uri().'/js4u/primary-menu.js', array('jquery'), $rand, true);
}
add_action( 'wp_enqueue_scripts', 'custom_scripts', 999 );
更正这两个错误,然后看看是否有效:
- wp_enqueue_script() 的最后一个参数需要是 Bool。您正在传递一个可能默认为 false 的 String ("all")(它至少应该引起 PHP 警告)。
- 倒数第二个参数必须是字符串,bool 或 null。您正在传递一个 Integer ,因为这就是 rand() return (它也应该引起 PHP 警告)。将 int 转换为字符串(例如通过 strval())。
$ver 参数与缓存非常相关!确保您正确阅读了文档:https://developer.wordpress.org/reference/functions/wp_enqueue_script/
此外,我建议您同时激活 WP_DEBUG
和 SCRIPT_DEBUG
:https://wordpress.org/support/article/debugging-in-wordpress/#wp_debug
最后的建议:正确删除浏览器缓存(这可以在浏览器设置中完成)。
我 运行 遇到了一个问题,我的脚本已加载但仅在我登录时才执行。用户的角色无关紧要。
它上周运行良好,据我所知,此后网站没有任何变化。
为了对此进行测试,我尝试执行以下代码:
console.log('??????????')
文件中没有其他代码。
这是我的结果:
未登录:
I can find it in the 'Network' tab of the Chrome console
But there are no '??????' in the console
以普通用户身份登录
I can still find it in the 'Network' tab
There are '??????' in the console
脚本如何入队:
function custom_scripts() {
$rand = rand(1, 99999999);
wp_enqueue_script(
'custom', /* Name für das Script */
get_stylesheet_directory_uri() . '/custom.js', /* Pfad */
array( 'jquery' ), /* jQuery ist erforderlich */
false,
true
);
wp_enqueue_script( 'js4u', get_stylesheet_directory_uri() . '/js4u/js4u.min.js', array( 'jquery'), $rand, "all");
if( is_page(8) ) {
wp_enqueue_script( 'create-listing', get_stylesheet_directory_uri().'/js4u/create-listing.js',array(), $rand, "all");
}
if( is_page(13) ) {
wp_enqueue_script( 'login-page', get_stylesheet_directory_uri().'/js4u/login-page.js',array(), $rand, "all");
}
if( is_page(14) ) {
wp_enqueue_script( 'register-page', get_stylesheet_directory_uri().'/js4u/register-page.js',array(), $rand, "all");
}
wp_enqueue_script( 'primary-menu', get_stylesheet_directory_uri().'/js4u/primary-menu.js',array(), $rand, "all");
}
add_action( 'wp_enqueue_scripts', 'custom_scripts', 999 );
如果我将代码直接粘贴到控制台,它会按预期工作。
我在 Chrome、Firefox 和 Edge 中试过,结果相同。
隐身模式和普通模式也没有区别。
这是唯一出现此问题的文件,其他文件仍然可以正常工作。
希望你能帮助我:D
更新:警报和 console.log 似乎在除首页以外的所有页面上都有效。其余代码仍然没有做任何事情。
应该执行的代码:
function fn() {
let loginButton = document.querySelectorAll('#login-button-link')[1].parentNode;
let usernameMenuItem = document.getElementById('menu-item-username');
let firmaEintragen = document.getElementsByClassName('menu-item-8199')[1];
let favorites = document.querySelectorAll('#menu-item-my-favorites')[1];
/**
* Entfernt den Eintrag 'Favorites' aus dem Username Dropdown im Header
*/
if (usernameMenuItem){
favorites.parentNode.removeChild(favorites);
console.log('Username Dropdown angepasst');
}
/**
* Entfernt den "Firma Eintragen" Button wenn der Login-Button sichtbar ist, der User also nicht eingeloggt ist.
*/
let loginStyle = loginButton.style.display;
if (!(loginStyle === 'none')) {
firmaEintragen.style.display = 'none';
console.log('Firma Eintragen entfernt');
}
if (loginStyle === 'none') {
loginStyle = 'table-cell';
console.log('Login geändert')
}
}
fn();
现在一切正常。
非常感谢@jasie
已将 $rand
转换为带有 $rand = strval(rand(1, 99999999));
的字符串。
已将 $in_footer
更改为 true
。
启用缓存插件 WP Rocket 并清除缓存几次。
functions.php中的代码:
function custom_scripts() {
$rand = strval(rand(1, 99999999));
wp_enqueue_script(
'custom', /* Name für das Script */
get_stylesheet_directory_uri() . '/custom.js', /* Pfad */
array( 'jquery' ), /* jQuery ist erforderlich */
false,
true
);
wp_enqueue_script( 'js4u', get_stylesheet_directory_uri() . '/js4u/js4u.min.js', array( 'jquery'), $rand, true);
if( is_page(8) ) {
wp_enqueue_script( 'create-listing', get_stylesheet_directory_uri().'/js4u/create-listing.js',array(), $rand, true);
}
if( is_page(13) ) {
wp_enqueue_script( 'login-page', get_stylesheet_directory_uri().'/js4u/login-page.js',array(), $rand, true);
}
if( is_page(14) ) {
wp_enqueue_script( 'register-page', get_stylesheet_directory_uri().'/js4u/register-page.js',array(), $rand, true);
}
wp_enqueue_script( 'primary-menu', get_stylesheet_directory_uri().'/js4u/primary-menu.js', array('jquery'), $rand, true);
}
add_action( 'wp_enqueue_scripts', 'custom_scripts', 999 );
更正这两个错误,然后看看是否有效:
- wp_enqueue_script() 的最后一个参数需要是 Bool。您正在传递一个可能默认为 false 的 String ("all")(它至少应该引起 PHP 警告)。
- 倒数第二个参数必须是字符串,bool 或 null。您正在传递一个 Integer ,因为这就是 rand() return (它也应该引起 PHP 警告)。将 int 转换为字符串(例如通过 strval())。
$ver 参数与缓存非常相关!确保您正确阅读了文档:https://developer.wordpress.org/reference/functions/wp_enqueue_script/
此外,我建议您同时激活 WP_DEBUG
和 SCRIPT_DEBUG
:https://wordpress.org/support/article/debugging-in-wordpress/#wp_debug
最后的建议:正确删除浏览器缓存(这可以在浏览器设置中完成)。