A strategy for synthesizing highly functionalized porous silica–carbon composites made up of a sulphur- or nitrogen-doped carbon layer coating the pores of two mesostructured silica samples (i.e. SBA-15 and KIT-6) is presented. The synthesis scheme involves several steps: a) infiltration of the silica pores by sulphur-rich (thiophene) or nitrogen-rich (pyrrole) monomers, b) in situ polymerization of these precursors to form polythiophene or polypyrrole, and c) carbonization of the polymers. The resulting silica–carbon composites contain ∼25 wt % of carbonaceous matter and a large number of nitrogen and sulphur functional groups attached to the deposited carbon (up to 4.2 wt % of nitrogen and 6.1 wt % of sulphur). The structural characteristics of the parent silica are retained in the composite materials, which exhibit high surface area, large pore volume and a well-ordered porosity made up of uniform mesopores.