WooCommerce: pre populate registration fields with parameters from URL - php

I'm using a small form (Gravity Forms) on my site which asks for first name, last name and e-mail. After sending the form it redirects to the WooCommerce registration page and adds some parameters to the URL.
The URL looks like this: https://example.com/?first_name=First&last_name=Name&email=info%40example.com
Now I've changed the WooCommerce Template form-login.php with this code (for the e-mail field):
<?php $first_name = $_GET['first_name']; ?>
<?php $last_name = $_GET['last_name']; ?>
<?php $email = $_GET['email']; ?>
<div class="form-group">
<label for="reg_email"><?php _e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="form-control" name="email" id="reg_email" value="<?php if ( ! empty( $_POST['email'] ) ) echo esc_attr( $_POST['email'] ); ?><?php if ( !empty($email) ) : echo $email; endif; ?>" placeholder="info#example.com" />
</div>
It works for this field because I can change it. (Is it ok how it's done?)
The problem is, I can't see the fields for first and last name in the template.
Therefore I don't know how to change them or add the parameter.
Is there any trick?

I could solve the problem... My mistake!
I've added the fields for first and last name like in this tutorial: https://businessbloomer.com/woocommerce-add-first-last-name-account-register-form/
Now I could add the paramaters like above.

Related

Include Wordpress Login Form into a Page to protect it

hope you can help me and I hope I write it correct:
I've a WP Page with content. If a user isn't logged in, a login form with the possibility to register should be shown. after login, the content of the page should be shown, The rest of my WP Pages doesn't need this.
I've tried this:
<form style="text-align: left;" action="http://fragandy.com/mein-konto/" method="GET">
' . $errors . '
<p>
<label for="log"><input id="user_login" name="log" class="input" type="text" placeholder="Type Username or Email" style="padding: 5px 5px 5px 45px;"> '
. apply_filters( 'pr_username_label', 'Username' ) . '</label><br />
<label for="pwd"><input id="user_pass" name="pwd" class="input" type="password" placeholder="Type Password" style="padding: 5px 5px 5px 45px;"> '
. apply_filters( 'pr_password_label' , 'Password' ) . '</label><br />
<input type="submit" name="submit" value="Log In" class="button" />
<label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label><br />
</p>
<input type="hidden" name="redirect_to" value="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" />
</form>
Any ideas ??
Thanks
Andy
If using the native Password Protection won't suit your needs, you have a few options, despite the fact there are definitely some plugins that do this, but I can't recommend any of them specifically.
Option 1: Filter the_content based on the post_id or slug.
Basically, you can use the the_content filter and conditionally filter out the content and replace it with a login form and/or login/register links. Here's a snippet that will activate on post id 123 (replace that with the ID of your actual page). If you need a one-and-done solution, I'd probably go this route:
function require_login_for_content( $content ){
global $post;
if( $post->ID == 123 ){
if( ! is_user_logged_in() ){
$redirect = get_permalink();
$content = sprintf('<h4>Please Log In or Register to view this content.</h4>', wp_login_url( $redirect ), wp_registration_url() );
$content .= wp_login_form( array( 'redirect' => get_permalink(), 'echo' => false ) );
}
}
return $content;
}
add_filter( 'the_content', 'require_login_for_content' );
Option 2: Create a Page Template.
This may make more sense if you need to have this available on more than one page without editing code or managing an array of post ids. Go in to your theme and copy your page.php or equivalent file, and name it something like page-login-required.php. Now, depending on your theme, the exact methodolgy will be different. I'll use the TwentySeventeen theme as an example.
Add the following line to the comment section at the top: Template Name: Login Required so it looks something like:
<?php
/**
* Template Name: Login Required
*
* The template for displaying all pages
…
This will allow you to select it as a Page Template named "Login Required" in the page editor on the right hand side of the page. Now, where the page pulls in the content ( again, this will be different for every theme ), in this case on what's now line 30, get_template_part( 'template-parts/page/content', 'page' );.
You can wrap that with the is_user_logged_in() function and make use of the wp_login_form():
while ( have_posts() ) :
the_post();
if( is_user_logged_in() ){
get_template_part( 'template-parts/page/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
} else {
printf('<h4>Please Log In or Register to view this content.</h4>', wp_login_url( $redirect ), wp_registration_url() );
wp_login_form( array( 'redirect' => get_permalink() ) );
}
endwhile; // End of the loop.
Now save that and select it as the page template, and you should be good to go.
You can also do something similar using Shortcodes, but I think it makes more sense to keep the content blocking done in the template.
There's a few other things you could do, but I'd evaluate the above options before weighing in any other ones.

Hook for post-Wordpress/Woocommerce registration to check form data & add function

What I have:
A log in page created by WooCommerce
A check box added to it by MC4WP (Mailchimp for Wordpress)
Roles managed by Members plugin
What I am trying to do:
When a user registers, if they have selected the check box, I would like to assign them an additional role. (NOT update their role from the default woocommerce Customer to something else; trying to add a role in addition to that).
I have assigned multiple roles with another form successfully, but without the added step of the checkbox or woocommerce's form.
I've tried hooking user_register, profile_update, even woocommerce_created_customer, but none so far have worked.
I'm not sure where my issue is, but this I have found:
When including the checkbox name as taken from source code, I get a 500 error after form submission. User is registered (with customer role only).
If I put in a junk name there, no error.
I'm not sure what I've misunderstood (or just completely missed).
Any help/thoughts are appreciated!
//add_action( 'user_register', 'check_the_form_box_add_role');
//add_action('woocommerce_created_customer', 'check_the_form_box_add_role');
add_action('profile_update', 'check_the_form_box_add_role');
function check_the_form_box_add_role( $user_id ) {
if ( isset($_POST['_mc4wp_subscribe_wp-registration-form']) ) {
$user_id = wp_insert_user( $user_id );
$role_to_add = 'thisistherole;
$user_id->add_role($role_to_add);
}
}
The form from source code:
<form method="post" class="register">
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="reg_email">Email address <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" value=""/>
</p>
<div style="left: -999em; position: absolute;"><label for="trap">Anti-spam</label><input type="text" name="email_2" id="trap" tabindex="-1"/></div>
<input type="hidden" name="_mc4wp_subscribe_wp-registration-form" value="0"/><p class="mc4wp-checkbox mc4wp-checkbox-wp-registration-form"><label><input type="checkbox" name="_mc4wp_subscribe_wp-registration-form" value="1" checked="checked"/><span>This is text for checkbox!</span></label></p>
<p class="woocomerce-FormRow form-row">
<input type="hidden" id="_wpnonce" name="_wpnonce" value="994ca0c618"/><input type="hidden" name="_wp_http_referer" value="/my-account/"/> <input type="submit" class="woocommerce-Button button" name="register" value="Register"/>
</p>
</form>
I thought about hooking registration_errors, but this runs before the user is created. Previously, I would create user as Customer and use something like
$user_id = wp_insert_user( $user_id );
$role_to_add = 'thisistherole;
$user_id->add_role($role_to_add);
to add the additional role.
If anyone needs it:
I was correct previously hooking user_register.
The issue was I was not getting a WPUser object and was thusly calling add_role() when it wasn't available. Passing the id to get_userdata seems to have solved it.
add_action('user_register', 'name_of_function', 10, 1);
function name_of_function( $user_id ) {
if ( isset($_POST['_mc4wp_subscribe_wp-registration-form']) &&
$_POST['_mc4wp_subscribe_wp-registration-form'] == '1')
{
$useridid = get_userdata( $user_id );
$role_to_add = 'thisistherole';
$useridid->add_role($role_to_add);
}
else
{
//do nothing
}
}
More on get_userdata: https://codex.wordpress.org/Function_Reference/get_userdata
and on user_register:
https://codex.wordpress.org/Plugin_API/Action_Reference/user_register

A form is displaying as filled in, instead of blank, using PHP

I am using an add on with Wordpress and it appears the PHP code is somehow calling my admin e-mail to autofill a form that is supposed to show up as blank.
Unfortunately for me, PHP is WAY over my head and I don't want to break everything.
<div class="form-group">
<label for="stc-email"><?php _e( 'E-mail Address: ', 'stc_textdomain' ); ?></label>
<input type="text" id="stc-email" class="form-control" name="stc_email" value="<?php echo !empty( $email ) ? $email : NULL; ?>"/>
</div>
I just want the form to be blank, but it's autofilling with the admin e-mail on the Wordpress account. I've targeted the line of code, I think it has something to do with this bit:
value="<?php echo !empty( $email ) ? $email : NULL; ?>"/>
But I don't think
!empty
is working, nor is NULL, could anyone help out please?

codeigniter post html textarea

when i try to post html from my textarea the variable value is empty and form_validation show error message that the input is required......
i search for solution and i didn't found ....
this is my code
<form accept-charset="utf-8" method="post" action="<?php echo base_url('send_message/validate'); ?>">
<?php
echo validation_errors();
echo form_dropdown('cat_name', $plan_cat);
?>
<div>
<span><label>اسم الراسل باللغه الانجليزية</label></span>
<span><input name="from" type="text" class="textbox" value="<?php echo set_value('from'); ?>" /></span>
</div>
<div>
<span><label>العنوان</label></span>
<span><input name="subject" type="text" class="textbox" value="<?php echo set_value('subject'); ?>" /></span>
</div>
<div>
<span><label>الرساله</label></span>
<?php
/*
$options = array(
'name' => 'message',
'id' => 'elm1'
);
echo form_textarea($options);
*/
?>
<textarea name="message" id="elm1"><?php echo set_value('message'); ?></textarea>
</div>
<div>
<span><input type="submit" value="ارسل" /></span>
</div>
<?php
echo form_close();
?>
and this is my controller code :
$this->load->library('form_validation');
$this->form_validation->set_rules('from', 'الراسل', 'required|min_length[8]');
$this->form_validation->set_rules('subject', 'عنوان الرساله', 'required|min_length[6]');
$this->form_validation->set_rules('message', 'الرساله', 'trim|required|min_length[10]|xss_clean');
if($this->form_validation->run() == false){
$user_id = $this->session->userdata('user_id');
$send = array(
'title' => 'ارسل الرساله',
'plan' => $this->user_plan->get_plan($user_id)
);
$this->load->library('parser');
$this->parser->parse('header', $send);
$this->parser->parse('send_message', $send);
$this->parser->parse('footer', $send);
}else{
$user_id = $this->session->userdata('user_id');
$plan_cat = $this->input->post('cat_name');
$from = $this->input->post('from', true);
$subject = $this->input->post('subject', true);
$message = $this->input->post('message', true);
$message = '<code>'.$message.'</code>';
i hope to find way to solve this :)
First , before going deep in your code , I have to say that : using base_url functionality as action attribute is bad. You have to use site_url instead in this way :
echo site_url('send_message/validate');
The reason stands behind that :
site_url gives you the full URL including the main processing file (index.php) which is important to handle your MVC requests (e.g http://domain.com/codeigniter/index.php/send_message/validate)
base_ur gives you the URL without any consideration for the main file (index.php) (e.g http://domain.com/codeigniter/send_message/validate)
Second (And the important). I see that you are using XSS Filter for the (message) which means you are trying to put illegal content in the textarea. Try to put anything else everything will be ok.
So , make sure that your message value doesn't contain illegal characters.

PHP function to output a form value?

I'm using this piece of code to add a form to a Wordpress taxonomy:
function albums_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'albums' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','albums' ); ?></p>
</div>
<?php
}
add_action( 'albums_add_form_fields', 'albums_taxonomy_add_new_meta_field', 10, 2 );
The value is saving fine. However, how can I output on my template the value user filled out in the form ? What's the php function to use this value in front-end ?
Thanks.
Just taking a guess, on the page where the form is being redirected to try:
echo $_POST['term_meta']['custom_term_meta']

Categories