how to block direct access using url to multiple thank you pages? - redirect

I have multiple contact forms in my wordpress website and have a different thankyou page for each. So, i want to prevent direct access of thankyou pages by just entering urls. I want if the user fills the form than it is only redirected to that forms thankyou page.
I have working code for 1 thankyou page but i don't know how to set up for multiple contact forms & thankyou pages.
if ( ! is_page(1911)) {
return;
}
// coming from the form, so all is fine
if (wp_get_referer() == 'https://www.example.com/contact') {
return;
}
// we are on thank you page
// visitor is not coming from form
// so redirect to home
wp_redirect( get_home_url() );
exit;
} );
Other thankyou page id's: 1269, 1825, 1623
Other contact form page urls: https://www.example.com/contact-form-2, https://www.example.com/contact-form-3, https://www.example.com/contact-form-3

I have solved the issue by myself. I am posting the solution here in case if someone gets the same problem.
function wpse15677455_redirect() {
$ref = wp_get_referer();
if (is_page(1911) && $ref !== "https://www.example.com/contact"){
wp_redirect( get_home_url() );
}
else if(is_page(1269) && $ref !== "https://www.example.com/contact-form-2"){
wp_redirect( get_home_url() );
}
else if(is_page(1825) && $ref !== "https://www.example.com/contact-form-3"){
wp_redirect( get_home_url() );
}
else if(is_page(1623) && $ref !== "https://www.example.com/contact-form-4"){
wp_redirect( get_home_url() ); exit();
};

Related

Redirect to different thank you pages on specific shipping method selection

I have two different shipping method on my WooCommerce site COD & local pickup, I want to redirect on two different thank you page depending on which shipping method the buyer choose. I tried this but its showing an error after purchase
add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && !empty( $wp->query_vars['order-received'] )) {
if( $order->has_shipping_method('flat_rate:21') ){
wp_redirect( 'mysite.com/thank-you-1/' );
exit;
}
elseif( $order->has_shipping_method('local_pickup:24') ) {
wp_redirect( 'mysite.com/thank-you-2/' );
exit;
}
}
}
You can use woocommerce_thankyou
add_action( 'woocommerce_thankyou', 'woo_custom_redirect_after_purchase');
function woo_custom_redirect_after_purchase( $order_id ){
$order = wc_get_order( $order_id );
if( $order->has_shipping_method('flat_rate:21') ){
wp_redirect( 'mysite.com/thank-you-1/' );
exit;
}elseif( $order->has_shipping_method('local_pickup:24') ) {
wp_redirect( 'mysite.com/thank-you-2/' );
exit;
}
}

Gravity Forms Multi Page Losing POST value

I have a complex Gravity Form built, it has 10 pages. I am using fields to build a "string" that I then match to a CPT name to get meta data from to display a selection, based on user choices in the form.
One field I have is not holding its value in POST. I can see it when I select the value on the page, then when I click to next page the value is still there. However, after two pages the value ( and field ) disappear from POST.
This is the function I have put together that builds my product string.
add_filter( 'gform_pre_render_12', 'display_choice_result' );
function display_choice_result( $form ) {
$current_page = GFFormDisplay::get_current_page( $form['id'] );
$html_content = "";
$prod_string = "";
if ( $current_page >= 10 ) {
foreach ( $form['fields'] as &$field ) {
// Check for a class of "product-builder-item" on the field
// I use this as another way to denote what fields to add to string
if ( strpos( $field->cssClass, 'product-builder-item' ) === false ) {
continue;
}
//gather form data to save into html field (Field ID 14 on Form ID 12)
//exclude page break and any hidden fields
if ( $field->id != 14 && $field->type != 'page' ) {
$is_hidden = RGFormsModel::is_field_hidden( $form, $field, array() );
$populated = rgpost( 'input_' . $field->id );
// Make sure the field we are getting the value from is not hidden and has a value
if ( !$is_hidden && $populated !='' ) {
$html_content .= '<li>' . $field->label . ': ' . rgpost( 'input_' . $field->id ) . '</li>';
$prod_string .= rgpost( 'input_' . $field->id );
}
}
}
// Do a bunch of stuff here with the $prod_string variable
// ...
// ...
// ...
}
return $form;
}
Screenshots showing the POST disappearing..The POST field in question is input_22 with a value of 18000
This is one page after I choose from the field
This is two pages after,
Anyone run into this before or have any idea why it would be disappearing?
Thank you.
I was having the same exact issue as you described. I realized that a jQuery function was interfering with Gravity Form process. The jQuery function was set to change a zip code field from type text to tel so the number pad would open up on mobile devices. This is what was causing my issue.

Is it possible to hide form from non-registered users?

I need to hide form with some shortcode [contact-form-7 id="3080"] from non-registered users in WordPress.
So i've tried to use inserted tags like this '[client][contact-form-7 id="3080"][/client]' and it doesn't work.
with this php code
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'access', 'access_check_shortcode' );
This one isn't interesting, cause i need to show it inside the template
<?php
if ( is_user_logged_in() )
echo do_shortcode( '[contact-form-7 id="1234" title="Contact form 1"]' );
?>
Are you willing/able to install third party plugins? If so, you might want to check out either or both of these:
Hide This (https://wordpress.org/plugins/hide-this/)
Eyes Only (https://bs.wordpress.org/plugins/eyes-only-user-access-shortcode/)
Both of these work by enabling shortcode that can be wrapped around specific content. I believe both have options to how only to logged-in users.

How can i redirect to previous page after a form submit in codeigniter

I have a test function in controller which generates a form page.
public function testing()
{
$this->form_validation->set_rules('test', 'TEST', 'required');
if ($this->form_validation->run()) {
redirect($this->agent->referrer());
} else {
$data['title'] = 'Testing';
$data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->load->view('templates/header', $data);
$this->load->view('pages/nonmember/testing', $data);
$this->load->view('templates/footer');
}
}
The code of testing view as below
<div id="message"><?php echo $message; ?></div>
<?php echo form_open(base_url()."testing");?>
<p>
<?php echo form_input('test');?>
</p>
<p><?php echo form_submit('submit', 'Submit');?></p>
<?php echo form_close();?>
If users come from another page lets say About page to testing page i want after submitting form properly they get redirected back to About page. But it is not being possible as referer agent is staying at the testing page, so after submit it is staying there and not redirecting to About page.
All you need is:
redirect($_SERVER['HTTP_REFERER']);
add this line in your controller's testing function
$this->session->set_flashdata('url',$this->agent->referrer());
change this in the function testing
if ($this->form_validation->run()) {
redirect($this->session->flashdata('url'));
} else {
$this->session->set_flashdata('url',$this->agent->referrer());
//load the view
}
...
or you can also submit an hidden field like,
form_hidden('rURL', $this->agent->referrer());
then simply use its value in the controller.
if ($this->form_validation->run()) {
redirect($this->input->post('rURL'));
} else {
...
Not sure if it is the best solution or not , but i found the solution working perfectly.
We need to write the referrer address in a file and to redirect we need to read from that file.
first we need to load the file helper.
$this->load->helper('file');
if ($this->form_validation->run())
{
redirect(read_file('referrer.php'));
}
else
{
$referrer = $this->agent->referrer();
//need to check wrong input submission and page refresh
if ($referrer != "http://localhost/CIpractice2/testing" && !empty($referrer))
{
write_file('referrer.php', $referrer);
}
//show the form
}
Please refer to codeIgniter's file helper documentation

Symfony 1.4 Validation

Can anyone tell me how could i validate the data on the other page (where was not created the form object)?
The thing is: on the page 'A' i am creating the form object with its own validators and showing the form to the user. But the action goes to the page 'B', where i need to validate the data.
I want to do something like this (page 'B'):
$form = new someForm();
$form->bind($this->getRequest()->getParameter('data'));
if($form->isValid())
{
print 'true';
}
else
{
print 'false';
}
But as you can imagine, it will print 'false'.
I guess it happens due to CSRF protection of forms in Symfony
Try to use this code
$form = new someForm();
$form->disableLocalCSRFProtection();
$form->bind($this->getRequest()->getParameter('data'));
if($form->isValid())
{
print 'true';
}
else
{
print 'false';
}
maybe you could solve this like:
public function executeFoo($request){
$this->form = new fooForm();
$this->getUser()->setAttribute('tmpForm', $this->form);
}
in your form the action has to point to module/bar
there you can do:
public function executeBar($request){
$this->forward404Unless($form = $this->getUser()->getAttribute('tmpForm'));
$form->bind($this->getRequest()->getParameter('data'))
// and so on
}