Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Function When Add User WordPress

  • SOLVED

Hi All,

Im searching all over codex, google and forum to create a custom function when adding users in wordpress, but im a new coder.
Can anyone help me with this:

When creating a user, automaticly create a category with username, page with username and custom template.

Im new to php and wordpress, dont know exactly were to add these functions.

lines 1593 - 1602 - wp-includes/users.php

function wp_create_user($username, $password, $email = '') {
$user_login = esc_sql( $username );
$user_email = esc_sql( $email );
$user_pass = $password;

// create a category with username
wp_create_category( $username );

// create page with username and theme?
$my_page = array(
'post_title' => '$username',
'post_content' => '[list category=$username]',
'post_status' => 'publish',
'post_author' => 1,
//didnt find this function above
//'page_template' => 'portal.php',
);

// Insert the post into the database
wp_insert_post( $my_page );

$userdata = compact('user_login', 'user_email', 'user_pass');
return wp_insert_user($userdata);
}


Edited
----------------------

Thanks guys.

It worked great, so excited about my(our) first function.

Lets decide the winner of this one. =)
Wondering another function to remove category, page and user when deleting a user for complete functionality

add_action('user_??','my_function2');

function my_function2($user_id){
wp_delete_user( $id, $reassign );
wp_delete_category( $cat_ID );
wp_delete_post( $ID );

}

Answers (7)

2011-12-09

Hai Bui answers:

Try putting this code in functions.php (code updated)

add_action('user_register','my_function');

function my_function($user_id){

$user_info = get_userdata($user_id);
$username=$user_info->user_login;

// create a category with username
wp_create_category( $username );

// create page with username and theme?
$my_page = array(
'post_title' => '$username',
'post_content' => '[list category=$username]',
'post_status' => 'publish',
'post_author' => 1
);



// Insert the post into the database

$pid = wp_insert_post( $my_page );

//set the page template
update_post_meta($pid,'_wp_page_template','portal.php');

}


basequatro comments:

It worked great, so excited about my(our) first function.

Lets decide the winner of this one. =)
Wondering another function to remove category, page and user when deleting a user.


Hai Bui comments:

Function when delete a user:

function my_delete_user($user_id) {

}
add_action( 'delete_user', 'my_delete_user');

2011-12-09

Luis Abarca answers:

Did you try this action ?

[[LINK href="http://codex.wordpress.org/Plugin_API/Action_Reference/user_register"]]http://codex.wordpress.org/Plugin_API/Action_Reference/user_register[[/LINK]] ??

<blockquote>Runs when a user's profile is first created. Action function argument: user ID.</blockquote>


Luis Abarca comments:

Here is an example


add_action('user_register', 'add_custom_register_actions');

function add_custom_register_actions($user_id)
{
// get the user
$user_info = get_userdata( $user_id );

$username = esc_sql( $user_info->user_login );

// create a category with username
wp_create_category( $username );

// create page with username and theme?
$my_page = array(
'post_title' => $username,
'post_content' => '[list category=$username]',
'post_status' => 'publish',
'post_author' => 1, // this should be $user_id ??
//'page_template' => 'portal.php',
);

// Insert the post into the database
wp_insert_post( $my_page );
}


Luis Abarca comments:

Hai bui add the custom template code


//set the page template
update_post_meta($pid,'_wp_page_template','portal.php');


And robin gupta the page post type in the array
'post_type' => 'page'

So, this can works


add_action('user_register', 'add_custom_register_actions');

function add_custom_register_actions($user_id)
{
// get the user
$user_info = get_userdata( $user_id );

// user name
$username = $user_info->user_login;

// create a category with username
wp_create_category( $username );

// create page with username and theme?
$my_page = array(
<strong>'post_type' => 'page',</strong>
'post_title' => $username, <strong>// this could be changed to $user_info->display_name or nickname</strong>
'post_content' => '[list category=$username]',
'post_status' => 'publish',
'post_author' => $user_id // the new user should be the author
);

// Insert the post into the database
$post_id = wp_insert_post( $my_page );

// assign the custom template
<strong>update_post_meta($post_id, '_wp_page_template' , 'portal.php');</strong>
}


basequatro comments:

It worked great, so excited about my(our) first function.

Lets decide the winner of this one. =)
Wondering another function to remove category, page and user when deleting a user.


Luis Abarca comments:

I think that the second question (delete user content) is for another question :D

I'm reading what Jhon said, i think you can use the author page for show the user content and also add a custom template for authors.

Or there's an special goal for using your own method ?


basequatro comments:

list more posts private only for one user

2011-12-09

Pixel Coder answers:

I've not written this exact function before but been close to it. You'll need to use

wp_create_user()

Found here...

http://codex.wordpress.org/Function_Reference/wp_create_user

Which after running successfully will return a user ID.

You'll then need to add a new category by using the WPDB class and populating all necessary fields within the wp_term_taxonomy table.

http://codex.wordpress.org/Class_Reference/wpdb

From there, you would then need to use raw PHP functions to create a new file.
Write to it a custom string which encapsulates your desired output and file name with WordPress conditionals e.g.

// TEMPLATE NAME: CAT Template: User-name category template

Basic tutorial for creating a file.
http://www.tizag.com/phpT/filecreate.php

Basic tutorial for writing to a file.
http://www.tizag.com/phpT/filewrite.php

Hope that gets you on the Road


2011-12-09

Arnav Joy answers:

wp_insert_post function as you are using , but few changes

try this:--

$my_page = array(

'post_title' => '$username',

'post_content' => '[list category=$username]',

'post_status' => 'publish',

//use post type parameter

'post_type' => 'page'

'post_author' => 1,

);



// Insert the post into the database
// get the id of the newely inserted page here

$newPageID = wp_insert_post( $my_page );

// now to assign this page a template use this

update_post_meta($newPageID, "_wp_page_template", "new_template.php");


maybe this code is useful to you.


Thanks
Robin


basequatro comments:

Wondering another function to remove category, page and user when deleting a user for complete functionality

add_action('user_??','my_function2');

function my_function2($user_id){
wp_delete_user( $id, $reassign );
wp_delete_category( $cat_ID );
wp_delete_post( $ID );

}


Arnav Joy comments:

for deletion of category and page i think you should modify your code for the addition of user

try this:--

// get the id of category in variable

$catID = wp_create_category( $username );

$my_page = array(

'post_title' => '$username',

'post_content' => '[list category=$username]',

'post_status' => 'publish',

//use post type parameter

'post_type' => 'page'

'post_author' => 1,

);


update_post_meta($newPageID, "_wp_page_template", "new_template.php");

// now update the meta table of user to hold the information for this user in terms of category id and page id

update_user_meta($user_id, "_wp_page_id", $newPageID);
update_user_meta($user_id, "_wp_cat_id", $catID);

//now for deletion

add_action('delete_user','my_function2');



function my_function2($user_id){

$catID = get_user_meta($user_id, '_wp_cat_id');
$pageID = get_user_meta($user_id, '_wp_page_id');

wp_delete_user( $id, $reassign );


wp_delete_category( $catID);

wp_delete_post( $pageID);



}


try this code i think it will help full for you.

Thanks
Robin


basequatro comments:

With your function delete user and category works, but not delete page and generate a 500 error after.
With my modified function delete user and category works, no error but dont delete the page too.

$newPageID = wp_insert_post( $my_page );

update_post_meta($newPageID, "_wp_page_template", "cliente-portal.php");
update_user_meta($user_id, "_wp_page_id", $newPageID);

}


add_action( 'delete_user', 'wpq_delete_user_stuff', 15 );

function wpq_delete_user_stuff( $user_id ) {

// vars

$tax = 'category';
$user_info = get_userdata( $user_id );
$username = $user_info->user_login;



// check if term with that slug exists
if ( $user_term = get_term_by( 'slug', $username, $tax ) ) {

// delete the term associated with that user
wp_delete_term( $user_term->term_id, $tax );


}

// check for page
//if ( $user_page = get_page_by_path( $username ) ) {

// delete the associated page bypassing the Trash (change the second parameter to false to use the trash)
//wp_delete_post( $user_page->ID, true );
wp_delete_post( $pageID);


//}

}


Arnav Joy comments:

Try to use this code , it will make the page in trash

use following function in place of:-

//wp_delete_post( $pageID);


wp_update_post(array('ID'=>$pageID , 'post_status' => 'trash' , 'post_type' =>'page'));

2011-12-09

Ivaylo Draganov answers:

Hello,

The other guys have provided you with an excellent function. Here's a function to use for deletion:

/**
* Delete stuff upon user removal
*/
add_action( 'delete_user', 'wpq_delete_user_stuff', 15 );

function wpq_delete_user_stuff( $user_id ) {

// vars
$tax = 'category';
$user_info = get_userdata( $user_id );
$username = $user_info->user_login;

// check if term with that slug exists
if ( $user_term = get_term_by( 'slug', $username, $tax ) ) {

// delete the term associated with that user
wp_delete_term( $user_term->term_id, $tax );

}

// check for page
if ( $user_page = get_page_by_path( $username ) ) {

// delete the associated page bypassing the Trash (change the second parameter to false to use the trash)
wp_delete_post( $user_page->ID, true );

}

}


Cheers


basequatro comments:

Hello Ivaylo Draganov,

It worked great for depelting categorie, but not for page, maybe and issue that are subpages?


Ivaylo Draganov comments:

<blockquote>It worked great for depelting categorie, but not for page, maybe and issue that are subpages?</blockquote>

Yes, get_page_by_path() expects the path to the page as an argument. So if your user pages are not top level, then you need to prepend the path:

get_page_by_path( 'path/to/page/' . $username )


John Cotton and Cliff P are pointing you to something that you might've not known: <strong>each user by default gets his own page</strong>. Usually it's located at <em>http://yoursite.com/author/username</em> and it's generated by the template <em>author.php</em>. By default that template lists all the posts from the user, but it's possible to do just about anything with user by editing the template. And by modifying WP's rewrite rules it's possible to change the path to that auto-generated page - for example <em>http://yoursite.com/users/username</em>.

2011-12-09

John Cotton answers:

Am I the only one wondering why you are creating a page for a user?

I'm guessing that you want somewhere for them to go to see information specific to them, but since you're not adding any content on create, you just want the page to act as a place holder to create a slug for your portal.php template.

If that's correct - and you are never going to edit that page to hold html content - then you really shouldn't be adding a page like that....you should do it with rewrites.

I'm not saying it's bad (at worst it's a wasted database row and a minor impact on performance), but it's not a good way to do it.

JC


basequatro comments:

Kind of a CMS of wp, a simple cliente area, but not only for one page, that's why categories. I will take som etime to learn about slug and how this can solve some problems.


John Cotton comments:

<blockquote>Kind of a CMS of wp, a simple cliente area,</blockquote>

Sure - I've done that myself many times.

But you shouldn't need to create a page......you should just grab the url via a rewrite and have a custom template that does the work.

2011-12-09

Clifford P answers:

Are you familiar with each author getting their own page?

See this: [[LINK href="http://codex.wordpress.org/images/1/18/Template_Hierarchy.png"]]http://codex.wordpress.org/images/1/18/Template_Hierarchy.png[[/LINK]]
from here: [[LINK href="http://codex.wordpress.org/Template_Hierarchy"]]http://codex.wordpress.org/Template_Hierarchy[[/LINK]]

In your theme files, you could have <strong>author.php</strong>, author-<strong>$id</strong>.php, or author-<strong>$nicename</strong>.php


basequatro comments:

but i needed to be more than one page. maybe in next project i will look at this.

Anybody know another solution, WPMUdev could handle this too?


Clifford P comments:

How many pages do you want? What for? Understanding that might help advise. Are you a WPMU DEV member?