Find wordpress user meta ID - php

I wamt to make an auto fill field with gravity form that fill the name of the user and their roles automatically by inserting meta ID, for the name i found the ID 'display_name' but I can;t figure it out what is the roles meta ID, thank you.

It is not so straightforward. user_meta table in wordpress database contains field wp_capabilities, in which is stored an object of capabilities. It is better to use WordPress funcions to extract user roles:
$id = ... // ID of the user
$caps = get_userdata(1)->wp_capabilities; // object of capabilites looking like {administrator: true}
$roles = array_keys($caps); // now we have list of capabilies in array

Related

Dynamically add custom input fields and save them to database

I want to develop an web-service where a user can add tickets. Every ticket has got an input text field title and a textarea description. If the user klick the save button, the data will be saved in a mysql database.
The admin has got an admin panel. He can add or remove input fields in this admin panel to change the add ticket view form the user.
For example: The admin adds a select field category. You can select category A, B or C. If you select category A, there will be a new input text field called animal. If you select category B, nothing happens. If you select category C, there will be 2 new fields: A text field and a number field. The number field is required. And so on, and so on. After a week, the admin could remove some fields, or add a category or... To conclude, the admin can add and remove select, text, number, password,... fields to the add ticket section with relationships, length and requirements.
I dont know how to structure the database and to save the data. I think about something like a mysql table tickets with title, desc, and data and put an XML / JSON String to the data field and another table ticketFields with name (category, animal,...), type (text, number,...), required (yes/no), length (int), data (to store data for select fields). The problem is, that the relationships are missing in this model. So how can I save this data efficient?
The relational model might look like this:
Ticket:
id PK
Answer:
ticketId FK PK
fieldId FK PK
value
Field:
id PK,
name,
parentFieldId NULL FK,
parentFieldValue NULL,
type,
required,
min NULL,
max NULL,
range NULL
(...other "constraint" fields, checked regarding choosen type)
At first, fields with parentFieldId of NULL are displayed. Fields having parentFieldId set are shown only if the answer for parent field is given. Fields having parentFieldId and parentFieldValue set are displayed if the answer for parent field is given and it equals parentFieldValue.
Given answer is checked regarding to field's type (e.g. if type is "number" then the answer must be a number between min and max).
You can create another table with id, category and field for the admin to use and relate them both using a common id field, preferably primary key. Now, this table can be queried for the reference of new categories inserted against a particular id in your main table. Use javascript/jquery to dynamically create html code for the new fields and show them on your page.
Suppose your the user selects Option 'A', then the new table can be queried to see if there are any fields set by the admin against the option 'A'. If yes, retrive those fields and show them to the user.

Wordpress - Retrieving specific posts from the current user

I have a custom post with 2 fields, the first field being the user, and the second one being a table, and whenever a user is logged in, I must display all of the posts that have the user in the 1st field, this is what I found:
In wp_user the ID of the user I'll be using to test it will have: user_id = 3
In wp_postmeta, I have a row with these values:
-post_id=92
-meta_value=3
And in wp_posts, in the ID = 92, I have the specific post name I was looking for.
I'm pretty confused on how to make this code dynamically, so any user will be able to see their own posts. And BTW, the post_author is the admin, so I can't use that field
Basiclly, I must retrieve the post_id from wp_postmeta by only using the the meta_value which I've seen a couple of guides but can't really understand how to do it. I know I must use wp_get_current_user(). Please someone explain me with detail how to achieve this.
Thanks
In your functions.php you need to place this hook , this hook is fired whenever a post is added or updated you need to use this for saving post id in the post meta table.
function enter_post_id( $post_id ) {
wp_get_current_user() // Get current user id with this
update_post_meta()($post_id, $meta_key, $meta_value, $unique)
// add a meta key say 'user_id' and place the value of user_id as meta value
}
add_action( 'save_post', 'enter_post_id' );
Now You have the match completed , you just need to query through the post meta table and get the post id for the user , and then from wp_posts show only the post's with matched ID.
Hope this helps . Feel free to ask if you have any confusion.
No problem :) Use this function.
function get_post_id(){
global $wpdb;
$result = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE meta_key='user_id' AND meta_value= $user_id ", OBJECT );
// Where $user_id is the id you get from wp_current_user()
}
Now var dump this $result to see the post_id
loop through this to get values for each post id

Add Custom PHP Page / Post in Wordpress to display data based on URL

I am creating a simple db for School Students and want to achieve it in wordpress using any method that is available now.
Every student details like first name, class etc will be saved in database along with ID as primary key.
In PHP, there used to be ONE file like details.php etc and based on QUery passed using GET or POST method, it will display the details. Can we do the same in Wordpress using a SINGLE Page or Post;
Instead of creating seperate Page / Post for every student, i had created PHP Queries in Page / Post using WP Plugin which will display the details of student based on querying ID.
But i am not sure how to make it generalized so that on entering http://mywpsite.com/studentpageorpost/?id=10 i should get the details of student with ID 10; and on entering http://mywpsite.com/studentpageorpost/?id=11, i should get the details of ID 11;
Can anyone please help on this.
If I understand well the code would work like this:
1 Take the id number from url and stored
1.1 htmlspecialchars() is for converting html tags so you can't be hacked by php injection
$id = htmlspecialchars( $_GET["id"] );
2 Here we have stored the user info in a object with the wordpress function get_userdata();
2.1 If ID not found return false.
$user_data = get_userdata( $id );
Accessing Usermeta Data
$user_data = get_userdata( $id );
echo $user_data->last_name . ", " . $user_info->first_name . "\n";
Results in:
Doe, John
If you want to know how to access more user info use print_r($user_data); and will output all the info that the user has.
Here are some of the useful values in the wp_users and wp_usermeta tables you can access with this function for use in your theme or plugin:
users
ID
user_login
user_pass
user_nicename
user_email
user_url
user_registered
display_name
user_meta
user_firstname
user_lastname
nickname
description
wp_capabilities (array)
admin_color (Theme of your admin page. Default is fresh.)
closedpostboxes_page
primary_blog
rich_editing
source_domain
Edit: I think the above system is the easiest still as MarkBlythe sed, no need for individual account, you can use custom post type plugin and custom fields. You could add the students very fast in a loop and array with this function wp_create_user( $username, $password, $email );

How to specify user ID when creating a user in wordpress?

How can I specify my own user ID when inserting a new user in wordpress?
Looking at this page here: http://codex.wordpress.org/Function_Reference/wp_insert_user
It doesn't give me an option to specify the user ID of the user I want to insert into wordpress.
How would I be able to specify the User ID then?
The user id ( ID ) in wordpress is auto-increment field, you cant specify it while adding new user, if you add it then update will be done matching the user id rather new insert, like:
$user_id = 10;
$somevar = 'test';
wp_insert_user( array ('ID' => $user_id, 'user_dat' => $somevar) ) ;
Since ID is provided here, the user id with value 1 will be updated, if matching ID is found.
According with Wordpress documentation, if a user ID is pass to the wp_insert_user() function, it will try to modify the user with that ID if exists, but a new user won't be created. You sould store the facebook ID as user_meta.
You can use this, I think it helps to solve this problem. But the set ID must be an integer value.
$sql = "INSERT INTO `$table_name`(`ID`, `user_login`, `user_pass`, `user_nicename`, `user_email`, `user_registered`, `user_activation_key`, `user_url`, `user_status`, `display_name` ) VALUES (4545343,'$user_name','$pass','$fullName','$user_email','$time','$user_id','$url','0','$user_id')";

How to tell if a user has updated a profile field in Drupal 7

I am trying to determine how in drupal 7 I can programmatically tell if a user has updated a custom field in their profile within the past 24 hours however looking through the tables I can't seem to find any field with that information does anyone have any ideas?? Essentially I need to pull this information out during a batch job that run onces a night and updates other systems based upon the information in drupal. If someone can think of a better way todo this it would be appreciated. thanks for any help you can provide
ok i dont know if its working or not try the following steps
1- first you should tell drupal....when user edit the profile page do something...(whatever the something is ..insert something to database..retrieve something..etc)
/**
* Implementation of hook_form_alter
*/
function newcontent_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'user_profile_form'){
$form['#submit'][] = '_do_something_when_user_save_profile';
}
}
/**
* do something when user save profile
*/
function _do_something_when_user_save_profile($form,$form_state){
// do something
}
now i think we need to do some queries ... first we need to create 2 tables
http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_schema/7
(this url will help you to create your schema)
the first table will contain the current value of the field u want track so i think this table should have the following fields
(primary key , user id, field name, field value)
the second table will contain the date of the last upated operation the user made
i think the fields will be like the following (primary key,user id , field name, date)
now lets return to the form submit function
/**
* do something when user save profile
*/
function _do_something_when_user_save_profile($form,$form_state){
// now i can check in the first table to see
// is the current submitted value for this field = the value in the table
// if its = the table value then the user didn't change any thing now i dont
// need to update the second table
// if the current submitted value != the value in the table then its mean
// that the user have updated this field ...
// now i should insert new value to the second
// table with the current date time and the current field value
}
i hope u under stand me and sorry for my bad english
maged adel provided reasonable solution, however there is one more way of doing this:
you can use hook_user_update for this purpose. It's better that solution provided above because of 2 reasons:
1) you have both $edit - entered values and $account - user account values, so you can compare and get know what fields being updated (if you don't have 1 specific field to check).
2) just 1 hook to implement.

Categories