Undulator radiation (UR) in an undulator with field harmonics in two orthogonal planes is theoretically studied taking nonperiodic magnetic components and off-axis effects into account. New analytical expressions for the UR spectrum and UR intensity are written explicitly in terms of the generalized Bessel function and Airy function. In limiting cases, they describe the radiation of a two-frequency planar undulator, a helical undulator, and an elliptical undulator. The influence of the finite size of the electron beam, the emittance, electron beam deflection from the axis, the spread of the electron energy, and the influence of the constant components of the magnetic field are taken into account analytically. The expressions make it possible to distinguish the contributions of each field component and characteristics of the beam and undulator to the generation of UR harmonics. The phenomenological FEL model is used to study the evolution of harmonic power in the FEL experiments of LCLS and LEUTL. The influence of the beam parameters and undulator parameters on the harmonic generation is analyzed. The theoretical results of the power and radiation spectrum of the FEL are in agreement with experiments. It is shown that the second harmonic in the X-ray FEL of LCLS can be due to the beam deflection from the axis by $${\sim}12$$ $$\mu$$ m, and the second harmonic in the UV-A FEL of LEUTL is due to wide ( $${\sim}0.2$$ mm) beams of electrons and photons.