Nanostructured polyaniline (PANI) has the ability of fast and reversible acid/base doping/dedoping, multi-color changes and electron-donating/accepting using surface or near-surface reactions. Here we report PANI films with 3D ordered nanostructures through template-assisted electrodeposition method and aim to study the ion diffusion behavior as well as optical switching properties. It is found the ion diffusion coefficient is greatly increased because of the chain and crystalline structural modulation of deposited polyaniline arising from the template confinement effect. The ion diffusion reversibility and ion storage of 3D ordered macroporous (3DOM) PANI is highly improved due to the interconnected porous structure, which benefits for the fast penetration of ions. The optical switching properties of 3DOM PANI are also highly enhanced especially in the infrared region mainly because of the extended optical transmission path associated with the interconnected porous structure. The outstanding ion/electron doping/dedoping performance and better interaction with lights shown here render the ordered porous polyaniline a promising candidate as high-density electrochemical/electrochromic energy materials.