Forms are the lifeblood of any interactive website. Whether it’s a simple contact form, a newsletter signup, or a lead generation powerhouse, forms are essential for connecting with your audience. But in many content management systems, like WordPress, managing forms means wrestling with third-party plugins that can be slow, insecure, and complicated.
AstroPress takes a different approach.
In this guide, we’ll explore the built-in AstroPress forms system—a powerful, developer-friendly, and incredibly simple way to manage all your site’s forms without a single plugin.
The Problem with Traditional Form Plugins
For years, the go-to solution for forms in WordPress has been to install a plugin like Contact Form 7, WPForms, or Gravity Forms. While powerful, these plugins often come with significant downsides:
- Performance Bloat: They load extra CSS, JavaScript, and database queries on every page, slowing down your site.
- Security Risks: Form plugins are a common target for hackers. A single vulnerability can compromise your entire site.
- Complex Configuration: Building and configuring forms often involves a clunky UI, and styling them to match your theme can be a nightmare.
- Data Silos: Your form data is locked away in the WordPress database, making it difficult to integrate with modern marketing tools.
AstroPress solves these problems by treating forms as part of your content and code, not as an afterthought.
The AstroPress Way: Centralized Form Management
At the heart of the AstroPress forms system is a single, simple JSON file: /src/content/data/forms.json. This file is the single source of truth for all your forms.
Here’s a look at its structure:
[
{
"id": "sidebar-lead",
"enabled": true,
"title": "Subscribe to Updates",
"description": "Get the latest coffee tips and brewing guides delivered to your inbox.",
"image": "/coffee-newsletter.jpg",
"imageAlt": "Coffee newsletter signup",
"buttonText": "Subscribe"
},
{
"id": "contact-us",
"enabled": true,
"title": "Contact Us",
"description": "Have questions about coffee? We're here to help.",
"buttonText": "Send Message"
}
]
This approach has several key advantages:
- Simplicity: Anyone, even non-developers, can easily update form text, titles, and settings by editing this file.
- Version Control: Your form configurations are stored in Git, so you can track changes, revert to previous versions, and collaborate with your team.
- Consistency: All your forms are defined in one place, ensuring a consistent voice and style across your site.
Anatomy of a Form Configuration
Let’s break down the fields for each form object:
id(required): A unique identifier for the form. This is how you’ll reference the form in your components. Use clear, descriptive names likecontact-usornewsletter-sidebar.enabled(required): A boolean (trueorfalse) that lets you enable or disable a form with a single switch. No need to delete the configuration.title(required): The main heading displayed on the form.description(optional): A short piece of text to encourage users to fill out the form.imageandimageAlt(optional): Add a visual element to your form, like a product image or a newsletter icon.buttonText(required): The text that appears on the submission button (e.g., “Send Message,” “Subscribe Now”).
Ready-to-Use Form Components
AstroPress comes with pre-built Astro components that are automatically linked to your forms.json file. You can find them in /src/components/forms/.
1. ContactForm.astro
This is a full-featured contact form, perfect for your “Contact Us” page. It includes fields for:
- Name
- Message
- A required “Terms & Conditions” checkbox
How it Works:
The ContactForm.astro component automatically fetches the configuration for the form with the id of contact-us from your forms.json file.
To use it, simply import it and add it to any page:
---
import ContactForm from '~/components/forms/ContactForm.astro';
---
<ContactForm />
The component handles everything: rendering the form, client-side validation, and submitting the data.
2. SidebarLeadForm.astro
This is a compact form designed for capturing leads, perfect for your blog’s sidebar or footer. It includes a single field for an email address and a “Terms & Conditions” checkbox.
How it Works:
Similarly, SidebarLeadForm.astro is hardwired to look for the form with the id of sidebar-lead in your forms.json file.
You can place it in any sidebar or call-to-action area:
---
import SidebarLeadForm from '~/components/forms/SidebarLeadForm.astro';
---
<div class="sidebar">
<SidebarLeadForm />
</div>
Secure and Modern Form Submission with Astro Actions
So, where does the data go? AstroPress uses a modern and secure feature called Astro Actions.
When a user submits a form, the following happens:
- The client-side script in the form component captures the data.
- It securely sends the data to a serverless function (an Astro action) that you define. In AstroPress, this is pre-configured to handle lead submissions.
- This action can then process the data—validate it, save it to a database, send an email, or forward it to a CRM or email marketing service.
This is a huge improvement over traditional methods:
- Enhanced Security: There’s no direct, exposed API endpoint for spammers to target. Astro handles the security layer.
- Developer Experience: The logic for handling form submissions is written in TypeScript, right inside your Astro project at
/src/actions/index.ts, making it easy to customize and extend. - Flexibility: You can connect your forms to any service you want, from Mailchimp and ConvertKit to your own custom database.
Why AstroPress Forms Are a Game-Changer
Let’s recap the benefits of the AstroPress forms system compared to a traditional WordPress setup.
| Feature | AstroPress | WordPress + Plugin |
|---|---|---|
| Performance | Zero impact. Forms are static HTML until used. | Adds CSS/JS to every page, slowing down the site. |
| Security | Secure by design with Astro Actions. | Plugins are a major source of vulnerabilities. |
| Configuration | A single, simple forms.json file. | Complex, multi-screen UI in the admin dashboard. |
| Styling | Easy to style with Tailwind CSS or plain CSS. | Often requires CSS overrides and !important. |
| Version Control | All form configurations are tracked in Git. | Form data is stored in the database, not in Git. |
| Extensibility | Easily extendable with TypeScript and Astro. | Locked into the features of the chosen plugin. |
Get Started with AstroPress Forms
You’re already set up for success. To customize your forms, simply:
- Edit the Content: Open
/src/content/data/forms.jsonand modify the titles, descriptions, and button text to match your brand’s voice. - Place the Components: Add the
<ContactForm />or<SidebarLeadForm />components to your pages where you want them to appear. - Customize the Action (Optional): For advanced users, you can open
/src/actions/index.tsto change what happens on form submission.
With AstroPress, you get a forms system that is not only easier to manage but also faster, more secure, and infinitely more flexible. It’s another example of how AstroPress puts developers and content creators back in control.