Use URL parameter in Contact Form 7 Field

Use URL parameter in Contact Form 7 Field

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Using URL Parameters to Pre-fill Contact Form 7 Fields

Attempting to inject raw PHP logic directly into a textarea or hidden input within a Contact Form 7 form often leads to immediate failures. The core issue usually stems from how WordPress handles shortcode rendering versus standard PHP execution. When you try to execute code inside the form definition file, it runs before the server has fully initialized the session or loaded necessary hooks, causing the site to crash.

To achieve your goal of moving a value from $_SESSION into a Contact Form 7 field, you should bypass direct PHP injection in the form structure and instead use URL parameters. This approach leverages the HTTP request cycle naturally: the parameter arrives in the URL, gets read by the plugin during initialization, and populates the field automatically.

The Mechanism of URL Pre-filling

Contact Form 7 processes fields based on their defined name attributes. If you define a form element like [text* your-name], the plugin looks for a specific variable to populate that field when the page loads. By appending a query string parameter—such as ?your_name=John—you can provide that value directly to the rendering context.

This method is distinct from trying to echo PHP variables inside the shortcode tag, which requires access to global scope at a time when shortcodes are being parsed. Using URL parameters decouples the data source from the form definition logic. The form remains static in your contact-form-7/contacts.php or similar file, while the dynamic data is passed via the request line.

Implementing Session Data via URL

Since you have a value stored in $_SESSION, you need to expose that session variable as a URL parameter before the user reaches the form. This typically happens after an action (like logging in or completing a checkout step) where you redirect the user.

In your redirect logic, construct the link by appending the session value:

// Example within a function that handles a previous action
$session_value = $_SESSION['my_custom_variable'];
$redirect_url = add_query_arg('your_name', $session_value, home_url('/contact-us/'));
wp_redirect($redirect_url);
exit;

Note the use of add_query_arg(). This ensures the parameter name matches exactly what is defined in your Contact Form 7 field. If your form uses [text* your-name], the URL parameter must be your_name (converted to kebab-case or snake_case depending on your specific CF7 setup, but standard practice often aligns with the slug used in the shortcode).

Handling the Form Submission

Once the user arrives at the contact page via this redirect, Contact Form 7 will automatically detect the your_name parameter and populate the hidden input or text field accordingly. You can verify this by inspecting the network tab or simply viewing the rendered HTML source; the value should appear without requiring any additional PHP code inside the form file.

When the user submits the form, the data is sent to your contact-form-7/contacts.php (or custom handler). Inside that handler, you will receive the pre-filled value in the standard $_POST or $_GET array depending on whether you configured the plugin to submit via GET or POST.

If your goal is to capture this data specifically for processing, ensure your handler checks both sources:

// In your form submission handler
$name = isset($_POST['your_name']) ? $_POST['your_name'] : ($_GET['your_name'] ?? '');
// Process $name as needed

Security Considerations and Pitfalls

While using URL parameters is a clean solution, it introduces security considerations you must address. Since the value comes from $_SESSION, it is inherently trusted within your session context, but passing it to a public form requires sanitization if that data ever touches a database or email body. Always sanitize inputs before storing or displaying them outside the immediate scope of the form processing.

Additionally, be aware of URL length limits. If $_SESSION['my_custom_variable'] contains a very long string, appending it to the URL might hit browser or server constraints (typically around 2048 characters for GET requests). For large payloads, consider using a hash or storing the data in a cookie or temporary database record linked by a token instead of passing it directly in the URL.

Finally, avoid hardcoding logic inside the form definition file that relies on $_SESSION. The Contact Form 7 engine does not automatically provide session variables to shortcodes in all configurations, and relying on global state within shortcode tags is fragile across different WordPress environments and plugin versions.

Our network