Hard carbons are one of the most promising anode materials for sodium/potassium-ion batteries (SIBs/PIBs), which demonstrate favorable long charge/discharge plateaus, but suffer from low rate performance. Herein, we report a new design of N–P codoped hard carbon (NPHC) with three-dimensional (3D) hierarchical porous frameworks. Such unique structure provides bicontinuous ion/electron transportation paths, regulated electronic structure, enlarged interlayer spacing, and moderate surface area. The as prepared NPHC demonstrates a high reversible specific capacity (336 mAh g−1 for SIBs, 339 mAh g−1 for PIBs), along with a good rate performance of ~5.3 C. Particularly, an in-depth study on the charge storage mechanism for both SIBs and PIBs is conducted by combining in-situ Raman spectra and quasi in-situ synchrotron X-ray diffraction analysis, whereby the coexistence of physical adsorption/graphitic layer intercalation, or intercalation/pore filling within certain potential ranges is detected, and the ion storage behavior at different charge/discharge stages is precisely identified.