Fluid-saturated granular and porous layers can undergo liquefaction and lose their shear resistance when subjected to shear forcing. In geosystems, such a process can lead to severe natural hazards of soil liquefaction, accelerating slope failure, and large earthquakes. Terzaghi's principle of effective stress predicts that liquefaction occurs when the pore pressure within the layer becomes equal to the applied normal stress on the layer. However, under dynamic loading and when the internal permeability is relatively small the pore pressure is spatially heterogeneous and it is not clear what measurement of pore pressure should be used in Terzaghi's principle. Here, we show theoretically and demonstrate using numerical simulations a general criterion for liquefaction that applies also for the cases in which the pore pressure is spatially heterogeneous. The general criterion demands that the average pore pressure along a continuous surface within the fluid-saturated granular or porous layer is equal to the applied normal stress.