我想在我的脚本中添加评论,以便每当我查看页面源代码时我都可以看到该评论。我的代码是:
public function front_scripts() {
// Make sure that the GA ttracking code is being included
if ( 'on' === $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'install_ga_code', 'wp-analytify-profile', 'off' ) ) {
global $current_user;
$roles = $current_user->roles;
if ( ! isset( $roles[0] ) or ! in_array( $roles[0], $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'exclude_users_tracking', 'wp-analytify-profile', array() ) ) ) {
wp_enqueue_script( 'analytify-events-tracking', plugins_url( 'assets/js/analytify-events-tracking.min.js', __DIR__ ), array( 'jquery' ) );
add_filter( 'script_loader_tag', array( $this, 'add_script_comment_to_tracking_min' ), 10, 3 );
wp_localize_script(
'analytify-events-tracking',
'analytify_events_tracking',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'tracking_mode' => ANALYTIFY_TRACKING_MODE,
'ga_mode' => method_exists( 'WPANALYTIFY_Utils', 'get_ga_mode' ) ? WPANALYTIFY_Utils::get_ga_mode() : 'ga3',
'tracking_code' => WP_ANALYTIFY_FUNCTIONS::get_UA_code(),
'is_track_user' => analytify_is_track_user(),
'root_domain' => $this->get_root_domain(),
'affiliate_link' => $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'affiliate_link_path', 'wp-analytify-events-tracking', '' ),
'download_extension' => $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'file_extension', 'wp-analytify-events-tracking', 'zip|mp3*|mpe*g|pdf|docx*|pptx*|xlsx*|rar*' ),
'anchor_tracking' => $GLOBALS['WP_ANALYTIFY']->settings->get_option( 'anchor_tracking', 'wp-analytify-events-tracking' ),
)
);
}
}
}
我希望评论的方式是:
<script id="analytify-events-tracking-js-extra">
/* the comment to be shown. */
var analytify_events_tracking = {"ajaxurl":"http:\/\/analytify-github.local\/wp-admin\/admin-ajax.php","tracking_mode":"gtag","ga_mode":"ga4","tracking_code":"G-PF9VK8TRF6","is_track_user":"1","root_domain":"analytify-github.local","affiliate_link":[{"path":"https:\/\/wpbrigade.com\/?edd_action=add_to_cart&download_id=1835&edd_options%5Bprice_id%5D=4&_gl=1*bzfdf6*_ga*ODA1Mzc5NjgyLjE3MDM2ODA1MzM.*_ga_YCW7CRBRMF*MTcwNDM2NjA2Ny4xMi4wLjE3MDQzNjYxMTkuOC4wLjA.","label":"test"}],"download_extension":"zip|mp3*|mpe*g|pdf|docx*|pptx*|xlsx*|rar*","anchor_tracking":"on"};
</script>
我使用了以下方法来添加注释,但其中生成了另一个脚本:
$comment = "Your comment here";
wp_script_add_data('analytify-events-tracking', 'data', $comment);
但在此创建了另一个脚本。如何解决这个问题?
analytify 插件已经将脚本排入队列,因此您的代码将第二次将其排入队列。在调用 wp_enqueue_script 之前,尝试添加:
wp_dequeue_script( 'analytify-events-tracking' );
如果在 front_scripts 运行时插件已经将脚本排入队列,那么这应该可以工作。