简体   繁体   English

基于表单隐藏字段的WordPress重写

[英]WordPress Rewrite based on form hidden field

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. 我想要的是将表单提交到/ path1 / path2 / firstname-lastname后附加到url的末尾,所以我最终得到了/ 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? 在大多数情况下,我可以使用.htaccess重写规则来执行某些操作,但这是WordPress,所以我得到的只是404。我应该从哪里开始? I see some posts that point to changes in the functions.php file but I can't seem to get anything to work. 我看到一些帖子指出了functions.php文件中的更改,但似乎什么也没用。

Your help is much appreciated. 非常感谢您的帮助。

You can use add_rewrite_rule of WP ( http://codex.wordpress.org/Rewrite_API/add_rewrite_rule ) 您可以使用WP的add_rewrite_rule( 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');

Here is a simple example using vanilla javascript: 这是使用香草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. 我真正要做的就是为输入提供一个ID,以便可以选择它,将onSubmit事件添加到form标记,然后添加具有功能的script标记来处理更改表单动作。

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. 您尝试方式的问题在于,wp显然会尝试解析您提供的地址。 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) 如果要解决此问题,可以设置一个重写规则以匹配path1 / path2(如果您的页面存在于db中,请参见上面的ltasty答案;如果您要跳过db条目,请在此处添加没有页面的“自定义页面”

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. 完成排序后,您可以构建URL path1 / path2 / name并在收到$ _POST时重定向到它,或者您当然可以再次将form action更改为path1 / path2并从那里重定向到它自己。

<?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 您可以使用$_SERVER['REQUEST_URI']提取网址并str_replace不需要的部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM