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

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')";

Related

Find wordpress user meta ID

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

Get logged in memberID from database

I have two tables (blog_members and blog_posts) which are related 1 to many, 1 member to many posts. Therefore, in order to be able to relate the two I had to make one field in blog_posts named memberID and set up the relation between this field and the one from blog_members.
Now, I'm trying to make a script to add posts into the database. The thing is, now I have the field memberID in blog_posts which needs to be the same with the one from blog_members in order to be related. So, I'm trying to get the current logged in memberID from the blog_members so I can introduce it into the blog_posts.
I know this can be done with an input where you can type your ID but it doesn't feel right, I want this to be in the back, not to be seen.
Short story:
$memberID = get current logged in memberID from blog_members;
//insert into database
$sql="INSERT INTO blog_posts (memberID, postTitle,postDesc,postCont,postDate) VALUES('$memberID',$postTitle','$postDesc','$postCont','$postDate')";
$result=mysqli_query($link,$sql);
if($result){
echo '<p>Added successfully!</p>';
}else {
echo "Error: ".$sql."<br>".mysqli_error($link);
}
Normally you would store the ID of the logged in user in a session:
$_SESSION['login_userid'] = // THE USER ID OBTAINED FROM LOGIN
Now it will be stored in the browser as $_SESSION['login_userid'] and you can just put this to the top of your code:
$memberID = $_SESSION['login_userid'];
On every page where you use sessions you must run session_start() before the first line of HTML code. If you are not sure how to create your own login system, have a look at this tutorial.
Note: memberID is not unique in the blog_posts table, as one user can create many posts. You should probably create a primary key blogpostID as well.

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 );

Two different login pages according to ID using PHP and MySQL

$query = "SELECT *
FROM users
WHERE username = '".mysql_escape_string($username)."'
AND password = '".mysql_escape_string($password)."'";
$result = mysql_fetch_array(mysql_query($query));
Now this is my query for general visitors, now if i need a admin and moderator how can i process from the same users with a specific ID alone.
My SQL Query.
CREATE TABLE IF NOT EXISTS `users` (
`yourID` int(10) unsigned NOT NULL auto_increment,
`username` varchar(26) collate latin1_general_ci NOT NULL,
`password` varchar(26) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`yourID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=87992 ;
The login page will be the same for admin and moderator.
I don't know if I understand your question correctly, but normally a login system shouldn't make any difference between user roles. After logging in, you can check the logged in user's rights and and give him access to restricted areas of your application.
I may be misunderstanding what you want, but you could add another column to your users table to distinguish between the different types of user. You could add an integer column for this:
ALTER TABLE users ADD userlevel tinyint NOT NULL default 0;
Then you could set this according to what you want to let the user do, e.g.
0 = normal user
1 = moderator
2 = admin
etc.
When the user logs in you can then retrieve this value in the query and use it/store it in the session etc. in order to grant access to restricted actions.
You could also look at using the MySQL enum type for this as an alternative to the integer.
I'm not sure I understand this question. But I recently implemented a login system where I needed a user account and a admin account. I added a column to my users table that was "privilege" which I then stored a numeric number that related to that user's privilege level. Then on restricted pages I just checked to see if the user had the required level of access to view the page.
Add privilege (tinyInt) attribute user table.
Add active (Boolean) attribute to
user table.
Add cookie (varchar) attribute to
user table.
//Ran when page is accessed
checkLogin($privilege){
//Use Cookie to determine which user is accessing page
//Determine if user is active (toggled when logged in)
//retrieve users privilege level
//check against passed in value
//return true or false
}
//Php for website.
if(checkLogin(0)){
//0 – guest
//1 – user
//3 – admin
}
I'm guessing you want to differentiate between 'normal users' and 'admins'. Why not just add another column to your users table, e.g. AdminStatus and make it an int, zero for no, one for yes. Then in your login query just check the AdminStatus.
you need to add a field of user type to the table,
and you know what is the group user

Categories