Wordpress custom register form errors filters and prev values - wordpress

I created a front end registration form, I used the filters 'registration_errors' to customize the messages.
After WP detects the error and use 'wp-redirect' to return to the registration page and display an error if the email or the user exists for example.
My question is: how I can keep the previous values that generated the error.
¿JS?
Thanks in advance!

To keep values in the form after the error message:
function my_register_sesion (){
session_start();
$_SESSION['key_login']=$_REQUEST['user_login'];
$_SESSION['key_email']=$_REQUEST['user_email'];
}
add_action ('register_post', 'my_register_sesion');
My inputs form should be as follows:
<input type="text" name="user_login" id="user_login" class="input" value="<?php echo $_SESSION['key_login'];?>">
<input type="text" name="user_email" id="user_email" class="input" value="<?php echo $_SESSION['key_email'];?>">
Thank you David!

Related

How to generate Joomla login token from outside Joomla

Here's the context. I have a Joomla Backend with tons of custom code in a very old Joomla 1.X version. Everything is still surprisingly holding up well. The site owner wants a new front facing website and his company chose WordPress. Website was built, now we want to add a log in form to the Joomla backend from a WP page.
Here's what worked:
Go to Joomla login page (domain.com/administrator)
Copy the HTML form (including hidden input with token)
Paste the HTML and adjust the action attribute of the form
Went to the WP page (domain.com/wordpressFolder/page, entered credentials and it works perfectly!
Obviously these tokens can only be used once. Added a shortcode in WP that gets the form from Joomla and "extract" the token and returns it to the page.
function st_login_form( $atts ) {
$joomla = file_get_contents('http://www.example.com/administrator/index.php');
$doc = new DOMDocument();
$doc->loadHTML($joomla);
$inputs = $doc->getElementsByTagName('input');
$token = $inputs[5]->attributes[1]->nodeValue;
$html = '<form action="https://www.example.com/administrator/index.php" method="post" name="login" id="form-login" style="clear: both;">
<p id="form-login-username">
<label for="modlgn_username">Username</label>
<input name="username" id="modlgn_username" type="text" class="inputbox" size="15">
</p>
<p id="form-login-password">
<label for="modlgn_passwd">Password</label>
<input name="passwd" id="modlgn_passwd" type="text" class="inputbox" size="15">
</p>
<input type="submit" value="Connexion" />
<input type="hidden" name="option" value="com_login">
<input type="hidden" name="task" value="login">
<input type="hidden" name="'.$token.'" value="1">
</form>';
return $html;
}
The code behaves has expected and inspecting the form on the WP page with injected token looks fine, however when logging in it gives me an invalid token error.
I don't quite understand why it works when copy pasting but not when I retrieve the token from PHP. Any clue or potential solutions?
Found my first mistake. The GET is done over HTTP while the POST is sent over HTTPS. Obviously, CSRF token are domain-signed.
Now it simply redirects me to the login page but I'm not logged in.

Form POST data handling with WordPress

I have hard time figuring out how to recieve form data. This is how my form looks like:
<form action="register" method="POST">
<input type="hidden" name="action" value="process_form">
<input type="email" placeholder="Enter email" name="email">
<input type="password" placeholder="Enter password" name="password">
</form>
How can I access the data of my form?
You should change your form action to something like this:
action="<?= esc_url(admin_url('admin-post.php')) ?>"
And add a hidden input in your form like this:
<input type="hidden" name="action" value="add_foobar">
And then in your backend class add an action like this:
namespace Class\Namespace;
class ClassName {
public function init() {
add_action( 'admin_post_add_foobar', [$this, 'handleForm'] );
}
public function handleForm() {
// your logic here
// use $_POST to retrieve post data
}
....
Make sure to include your class in your functions.php of your theme, like this:
(new \Class\Namespace\ClassName)->init();
To read form data, then in your handleForm method just use $_POST.
For more examples have a look at this page.
After the form has been submitted, you can access it on the page it loads with the PHP $_POST variable.
eg.
$email = $_POST['email'];
Remember to validate and sanitise this variable as it will be what the user has entered.

wpdp update is not working when a form is posted. But can open directly

I am trying to update a custom table row.
Here is my form's code (page-vehicles.php)
<form action="<?php echo home_url( "update" ); ?>" method="post">
<input type="number" name="id">
<input type="number" name="number">
<input type="submit" value="submit">
</form>
When I submit the form to (page-update.php) it shows a 404 error. But if I open the page (page-update.php) directly it shows the page (without 404 error).
What am I doing wrong here? :(
You said the custom page where the update needs to happen is page-update.php but you are posting the form to form.action = "http://novits.com/5050/vedit"; Change the action to the proper link.

set curect address to <form action=">

i create simple plugin wordpress , one validationform.php and rflinsertdb.php
when user click on submit form , i want got rflinsertdb.php the page validation and insert information to db , but wordpress give me Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
this 2 php page in one folder that name in public ,i see to many codes in internet but not help, how can i do that ?
thx alot
i try this codes for action form
<form method="post" action="<?php bloginfo('template_url'); ?>/rflInsertdb.php">
<p id="errorMessage"></p>
<p>name: <input type="text" class="register" name="name" id="name"></p>
<p>family: <input type="text" class="registerForm" id="family" name="family"></p>
<p>numbers :<input type="number" class="registerForm" id="numbers" name="numbers" min="1" max="200" value="1"></p>
<p>tell: <input type="text" class="registerForm" id="tell" name="tell"></p>
<p><input type="submit" value="ثبت" class="registerForm" id="submit" name="submit"></p>
</form>
This happens to you, because you are using template directory for: /rflInsertdb.php
Try to use
<form method="post" action="<?php echo plugin_dir_url( __FILE__ ); ?>/rflInsertdb.php">
If your file is under the public (what is under the plugin dir), then maybe:
<form method="post" action="<?php echo plugin_dir_url( __FILE__ ); ?>/public/rflInsertdb.php">
See here: https://codex.wordpress.org/Function_Reference/plugin_dir_url

Creating wp plugin for external login => post comments

I need to create a wordpress plugin to connect wordpress to a central login. But all I want is the user to be able to post comments with name and email filled. I don't think I need create a real loggin into wordpress because the user should not be able to write posts or do admin stuff. I want him only to post comments.
I search the documentation but could not find any action for comments.
How can I change the html of a comment form?
Maybe not good but it works...
Fill comment author and email from central login stored in session:
function portal_user_comment()
{
$_POST['author'] = $_SESSION['portal']['name'];
$_POST['email'] = $_SESSION['portal']['email'];
}
add_action('pre_comment_on_post', 'portal_user_comment');
Edit comments.php from the template. Look for:
<input type="text" name="author" id="author" value="<?php $comment_author; ?>" ...>
<input type="text" name="email" id="email" value="<?php $comment_author_email; ?>" ...>
Set both fields disabled="true" and replace the values with the author and email from the session.

Resources