'defer', 'in_footer' => true, ) ); wp_localize_script( 'affistyle-tracker-js', 'affistyleTrackerData', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'affistyle_track_click_nonce' ), ) ); $should_enqueue = false; // 1. Check if on a singular post/page containing the block or shortcode. if ( is_singular() && is_a( $post, 'WP_Post' ) ) { if ( has_block( 'affistyle/aff-box', $post ) || has_block( 'affistyle/aff-box-block', $post ) || has_shortcode( $post->post_content, 'affistyle_box' ) || has_shortcode( $post->post_content, 'aff_box' ) ) { $should_enqueue = true; } } // 2. Allow shortcode execution on archive/category/shop pages or widgets to trigger enqueue dynamically. if ( apply_filters( 'affistyle_enqueue_tracker_script', $should_enqueue ) ) { wp_enqueue_script( 'affistyle-tracker-js' ); } } } new AffiStyle_Tracker();