Wordpress update user meta doesn't work with strtolower - php

I've a problem with the wp_update_user in wordpress. The code below works, but not as I want.
add_action('user_register', 'register_role', 10 , 1);
function register_role($user_id) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $_POST['Newrole']; //value for example 'Goldmember'
wp_update_user($userdata);
}
I have several problems:
the "Goldmember" is the displayname of the userrole. I see the new usermeta in the database (a:2:{s:6:"Goldmember";b:1;s:6:"subscriber";b:1;})
the "Goldmember" doesn't work in wordpress user administration, and the user has the default role activated
if I change "Goldmember" to "goldmember" with strtolower($_POST['Newrole']) the code doesn't work anymore
if I change the "Goldmember" to "goldmember" in the database (a:2:{s:6:"goldmember";b:1;s:6:"subscriber";b:1;}), it is visible in wordpress user administration and works
what do I have to change in my code, I don't see any mistake.
thanks a lot

I've changed my way to solve the problem. I took an other hook to change the userrole on registration.
I took the "uwp_after_process_register", this one will be fired at the whole end of the registration process, so there I can do, whatever I want.
This is now my solution, so every problem I had is solved, you only have to take UsersWP:
add_action('uwp_after_process_register', 'register_role', 10 , 2);
function register_role($data, $user_id) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $data['FormFieldRole'];
wp_update_user($userdata);
}

Related

Wordpress set value from input field as comment author for logged in users

I have a shared account, lets call it "marketing".
Now I have three people using this account named Anna, Ben and Max.
When someone logs into this account and wants to type a comment with their name, it always shows the name "marketing" as author. How can I change this so that I put a name in my input field and I get the value from this as my comment author. In my comments.php I do not check if the user is logged on and call it here:
<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="2"/>
I tried to make following in my functions.php:
function change_author( $commentdata ) {
if ( $commentdata['user_ID'] == 2 ) { // User ID for Marketing
$commentdata['user_ID'] = 0;
$commentdata['comment_author'] = '';
}
return $commentdata;
}
add_filter( 'preprocess_comment' , 'change_author' );
In this case my comment_author will always be Anonymous and also if I set it to NULL.
When i give the variable a value $commentdata['comment_author'] = 'test';
I get "test" as my comment_author but as I said, I would like to have the value I set in my input field. It works for not logged in users very well so I thought there might be a trick for logged in users as well.
In phpMyAdmin I checked for the entries in the database and it is giving me the right values for ID etc. But it also empties the comment_author field which results in an anonymous author.
I really hope that somebody can give me the right hint to accomplish this task.
https://wordpress.org/plugins/allow-multiple-accounts/ this plugin might help you, you can have multiple accounts under one account.
Goddamit, I didn't know that this was so f***ing easy.
function change_author( $commentdata ) {
if ( $commentdata['user_ID'] == 2 ) {
$commentdata['user_ID'] = 0;
$commentdata['comment_author'] = $_POST['author'];
$commentdata['comment_author_email'] = '';
$commentdata['comment_author_url'] = '';
}
return $commentdata;
}
add_filter( 'preprocess_comment' , 'change_author' );
I think this would work also with != 0 instead of == 2.
This is just a solution for my custom theme but I think it would also work with
$commentdata['comment_author_email'] = $_POST['mail'];
$commentdata['comment_author_url'] = $_POST['url'];
Note: the value in $_POST[] is either the name or the ID from the
input field. Didn't test everything because name and ID have the same
value in my theme.
Tested it a few times local and it worked very well!

CAKEPHP 3.1 clear form after submit

Usually on cakephp2 i used to unset form data and everything was ok.
Some times i use redirects to clear it. But i cant do that in this current page.
Anyone has found this issue or a solution for this?
If you are staying on the "add" page after successfully adding a record, for example to allow entry of multiple records more quickly, you'll need to reset the entity after saving. For example, if you're entering Posts, your controller will look something like:
$post = $this->Posts->newEntity();
if ($this->request->is('post')) {
$post = $this->Posts->patchEntity($post, $this->request->data);
if ($this->Posts->save($post)) {
$post = $this->Posts->newEntity(); // <- Reset the entity
}
}
$this->set(compact('post'));
(Error checking, flash messages, etc. all left out for brevity.)
An alternative is to simply redirect to the same page.
I had the problem that not everything was deleted
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->data)) {
$this->Flash->success(__('Submited.'));
$this->request->data(null);
$contact = new ContactForm();
return $this->redirect('/(Same Page)');// This did the final trick for me
}
}

Magento save(); cause app to hang

I am trying to update categories to no longer be active by setting is_active to 0, but whenever the action gets to save() Magento hangs and won't save the updated database. I have to restart the server in order to use magento again. Does anyone know why it hangs on save()? We are using mysql workbench as root user so it's not a permissions issue. $todelete->delete() works without any issue
public function onclearAction() //Clear button on sales app removes sale
{
echo "onclear start";
$model = Mage::getModel('countdown/observer_category');
foreach ($_POST['number'] as $entity_id) {
$type = "category";
$cat = Mage::getModel("catalog/category")->load($entity_id);
$cat->setData('is_active', 0);
$cat->save();
$todelete = Mage::getModel('countdown/countdown')->getCountdown($type, $entity_id);
$todelete->delete();
}
$this->_redirect('adminhtml/salesapp/index');
}
use this
$cat->setIsActive(1);
$cat->save();
I hope this would help you , don't forget to like my ans if it was help full

Multi-level Registration in Drupal

How to implement two type of registration like student and teacher?
I need two type of registration one for Teacher and one for Student. Both are different registration and both have different roles. Is it possible in Drupal? And also I need registering Student there is no admin approval but for Teacher registration, admin approval is required. How can I achieve this in Drupal 6?
In the custom user registration form add one more select box field with ROLE TYPE (student, teacher).Then on the submit hook check like shown below.
function add_student_form_submit($form, &$form_state) {
$fields = array();
$fields['is_new'] = true;
$fields['name'] = $form_state['values']['user_name'];
$fields['pass'] = $form_state['values']['pass'];
$role_type = $form_state['values']['role_type'];
//Add the user to the corresponding role
$fields['roles'] = array($role_type)
//here you can achieve the thing which you want.If the role is a teacher then set
//status = 0, else status = 1
if($role_type == 'student')
$fields['status'] = 1;
else
$fields['status'] = 0; // $user = user_save(drupal_anonymous_user(), $fields); //This works in D7
$user = user_save('', $fields); //pretty sure this is what works in D6 }
If the user is the teacher you should go to http://localhost/domain_name/admin/user/user. Here you can filter the Inactive users and activate them.
hey you can use below modules for creating multilevel registration.
http://drupal.org/project/content_profile
http://drupal.org/project/autoassignrole
above modules help you to create multilevel registration form in site. from content profile you can create form and also give auto assign role to student.
As far as i'm aware, drupal doesn't provide any mechanism for having multiple types of registration forms. However you can fairly easily create your own registration form from scratch. All you really need is the user_save function to create a new user. See the sample code below as part of a form_submit hook
function add_student_form_submit($form, &$form_state) {
$fields = array();
$fields['is_new'] = true;
$fields['name'] = $form_state['values']['user_name'];
$fields['pass'] = $form_state['values']['pass'];
$fields['status'] = 1;
// $user = user_save(drupal_anonymous_user(), $fields); //This works in D7
$user = user_save('', $fields); //pretty sure this is what works in D6
}
Using this you can create whatever custom logic you want for each form

Remove publishing capability from user IDs

I'm trying to remove the capability of users other than the administrator/superadmin (ID number 1) to add a page, I know that there are plugins which you can edit the wordpress role but in my case I need it to be per user/username/userid (no plugin that I no of is available)..
Currently user number 2 needs to be 'Administrator' because a specific plugin I use only displays reports to 'Administrator' role but I need to remove add page capabilities. I have the following code:
function modify_capabilities()
{
global $userdata;
get_currentuserinfo();
$userdata->ID != 1 ->remove_cap('publish_pages');
}
add_action('admin_init','modify_capabilities');
But it doesn't work.. The error is in this line:
$userdata->ID != 1 ->remove_cap('publish_pages');
Your code seems off by a bit:
function modify_capabilities()
{
global $userdata;
get_currentuserinfo();
if ($userdata->ID != 1) {
$role = get_role('author');
$role->remove_cap('publish_pages');
$role->remove_cap('publish_posts');
}
}
add_action('admin_init','modify_capabilities');
Updated based on your comments and taking the original details from the blog you linked to. Not sure why you removed those parts though...

Categories