  Lattice vibrations in materials induce perturbations on the electron dynamics
in the form of long-range (dipole and quadrupole) and short-range (octopole and
higher) potentials. The dipole Fr\"ohlich term can be included in current
first-principles electron-phonon ($e$-ph) calculations and is present only in
polar materials. The quadrupole $e$-ph interaction is present in both polar and
nonpolar materials, but currently it cannot be computed from first principles.
Here we show an approach to compute the quadrupole $e$-ph interaction and
include it in ab initio calculations of $e$-ph matrix elements. The accuracy of
the approach is demonstrated by comparing with direct density functional
perturbation theory calculations. We apply our method to silicon as a case of a
nonpolar semiconductor and tetragonal PbTiO$_3$ as a case of a polar
piezoelectric material. In both materials we find that the quadrupole term
strongly impacts the $e$-ph matrix elements. Analysis of $e$-ph interactions
for different phonon modes reveals that the quadrupole term mainly affects
optical modes in silicon and acoustic modes in PbTiO$_3$, although the
quadrupole term is needed for all modes to achieve quantitative accuracy. The
effect of the quadrupole $e$-ph interaction on electron scattering processes
and transport is shown to be important. Our approach enables accurate studies
of $e$-ph interactions in broad classes of nonpolar, polar and piezoelectric
materials.
