The matter produced in the early stages of heavy ion collisions consists mostly of gluons, and is penetrated by the coherent magnetic field produced by spectator nucleons. The fluctuations of gluonic matter in an external magnetic field couple to real and virtual photons through virtual quark loops. We study the resulting contributions to photon and dilepton production that stem from the fluctuations of the stress tensor ${T}_{\ensuremath{\mu}\ensuremath{\nu}}$ in the background of a coherent magnetic field $\stackrel{P\vec}{B}$. Our study extends significantly the earlier work [G. Basar, D. E. Kharzeev, and V. Skokov, Phys. Rev. Lett. 109, 202303 (2012)], in which only the fluctuations of the $trace$ of the stress tensor ${T}_{\ensuremath{\mu}\ensuremath{\mu}}$ were considered (the coupling of ${T}_{\ensuremath{\mu}\ensuremath{\mu}}$ to electromagnetic fields is governed by the scale anomaly). In the present paper we derive more general relations using the operator product expansion (OPE). We also extend the previous study to the case of dileptons, which offers the possibility to discriminate between various production mechanisms. Among the phenomena that we study are magneto-sonoluminescence [MSL, the interaction of magnetic field $\stackrel{P\vec}{B}(x,t)$ with the sound perturbations of the stress tensor $\ensuremath{\delta}{T}_{\ensuremath{\mu}\ensuremath{\nu}}(x,t)$] and magneto-thermoluminescence [MTL, the interaction of $\stackrel{P\vec}{B}(x,t)$ with smooth average $\ensuremath{\langle}{T}_{\ensuremath{\mu}\ensuremath{\nu}}\ensuremath{\rangle}$]. We calculate the rates of these process and find that they can dominate the photon and dilepton production at early stages of heavy ion collisions. We also point out the characteristic signatures of MSL and MTL that can be used to establish their presence and to diagnose the produced matter.