Heteroatoms doping and morphology design have been considered as effective approaches to enhancing the photocatalytic activity of graphitic carbon nitride (g‐C 3 N 4 ), whereas few of them have focused on the synergistic effect of codoping and the optical property of the porous structure during the catalytic process. Herein, a porous g‐C 3 N 4 sphere codoped with K and I is designed and prepared to investigate the different roles played during the reaction. Compared with the pristine carbon nitride, the activity of the prepared catalyst increased by 7.9 times to 75.2 μmol h −1 . Experimental results with density function theory calculations indicate the different roles of two elements played during the photocatalysis process and optical simulations are given to prove the enhancement of the porous structure in light absorption ability. The pore structure improves the light absorption and mass transfer capabilities of the catalyst, while the codoping of metal and nonmetal develop the separation capabilities of the carriers and optimize the energy band structure respectively, which synthetically lead to an enhanced performance of the photocatalysis.