How to create a custom URL based on dropdown in Wo

2019-03-02 13:15发布

问题:

I need to apend a certain word to the URL based on which option from a dropdown is selected, to create a custom confirmation screen URL. It doesn't matter which contact form I use (Contact Form 7, Gravity, etc). I've been looking online for solutions to this and found one that may help:

header( "Location: http://mysite.com/result/?" . $_POST['dropdown_name'] );

but I'm not sure where to put it. In Contact Form 7's submissions.php, I have this:

<input type="text" name="submit" class="tag" readonly="readonly" onfocus="this.select()" />

Could I use that in here somehow to redirect to a page based on the dropdown value selected? I am ok with pre-defining the URL values to be passed.

回答1:

A dropdown is a <select> element not an <input>.
Of course this can be done I would recommend making a small plugin for this and adding the redirect to the init action something like this.

<?php
/*
Plugin name: redirect on post
Desciption: http://stackoverflow.com/questions/13686245/how-to-create-a-custom-url-based-on-dropdown-in-wordpress-form-submission
*/ 
function redirect_on_submit() {
  // check if the post is set
  if (isset($_POST['dropdown_name']) && ! empty ($_POST['dropdown_name'])) {
    header( "Location: http://mysite.com/result/?" . $_POST['dropdown_name'] );
  }
}
add_action('init', redirect_on_submit);

add this to a new file in the plugin folder and activate it in the plugin menu.