Mobile genetic elements (MGEs) encoding virulence and resistance genes are widespread in bacterial pathogens, but it has remained unclear how they occasionally jump to new host species. Staphylococcus aureus clones exchange MGEs such as S. aureus pathogenicity islands (SaPIs) with high frequency via helper phages. Here we report that the S. aureus ST395 lineage is refractory to horizontal gene transfer (HGT) with typical S. aureus but exchanges SaPIs with other species and genera including Staphylococcus epidermidis and Listeria monocytogenes. ST395 produces an unusual wall teichoic acid (WTA) resembling that of its HGT partner species. Notably, distantly related bacterial species and genera undergo efficient HGT with typical S. aureus upon ectopic expression of S. aureus WTA. Combined with genomic analyses, these results indicate that a 'glycocode' of WTA structures and WTA-binding helper phages permits HGT even across long phylogenetic distances thereby shaping the evolution of Gram-positive pathogens.