Orbital period variations of hot Jupiters caused by the Applegate effect