I am new to Wordpress but I have some knowledge of PHP. I am creating a bilingual site (with Polylang) which has many forms. Some of them are handled by Contact Form 7 but some others have to be custom made.

So, the first form I made was quite a disaster - it works but under certain circumstances.

In the archives page of a custom post I insert

<form method="post" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="hidden" name="post_type" value="myvalue">
<select class="form-control"  name="k" id="k">
<option value="<?php echo $array3[$i]?>"><?php echo $array3[$i];?></option>
<?php }?>   
<span class="input-group-btn">
<button class="btn btn-search" type="submit"><i class="fa fa-search">  </i></button>

I created a template file named mysearchresults.php In there I inserted the essential (between them)

 echo $_POST['k'];
 echo "Hello world";   

I created a page having as template mysearchresults.php in one language (named mysearchresults) and another in the other language (same template) named mysearchresults-2.

Loading the two pages gives a page with the Hello world printed.


<form method="post" action="<?php echo esc_url( home_url( '/' ) ); ?>mysearchresults-2">

gives 404 (when I press the submit button - having made a selsction) - the same as instead of -2 I put mysearchresults. Putting mysearchresults.php gives the same error.

So I tried another approach, instead of

<select class="form-control"  name="k" id="k"> 

I put

<select class="form-control"  name="s" id="s">

and for action I left

action="<?php echo esc_url( home_url( '/' ) ); ?>">  

In the search page I put echo $_POST['s'] and it loads the search results page printing the value of my selection.

What am I doing wrong? How can I have a custom form, pass the values in my action file and make operations on them? Why leaving like this the action label it loads the search results page?

  • usually, you just need to put a empty action to send the form to the same page. and after the traitement, you redirect to another page – mmm Jul 14 '16 at 11:02

for the action label, there are some reserved terms, that wordpress uses internally. so you should avoid this terms (specially true in http_post ot get requests). 's' stands for search, that's why the output is a search result page. here you can find reserved terms: https://codex.wordpress.org/Reserved_Terms

If you need very simple forms (so it seems in this case), i would suggest you to learn how to append variables to an url with add_query_arg. Here's a good place to start, it's quite simple and ready to use : https://developer.wordpress.org/reference/functions/add_query_arg/

  • Thank you for your answer, I appreciate it, I have tried it and it works. For a more complicated form what should I do? I have seen around examples that pass the variables in an action file (something.php) but in my case it is not working. – user12 Jul 14 '16 at 16:50

You can also look at the polylang extension for contact form 7. It allows you to manage multiple forms in different languages using the familiar polylang translator management tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.