Halloween Sale 15% off Ends in See the plans
HTML

Monthly/Yearly Pricing Toggle

Adds a switch that flips prices on your pricing page between monthly and yearly amounts, using simple data attributes on any element that shows a price.

Don’t have WPIDE yet? Install it on your site

Code

<style>
.wpide-price-toggle{display:inline-flex;align-items:center;gap:.6em;font-weight:600;}
.wpide-price-toggle button{position:relative;width:52px;height:28px;border:0;border-radius:999px;background:#c3c4c7;cursor:pointer;transition:background .2s;}
.wpide-price-toggle button::after{content:"";position:absolute;top:3px;left:3px;width:22px;height:22px;border-radius:50%;background:#fff;transition:transform .2s;}
.wpide-price-toggle button[aria-checked="true"]{background:#2271b1;}
.wpide-price-toggle button[aria-checked="true"]::after{transform:translateX(24px);}
@media (prefers-reduced-motion: reduce){.wpide-price-toggle button,.wpide-price-toggle button::after{transition:none;}}
[data-wpide-period][hidden]{display:none!important;}
</style>
<script>
(function () {
    var holders = document.querySelectorAll('[data-wpide-price-toggle]');
    if (!holders.length) { return; }
    function apply(yearly) {
        document.querySelectorAll('[data-monthly][data-yearly]').forEach(function (el) {
            el.textContent = el.getAttribute(yearly ? 'data-yearly' : 'data-monthly');
        });
        document.querySelectorAll('[data-wpide-period]').forEach(function (el) {
            el.hidden = el.getAttribute('data-wpide-period') !== (yearly ? 'yearly' : 'monthly');
        });
        document.querySelectorAll('.wpide-price-toggle button').forEach(function (b) { b.setAttribute('aria-checked', yearly ? 'true' : 'false'); });
    }
    holders.forEach(function (holder) {
        holder.className += ' wpide-price-toggle';
        holder.innerHTML = '<span>Monthly</span><button type="button" role="switch" aria-checked="false" aria-label="Show yearly prices"></button><span>Yearly</span>';
        holder.querySelector('button').addEventListener('click', function () {
            apply(this.getAttribute('aria-checked') !== 'true');
        });
    });
    apply(false);
})();
</script>

Notes

Add a Custom HTML block with <div data-wpide-price-toggle></div> where the switch should appear, and give price elements data-monthly="$19" data-yearly="$190" (Custom HTML or the block's HTML editing).

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.