Porous heteroatom-doped carbons are desirable for catalytic reactions due to their tunable physicochemical properties, low cost and metal-free nature. Herein, we introduce a facile, general bottom-up strategy, so-called cooking carbon in a solid salt, to prepare hierarchically porous heteroatom-doped carbon foams by using poly(ionic liquid) as precursor and a common inorganic salt as structural template. The obtained carbon foams bear hierarchical micro-/meso-/macropores, large specific surface area and rich nitrogen dopant. The combination of these favorable features facilitates the catalytic degradation of aqueous organic pollutants by persulfate under visible light irradiation, in which they prevail over the state-of-the-art metal-/carbon-based catalysts.