0

what I have is a form:

<form method="POST" action="/path1/path2/">
<input type="hidden" name="usr" value="firstname-lastname">
<input type="image" src="/pathtoimage/image.jpg" alt="Submit Form" />
</form>

What I would like is once the form is submitted to /path1/path2/ firstname-lastname get appended to the end of the url, so I end up with /path1/path2/firstname-lastname. In most cases I could do something with a .htaccess rewrite rule but this is WordPress so all I get is a 404. Where should I start? I see some posts that point to changes in the functions.php file but I can't seem to get anything to work.

Your help is much appreciated.

Datumpup
  • 1
  • 1
  • If you want the form to append to the url, change the form method from `POST` to `GET` and it will handle automatically for you. – Jonathan Kuhn Nov 06 '14 at 18:16
  • When I use GET I end up with /?usr=firstname-lastname – Datumpup Nov 06 '14 at 18:44
  • Maybe GET is the way to go - but how do I get rid of ?usr= without getting a 404? – Datumpup Nov 06 '14 at 19:22
  • You would need to use javascript and change the form action so the form submits in the format you want. You would usually add an onSubmit event and change it then. – Jonathan Kuhn Nov 06 '14 at 19:37
  • Thanks Jonathan, do have a any code I could try or a link to an example? – Datumpup Nov 06 '14 at 19:47
  • This has a few examples that show how to change the action of a form: [examples](http://stackoverflow.com/questions/2701041/how-to-set-form-action-through-javascript). – Jonathan Kuhn Nov 06 '14 at 20:07

3 Answers3

0

You can use add_rewrite_rule of WP (http://codex.wordpress.org/Rewrite_API/add_rewrite_rule)

Example:

function wptuts_add_rewrite_rules() {
    add_rewrite_rule('^path-1/path-2/([^/]*)?$','index.php?page_id=PAGE_ID&nameSurname=$matches[1]','top');
    flush_rewrite_rules();
}
add_action( 'init', 'wptuts_add_rewrite_rules' );

function add_query_vars($aVars) {
    $aVars[] = "nameSurname"; 
    return $aVars;
}

add_filter('query_vars', 'add_query_vars');
LTasty
  • 2,008
  • 14
  • 22
  • You get the params in the page using $wp_query->query_vars["nameSurname"] – LTasty Nov 06 '14 at 18:25
  • Not sure I understand in the context of my post. I've changed nameSurname to usr but i still do not get the usr variable to append to the end of the url. – Datumpup Nov 06 '14 at 18:40
  • Maybe i don't understand your problem... With my code you fix the 404 with the path path1/path2/usr – LTasty Nov 06 '14 at 18:59
  • My original code works w/o making any changes to the functions.php file. The POST is sent to a template where I use $_POST['usr'] to get what I need to display in the template. What I need is the $_POST['usr'] variable to be appended to the url w/o getting a 404. – Datumpup Nov 06 '14 at 19:05
0

Here is a simple example using vanilla javascript:

<script type="text/javascript">
function changeAction(form){
    //get the value from the input
    var fl = document.getElementById("usr").value;
    //add the input to the action
    frm.action = "/path1/path2/" + fl;
}
</script>

<form method="POST" action="/path1/path2/" onSubmit="changeAction(this);">
<input type="hidden" name="usr" id="usr" value="firstname-lastname">
<input type="image" src="/pathtoimage/image.jpg" alt="Submit Form" />
</form>

All I really did was give the input an id so it can be selected, added the onSubmit event to the form tag and then added the script tag with function to handle changing the form action.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • That is up to you. In all honesty, if you only have one field, I personally wouldn't be using a form at all. I would just add an onclick event to an `` that gets the value from an input and simply does `location.href="/path1/path2/" + inputBoxValue`. – Jonathan Kuhn Nov 06 '14 at 21:20
  • All I have is one field. How does that change the code above? – Datumpup Nov 06 '14 at 21:35
  • I just realized this was a hidden form field so it isn't even displayed to the user. Is there a reason you don't just show an `` and make it a link? – Jonathan Kuhn Nov 06 '14 at 21:46
  • There is no reason it needs to be hidden or, as you mentioned, a form. Let me rephrase the question: I have a link - click here. All I want is to pass the usr variable but remove the ?usr= portion of the url, so I end up with /path1/path2/fname-lname. I still need to be able to use the usr variable and the link is on the same page that processes the usr variable. Thanks for your help so far. Oh and this is WordPress so /?usr= works but /fname-lname results in a 404. – Datumpup Nov 06 '14 at 22:23
0

a alternative way.....you need to build a url to redirect to. The problem with the way you are trying is that wp will obviously try and resolve the address you give it. If you want to overcome this you can set a rewrite rule to match path1/path2 (see ltasty answer above if your page exists in the db or here Add "custom page" without page if you want to skip the db entry)

Once you have that sorted you can build the url path1/path2/name and redirect to it when $_POST is received or You could of course change the form action to path1/path2 again and redirect to itself from there.

<?php
if($_POST):
  $name=sanitize_text_field($_POST['usr']);
  $url= get_site_url().'/path1/path2?name='.$name;// using query var method..change if you decide to use the rewrite rule. 
  wp_redirect($url);
  exit();
endif;

?>
<form method="POST" action="#">
<input type="hidden" name="usr" value="firstname-lastname">
<input type="image" src="/pathtoimage/image.jpg" alt="Submit Form" />
</form>

you can use $_SERVER['REQUEST_URI'] to pull the url and str_replace the part you dont need

Community
  • 1
  • 1
David
  • 5,897
  • 3
  • 24
  • 43