I'm trying to modify the below code snippet / function hook to disable registration if the user is logged in.
<?php
add_filter("gform_disable_registration", "disable_registration", 10, 4);
function disable_registration($is_disabled, $form, $entry, $fulfilled){
//check form id and if not the form being checked status passed in to function
if ($form["id"] != 160)
return $is_disabled;
//check submitted values to decide if registration should be stopped
if ($entry["4"] == "No" && $entry["5"] == "No") {
//disable registration
return true;
}
else{
return false;
}
}
?>
I've tried the following to no avail:
add_filter("gform_disable_registration", "disable_registration", 10, 4);
function disable_registration($is_disabled, $form, $fulfilled){
//check form id and if not the form being checked status passed in to function
if ($form["id"] != 2)
return $is_disabled;
//check user login to decide if registration should be stopped
if( ! is_user_logged_in() ) {
return true;
}
else {
return false;
}
}
Hoping I can get this to work! Thank you.
Here is an article/snippet I wrote to do this... I didn't confirm if this is still the best way to accomplish this, but it certainly is a way that works. :)
http://gravitywiz.com/skip-user-registration-for-logged-in-users/
I believe there's a setting in the Form Settings to allow you require users to be logged in. Is there a reason you can't just use that?
Related
I was wondering why my checkbox fails even after it has already been ticked. It is a checkbox for terms and conditions
Controller:
// Load the registration page
public function registration_page(){
$this->load->view('includes/main_index');
$this->load->view('recycler/forms/register');
$this->load->view('includes/footer');
}
public function accept_terms_conditions()
{
// Checkbox name is 'agree' which checks if the value of $checked
// is equal to 1
$checked = $this->input->post('agree');
return (int) $checked == 1 ? TRUE : FALSE;
}
$this->form_validation->set_rules('agree', '', 'callback_accept_terms_conditions');
if($this->form_validation->run() == FALSE)
{
// After form submission, it always goes here
$this->registration_page();
}
else{ /* Proceed to data insertion */ }
I printed out the post value of it using print_r function and it returned 1, yet the validation still fails. $this->form_validation->run() == FALSE means that if the form validation has errors, it will go back to the register view, which is the $this->register() method call.
print_r
ticked checkbox
Does anyone know why it keeps failing?
EDIT:
Added the method for loading the registration view which is the register() method
try
public function accept_terms_conditions() {
$checked = ($this->input->post('agree')) ? TRUE : FALSE;
return $checked;
}
A checkbox brings "on" as value when it is checked not 1.
public function accept_terms_conditions()
{
// returns true if the checkbox 'agree' has been checked
return ($this->input->post('agree') === 'on');
}
And your condition is wrong, you are saying that if the validation has failed then call method register I think it should be the other way round.
I'm developing a plugin, in which the administrator has the ability to add and remove ZIP codes on the backend. I found that the best way to do this is by creating a custom post type named zip_code with only a title being supported, as that functionality is already built-in to WordPress.
I'm having trouble with validating the title, as it must be a valid ZIP code to avoid errors on the front end.
I've added the following action hooks:
// Action hook to intercept WordPress' default post saving function and redirect to ours
add_action('save_post', 'zip_code_save');
$validator = new Validator();
// Called after the redirect
add_action('admin_head-post.php', array($validator, 'add_plugin_notice'));
zip_code_save function:
public function zip_code_save() {
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (isset($_POST['post_type']) && $_POST['post_type'] == 'zip_code') {
$validator = new Validator();
if (!$validator->validate(get_the_title($post->ID))) {
$validator->update_option(1);
return false;
} else {
update_post_meta(
$post->ID,
'zip_code', get_the_title($post->ID));
}
}
}
And finally this is my Validator class:
class Validator {
//This for your your admin_notices hook
function show_error() {
echo '<div class="error">
<p>The ZIP Code entered is not valid. <b>Note</b>: only US ZIP codes are accepted.</p>
</div>';
}
//update option when admin_notices is needed or not
function update_option($val) {
update_option('display_my_admin_message', $val);
}
// Function to use for your admin notice
function add_plugin_notice() {
if (get_option('display_my_admin_message') == 1) {
// Check whether to display the message
add_action('admin_notices', array(&$this, 'show_error'));
// Turn off the message
update_option('display_my_admin_message', 0);
}
}
function validate($input) {
$zip = (isset($input) && !empty($input)) ? sanitize_text_field($input) : '';
if ( !preg_match( '/(^\d{5}$)|(^\d{5}-\d{4}$)/', $zip ) ) {
return false;
} else {
return true;
}
}
}
The above code successfully outputs an error message if the entered ZIP is not valid, however regardless of the error, it does publish the post. Is there a way to block the publishing of the post if the title is not valid?
Also, is there a way to prevent WordPress from creating drafts automatically? Since there is so little data, it's really irrelevant here, and more a hassle.
I am working on a page that can update the sales agent on a specific order.
I made the list of options and a dropdown list is created.
Heres in the controller:
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is("post")) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
Heres the view:
echo $this->Form->create("Order");
echo $this->Form->input("OrderID");
echo $this->Form->input("UserID");
echo $this->Form->submit("Submit");
echo $this->Form->end();
When I submit the data, it appears that the data is saved, (the flash message is set).
However, when I then preset the fields with data that is already in the database, all the sudden it doesn't even post. (I put a debug after the reques->is("post) condition which doesnt show up after I submit).
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is("post")) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
if (!$this->request->data) {
$this->request->data = $order;
}
The input fields are correctly pre filled, but now the form doesn't even post.
Does anybody know whats wrong?
Thanks!
Update your controller code to:
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is('post') || $this->request->is('put')) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
Now put a debug after the reques->is('post') condition which will show what you need to show.
I want to learn sessions in Yii, thus I created a simple login form. Also I want to "set" session in this project.
My login action
public function actionLogin()
{
Yii::app()->session['userid'] = "value"; // where i should put line ??
$model=new LoginForm('login');
if(isset($_POST['ajax']) && $_POST['ajax']==='form-reg')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['regist']))
{
$model->username=$_POST['istiad'];
$model->password=$_POST['parol'];
if($model->validate() && $model->login()) {
$this->redirect(array( 'update','id'=>$this->getIdByUsername($model->username ) ));
/* $this->render(
'update',array(
'model'=> $this->loadModelByUsername($model->username ) ) );*/
}
}
else
$this->redirect(Yii::app()->user->returnUrl);
}
logout action
public function actionLogout()
{
Yii::app()->user->logout();
unset(Yii::app()->session['userid']); // also this,
Yii::app()->session->clear(); // this
Yii::app()->session->destroy(); // and this line ??
$this->redirect(Yii::app()->homeUrl);
}
p.s: PLEASE EXPLAIN ME what is the userid in unset(Yii::app()->session['userid']); ? I couldn't understand (because I'm new in Yii). It's just only a variable or any attribute of the db table name?
I copied the lines from this topic.
Thanks. Best regards.
Set session after validation user name and password. Like here..
if($model->validate() && $model->login()) {
Yii::app()->session['userid'] = "value"; //here
$this->redirect(array( 'update','id'=>$this->getIdByUsername($model->username ) ));
}
unset destroys the specified variable.
unset(Yii::app()->session['userid']);
Here userid is session variable. It is destroyed.
Yii::app()->session->clear();
clear() is used to remove all sessions.
After clear(), you need to remove actual data from server using
Yii::app()->session->destroy();
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