The demands for green production of hydrogen peroxide have triggered extensive studies in the photocatalytic synthesis, but most photocatalysts suffer from rapid charge recombination and poor 2e − oxygen reduction reaction (ORR) selectivity. Here, a novel composite photocatalyst of cyanorich graphitic carbon nitride g-C 3 N 4 is fabricated in a facile manner by sodium chloride-assisted calcination on dicyandiamide. The obtained photocatalysts exhibit superior activity (7.01 mm h −1 under λ ≥ 420 nm, 16.05 mm h −1 under simulated sun conditions) for H 2 O 2 production and 93% selectivity for 2e − ORR, much higher than that of the state-of-the-art photocatalyst. The porous g-C 3 N 4 with Na dopants and cyano groups simultaneously optimize two limiting steps of the photocatalytic 2e − ORR: photoactivity, and selectivity. The cyano groups can adjust the band structure of g-C 3 N 4 to achieve high activity. They also serve as oxygen adsorption sites, in which local charge polarization facilitates O 2 adsorption and protonation. With the aid of Na + , the O 2 is reduced to produce more superoxide radicals as the intermediate products for H 2 O 2 synthesis. This work provides a facile approach to simultaneously tune photocatalytic activity and 2e − ORR selectivity for boosting H 2 O 2 production, and then paves the way for the practical application of g-C 3 N 4 in environmental remediation and energy supply.