logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Custom Function When Add User

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

}

This question has been answered.

basequatro | 12/09/11 at 12:24am Edit

Previous versions of this question: 12/09/11 at 1:15am | 12/09/11 at 2:57am

(7) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/09/11
    12:35am
    Luis Abarca says:

    Did you try this action ?

    http://codex.wordpress.org/Plugin_API/Action_Reference/user_register ??

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

    Previous versions of this answer: 12/09/11 at 12:35am

    • 12/09/11 12:44am

      Luis Abarca says:

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

    • 12/09/11 12:54am

      Luis Abarca says:

      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(
      'post_type' => 'page',
      'post_title' => $username, // this could be changed to $user_info->display_name or nickname
      '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
      update_post_meta($post_id, '_wp_page_template' , 'portal.php');
      }

    • 12/09/11 1:40am

      basequatro says:

      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.

    • 12/09/11 9:26am

      Luis Abarca says:

      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 ?

    • 12/10/11 12:00am

      basequatro says:

      list more posts private only for one user

  • avatar
    Last edited:
    12/09/11
    12:37am
    Pixel Coder says:

    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


  • avatar
    Last edited:
    12/09/11
    12:43am
    Hai Bui says:

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

    }

    Previous versions of this answer: 12/09/11 at 12:38am | 12/09/11 at 12:42am | 12/09/11 at 12:43am

    • 12/09/11 1:40am

      basequatro says:

      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.

    • 12/09/11 3:11am

      Hai Bui says:

      Function when delete a user:

      function my_delete_user($user_id) {

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

  • avatar
    Last edited:
    12/09/11
    12:40am
    Arnav Joy says:

    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

    • 12/09/11 2:56am

      basequatro says:

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

      }

    • 12/09/11 4:03am

      Arnav Joy says:

      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

    • 12/09/11 11:59pm

      basequatro says:

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


      //}

      }

    • 12/11/11 11:31pm

      Arnav Joy says:

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

  • avatar
    Last edited:
    12/09/11
    3:08am
    Ivaylo Draganov says:

    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

    • 12/09/11 11:27pm

      basequatro says:

      Hello Ivaylo Draganov,

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

    • 12/10/11 8:56am

      Ivaylo Draganov says:

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


      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: each user by default gets his own page. Usually it's located at http://yoursite.com/author/username and it's generated by the template author.php. 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 http://yoursite.com/users/username.

  • avatar
    Last edited:
    12/09/11
    4:30am
    John Cotton says:

    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

    • 12/09/11 11:28pm

      basequatro says:

      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.

    • 12/10/11 4:27am

      John Cotton says:

      Kind of a CMS of wp, a simple cliente area,


      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.

  • avatar
    Last edited:
    12/09/11
    12:08pm
    Clifford P says:

    Are you familiar with each author getting their own page?

    See this: http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
    from here: http://codex.wordpress.org/Template_Hierarchy

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

    • 12/09/11 11:49pm

      basequatro says:

      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?

    • 12/10/11 5:28am

      Clifford P says:

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

This question has expired.



basequatro had additional discourse to offer.

Gabriel Reguly, Julio Potier, Jurre Hanema voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.