Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active February 6, 2018 18:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save xnau/6b9b703b719978d3fe2c9f990d8a2b3b to your computer and use it in GitHub Desktop.
Demonstrates how to alter the redirect according to a value in the user's record when using the PDB Login add-on
<?php
/*
Plugin Name: Redirect PDB Login
Description: Alters the redirect according to a value in the user's record when using the PDB Login add-on
*/
add_action( 'pdb-login_after_validate_submission', 'xnau_setup_pdb_login_redirect', 10, 2 );
/**
* sets up the redirect filter
*
* this checks the newly-logged-in user's record and
* sets up the redirect accodingly
*
* this example inspects the user's record for a field named "type"
* and redirects the user according to the value found: 'red' or 'green'
*
* @param object $record the user's record
* @param bool $validated true if the user's login was validated
* @return null
*/
function xnau_setup_pdb_login_redirect( $record, $validated )
{
if ( ! $validated ) {
// skip if not validated
return;
}
switch ( $record->type ) {
case 'red':
// set the redirect to the page slug
$redirect = 'red-record-edit';
break;
case 'green':
$redirect = 'green-record-edit';
break;
}
/*
* this filter uses an anonymous function, we pass the $redirect value
* into that function with 'use'
*/
add_filter( 'pdb-login_redirect', function ($default_redirect) use ( $redirect ) {
return Participants_Db::find_permalink( $redirect );
}
);
}
@xnau
Copy link
Author

xnau commented Feb 1, 2018

You can install this as a plugin, just download the .zip and use the WP plugin installer to upload the zip file.

Of course, you will have to modify this to your particular setup and needs.

You'll want to test this with your php error log set up. It will throw an error if the defined record values are not found so that you can make sure you've got it working correctly.

@Andrew94410
Copy link

Hi,
I'm sorry but I think there is a problem in your php file. When I try to activate this plugin as you told me (after modification and without modification) here is the message I get:
https://user-images.githubusercontent.com/36070788/35727689-437d6a16-0809-11e8-9f31-33c205b3f7ce.png

Please can you help me ? :)
Thanks

@xnau
Copy link
Author

xnau commented Feb 6, 2018

Yes, you're right, the function should have the "public" keyword. I fixed it.

@xnau
Copy link
Author

xnau commented Feb 6, 2018

I had to make a couple of changes to this script...there were a few errors...but it's tested working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment