How can I redirect the contact form in drupal 7 to a thank you page? There are modules to do this ini drupal 6 but not for drupal 7 and I cant find how to do this with rules, there is no option for a rule when submitting the contact form. Thank you.
Create your custom module with hook_form_alter implementation. Then type something like
$form_state['#redirect'] = 'path/to/redirect';
Hope this helps.
try something like this ,
<?php
function mymodule_form_alter(&$form, $form_state, $form_id){
if($form_id == 'user_register') {
$form['#submit'] []= 'mymodule_node_form_submit_handler';
}
}
function mymodule_node_form_submit_handler($form, &$form_state) {
unset($_REQUEST['destination']);
unset($form['#redirect']);
$form_state['redirect'] = 'http://google.com';
}
?>
Related
I am facing a problem when I try to use hook_form_alter to hide fields according to user's roles. I have used unset() and removed the field from the $form array, but it is still showing when the form is rendered.
Here is my code:
function mymodule_form_alter($form, $form_state, $form_id){
global $user;
if($form_id == 'my_content_type'){
if(array_key_exists(5,$user->roles) && !array_key_exists(3,$user->roles)){
if(empty($form_state['field']['args'][0]->title)){
unset($form['field_body']);
}
}
}
}
Instead of using unset() to hide a form element, you should set the #access property to FALSE. This keeps the form build tree intact, which avoids problems if other modules try to access or alter that information. Source
function MYMODULE_form_alter($form, $form_state, $form_id) {
global $user;
$account = $user;
if ($form_id == 'MYCONTENTTYPE_node_form') {
if (user_has_role(5, $account) && !user_has_role(3, $account)) {
if (empty($form_state['field']['args'][0]->title)) {
$form['field_body']['#access'] = FALSE;
}
}
}
}
If this is still not working, double-check your if-requests. Are they really doing something? Are you currently logged-in as a corresponding user?
I have found the solution. I need just added the & with $form and $form_state in hook_form_alter parameters. Like hook_form_alter(&$form, &$form_state, $form_id)
It exists a module which allows to hide fields. For Drupal 9 it handles per-roles, hide-or-disable, exceptions (such as not at creation…).
Look at: https://www.drupal.org/project/jammer
I have put a "Global:link" in the header of a view in Drupal 7. I suppose I could put the link as html in a Global:text area as well.
If the user is not admin I don't want them to see this link. So I have tried to put this code in my themes template.php:
// hide global text area in view header if user is not admin
function mytheme_views_pre_render(&$view) {
if ($view->name == 'taxonomy_term') {
dpm($view->name);
global $user;
// Check to see if $user has the administrator role or not.
if (!in_array('administrator', array_values($user->roles))) {
$header_item = $view->display_handler->get_option('header');
dpm($header_item['link']);
unset($header_item['link']);
}
}
}
}
.. but how do I unset a global field in the header of this specific view?
My code above does not do the trick.
Any help would be much appreciated!
SOLVED. I finally solved it. Here's the snippet. Hope it helps somebody having the same issue. Change "link" to whatever item you use in the header:
function mytheme_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'taxonomy_term') {
global $user;
$new_item = $view->get_item('page', 'header', 'link');
$new_item['text'] = "";
// Check to see if $user has the administrator role or not.
if (!in_array('administrator', array_values($user->roles))) {
$view->set_item('page', 'header', 'link', $new_item);
}
}
I'm using contact form 7 to load two different forms into a page and then, in addition to sending the email, dynamically adding that information to a database. Unfortunately, because of the plugin, I can't simply just create all inputs with different names to avoid needing a filter. So, essentially, I'd like to pull the form ID into the action hook and dynamically create the $data variable based on which form is being submitted, but I'm not sure how to get the cf7 form ID. Does anyone know how to accomplish this, or perhaps a more feasible way of doing it?
Form Shortcodes
[contact-form-7 id="221" title="Reg 1"] [contact-form-7 id="112" title="Reg 2"]
PHP Action Hook in functions.php
function save_form( $wpcf7 ) {
global $wpdb;
$form_to_DB = WPCF7_Submission::get_instance();
if($form_to_DB) {
$formData = $form_to_DB->get_posted_data();
}
if("Request a Free Demo" != $formData['demo_request'][0]){
$freeDemo = "yes";}else { $freeDemo = "nope";}
if(THE FORM ID = 221) {
$data = array(
some values from the 112 form
$wpdb->insert( $wpdb->prefix . 'registrations', $data );
);
}elseif(THE FORM ID = 112) {
$data = array(
some other values from the 112 form
$wpdb->insert( $wpdb->prefix . 'registrations_2', $data );
);
}
}
remove_all_filters('wpcf7_before_send_mail');
add_action('wpcf7_before_send_mail', 'save_form' );
I tend to use the "wpcf7_posted_data" action hook (though this might have changed as the question is a bit old now). You don't need to remove all filters.
For example:
function processForm($cf7)
{
$wpcf7 = WPCF7_ContactForm::get_current();
$form_id = $wpcf7->id;
if ($form_id === 221)
{
//Do Stuff
}
else if ($form_id === 112)
{
//Do different stuff
}
}
add_action("wpcf7_posted_data", "processForm");
$wpcf7->idis no longer accessible, use $wpcf7->id() instead.
simply use this:
function save_form( $wpcf7 ) {
if($wpcf7->id == 4711) {
// whatever
}
}
You can use this: $form_id = $_POST['_wpcf7'];
SOLVED:
I wound up just using a logical operator to check if a form specific field was empty or not. If the field "form_2_name" was empty when a form was submitted, then we know that form 1 is being submitted. Simple if statement with that logic did the trick!
I have a views block (Views 3 / Drupal 7) with a exposed filter form and the ajax mode is enabled. It works fine. I added in hook_form_alter() a validation function. It works too, but the form_set_error message shows only on a page refresh. How can I set it up the message without page reload?
function hook_form_alter(&$form, &$form_state, $form_id) {
if($form['#id'] === 'id_from_views') {
array_unshift($form['#validate'], '_custom_form_validate');
}
}
function _custom_form_validate($form, &$form_state) {
if(!empty($form_state['values']['field'])) {
form_set_error('field', t('Custom error message.'));
}
}
I had a similar problem. The answer is to use ajax callback in the hook_form_alter.
function hook_form_alter(&$form, &$form_state, $form_id) {
if($form['#id'] === 'id_from_views') {
$form['submit']['#ajax'] = array('callback' => '_custom_form_validate');
}
}
Cannot find where to add validation for a checkbox on wordpress login form. I have an additional checkbox set up called 'terms' that I need the user to check each time they want to log in.
Problem is that I cannot stop wordpress logging in if they don't check it. Where it the login code.
There is also a plugin installed that may be complicating matters called them-my-login.
I have all the code in front of me, just tell me what I'm looking for.
I know this is fairly old but I just stumbled across it and was able to look at the codex and work out a solution. Hope this helps somebody. Thanks to #Jason for pointing in the right direction.
This code will need to be added to your theme's functions.php file:
<?php
// As part of WP authentication process, call our function
add_filter('wp_authenticate_user', 'wp_authenticate_user_acc', 99999, 2);
function wp_authenticate_user_acc($user, $password) {
// See if the checkbox #login_accept was checked
if ( isset( $_REQUEST['login_accept'] ) && $_REQUEST['login_accept'] == 'on' ) {
// Checkbox on, allow login
return $user;
} else {
// Did NOT check the box, do not allow login
$error = new WP_Error();
$error->add('did_not_accept', 'You must accept the terms and conditions' );
return $error;
}
}
// As part of WP login form construction, call our function
add_filter ( 'login_form', 'login_form_acc' );
function login_form_acc(){
// Add an element to the login form, which must be checked
echo '<label><input type="checkbox" name="login_accept" id="login_accept" /> I agree</label>';
}
The answer Patrick Moore gave did not work for me, but I did modify it to provide a valid solution. This may be because he answered it back in 2013, and now the code has changed. I changed the filter to login_form_middle, and modified the function at the end as a variable, and then passing the value back through a return:
<?php
// As part of WP authentication process, call our function
add_filter('wp_authenticate_user', 'wp_authenticate_user_acc', 99999, 2);
function wp_authenticate_user_acc($user, $password) {
// See if the checkbox #login_accept was checked
if ( isset( $_REQUEST['login_accept'] ) && $_REQUEST['login_accept'] == 'on' ) {
// Checkbox on, allow login
return $user;
} else {
// Did NOT check the box, do not allow login
$error = new WP_Error();
$error->add('did_not_accept', 'You must accept the terms and conditions' );
return $error;
}
}
// As part of WP login form construction, call our function
add_filter ( 'login_form_middle', 'login_form_acc' );
function login_form_acc(){
// Add an element to the login form, which must be checked
$termsLink = '<label><input type="checkbox" name="login_accept" id="login_accept" /> I agree</label>';
return $termsLink;
}
WordPress Filters/Actions are your friend.
Take a look at:
admin_init
wp_login