I need to be able to add pages and then restrict these by different types of user: Anonymous, Partial and Full.
When registering, a user can enter all details to get a full registration or only half their details e.g don't need to enter address, telephone etc to gain a partial registration type.
Then when adding a page I need to be able to select whether anonymous, partial and/or full have access to it. If they don't then it still needs to show the summary of the page as a teaser but they won't have access to the main content until they have registered and signed in.
I have installed the Simple Access plugin which allows me to create groups but unsure how to implement the registration form so that if the user only enters the required fields they will become a Partial user, otherwise they will become a Full user. Any suggestions?
You can use the Rules module. Create a triggered rule that runs on new user creation, then check the fields and finally assign the user a role accordingly.
I've ended up using a hook to implement whether a user is a normal authorised user or a full user. Can someone please check the hook below is correct? Im new to Drupal so not sure if any other tables are affected when adding/deleting roles.
function module_user_update(&$edit, $account, $category) {
$dob = field_get_items('user', $account, 'field_dob');
$address1 = field_get_items('user', $account, 'field_address1');
$address2 = field_get_items('user', $account, 'field_address2');
$address3 = field_get_items('user', $account, 'field_address3');
$city = field_get_items('user', $account, 'field_city');
$postcode = field_get_items('user', $account, 'field_postcode');
$county = field_get_items('user', $account, 'field_county');
$telephone = field_get_items('user', $account, 'field_telephone');
if(empty($dob[0]['value']) || empty($address1[0]['value']) || empty($address2[0]['value']) || empty($address3[0]['value']) || empty($city[0]['value']) || empty($postcode[0]['value']) || empty($county[0][$
{
$userid = $account->uid;
//remove full role from db so user is only an authorised user
db_query("DELETE FROM {users_roles} WHERE uid = '".$userid."' && rid = '5'");
} else {
$userid = $account->uid;
//delete full role if it already exists so it doesnt go in twice
db_query("DELETE FROM {users_roles} WHERE uid = '".$userid."' && rid = '5'");
//insert full role
db_query("INSERT INTO {users_roles} (rid, uid) VALUES ('5',$userid)");
}
}
You would do well to look into some of the beginning Drupal tutorials. Roles, rules, triggers and CCK (and content_permissions) -- those are the modules/concepts you'll be looking at learning about.
They will arm you with what you need. CCK will allow for you to create specific content types, content_permissions (included in CCK) will allow for you to set visibility based on a user's role, roles will allow for you to create a new group of users, and as #Laxman13 said, rules will allow for you to set up rules to do what needs to be done (i.e. add this user to X role), and triggers will provide you with the functionality to do that.
Related
I have a Wordpress site that has lots of users who are just visitors. They can choose 2 settings which determines the sort order of results and these are saved as cookies.
If they choose to create an account later then I'd like to pre-populate 2 custom user fields (I'm using Advanced Custom Fields) with their previous choices as visitors from the cookies.
What is the best way to do this? I.e something the same way Woocommerce stores cart data / wishlists for non logged in users that persist after account creation.
Currently I am using this for the sorting but it seems a bit inefficient if I am going to try and pre-populate fields on registration anyway...
//check if user is logged in and then get saved fuel choice
if ( is_user_logged_in() ) {
$userid = get_current_user_id();
$fuelchoice = get_the_author_meta( 'fuel_choice', $userid );
} else {
//get the cookie if just a visitor
if(isset($_COOKIE['fuelchoice'])) {
$fuelchoice = $_COOKIE["fuelchoice"];
}
}
if ($fuelchoice == "diesel"){
//sort function etc
}
I created a new user role, but I need this user role to only be able to edit users with another user role, I do not want this new user role to be able to edit users like, editors, author, etc...
Ideas?
you can add roles so:
$result = add_role(
'editor_of_users',
__( 'Editor of Users' ),
array(
'edit_users' => true,
// Add more capabilities...
)
);
if ( null !== $result ) {
echo 'Yay! New role created!';
}
else {
echo 'Oh... the editor_of_users role already exists.';
}
For more information you can read the documentation about: add_role
And the all users capabilities here: Roles and Capabilities
EDIT:
Read this post: to know how add to users the capabilities to edit specific roles...
How to allow an user role to create a new user under a role which lower than his level only?
I hope you resolve it... Good Look! ;)
I need to grant users a specific role (Editor, Administrator, etc.) along with all its capabilities on the fly in Wordpress, but I don't want to update their role in the database (so that the next time they come back, they will have their original role). How can I go about doing this?
Here's what I ended up doing:
add_filter( 'user_has_cap', 'override_caps' );
function override_caps($allcaps){
if( ... ){ // When to override caps
$role_name = 'administrator';
$role = get_role($role_name); // Get the role object by role name
$allcaps = $role->capabilities; // Get the capabilities for the role
$allcaps[$role_name] = true; // Add role name to capabilities
}
return $allcaps;
}
I'm using Formidable forms in Wordpress and have a form that registers users. I can use a radio button in the registration form to determine what their role will be. I have a hook for that. What I need, however, is a hook that will change the user role based on radio selection on form entry UPDATE. My current code only works on entry creation. Here is the code that assigns roles on registration:
add_filter('frmreg_new_role', 'frmreg_new_role', 10, 2);
function frmreg_new_role($role, $atts){
extract($atts);
if($form->id == 8){
if($_POST['item_meta'][280] == 'Job Applicant')
$role = 'applicant';
}
return $role;
}
"8" is the id of the form itself. "280" is the id of the radio button field where "Job Applicant" is one of the values. And "applicant" is one of our site's user roles.
I need an adaptation of this that will change the role after the entry has already been created, on update. The closest thing I can find is a hook that changes user role after a successful PayPal payment. I tried to combine the two but I couldn't get it to work. Here is the PayPal generated user role changer:
add_action('frm_payment_paypal_ipn', 'change_paid_user_role');
function change_paid_user_role($args){
$new_role = 'contributor'; //change this to the role paid users should have
if(!$args['pay_vars']['completed'])
return; //don't continue if the payment was not completed
if(!$args['entry']->user_id or !is_numeric($args['entry']->user_id))
return; //don't continue if not linked to a user
$user = get_userdata($args['entry']->user_id);
if(!$user)
return; //don't continue if user doesn't exist
$updated_user = (array)$user;
// Get the highest/primary role for this user
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
if ( $user_role == 'administrator' )
return; //make sure we don't downgrade any admins
$updated_user['role'] = $new_role;
wp_update_user($updated_user);
}
UPDATE: the action hook should probably be: frm_after_create_entry according to Formidable forums.
Many times, researching the core files is more productive than any Google or Manual. Dropping the whole plugin directory in a code editor and researching for the string frm_after_create_entry takes us to the create() method where this hook happens.
After that, there's the update() method and it provides the action hook: frm_after_update_entry.
This hook passes two parameters: $id and $new_values['form_id']. I cannot reproduce your setup, so testing the hook is up to you.
Reference: Actions and filters are NOT the same thing…
In this example:
add_action( 'frm_after_update_entry', 'change_role_to_staff', 10, 2);
function change_role_to_staff( $form_id, $values ){
var_dump($values);
die();
}
As this is an action hook, nothing has to be returned.
There's no $roles or $atts, the parameters are the form ID and Values.
What you're looking for is inside $values.
var_dump() and die() are for debugging purposes and must be removed at once after testing.
Do your wp_update_user with this values and adapting your previous code.
I've created a simple module for displaying a flash game in a custom block by overwriting game_block_view() and game_block_info() in the sites/default/modules/game.module and it works ok.
I need however to pass user avatar and also gender and city (I've added the 2 mandatory fields to the registration form) through the FlashVars-parameter to the flash game in my block.
So I'm trying to overload the hook_user_load, because I suppose that this is the method where you add properties to the $user object after it has been initiated from the database (this probably happens when the user logins or alters his/her profile data?):
function game_user_load($users) {
global $user;
$uid = $user->uid;
$result = db_query('select filename from {file_managed} where uid=:uid', array(':uid' => array($uid)));
$avatar = $result->fetchField();
$users[$uid]->avatar = $avatar;
drupal_set_message("<pre>$uid: $avatar</pre>\n");
print_r($users);
}
Unfortunately I see no output produced by the last 2 lines above in the web page
What am I doing wrong?
Thank you!
Alex
The global user object does not go through hook_user_load(), see http://api.drupal.org/api/drupal/includes--session.inc/function/_drupal_session_read/7. Don't ask me why, that's just the way it is :)
When using user_load(), any added fields will automatically be loaded, you don't need custom code for that. You just need to know how to access them, which is a bit complicated.
Something like this should work:
global $user;
// $account is now a fully loaded user object.
$account = user_load($user->uid);
// Your field name is probably 'field_avatar'.
if ($avatar = field_get_items('user', $account, 'field_avatar')) {
dpm($avatar); // only works with devel.module, strongly suggested!
}