Snippets

cutiko Programatically create a post with the user name

Created by cutiko

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+/*This will create a post with the user name when the user is created*/
+add_action( 'user_register', 'new_user_insert_janitor_post', 999);
+
+function new_user_insert_janitor_post($user_id) {
+
+  // The $user variable for getting the user
+  $user = get_user_by('id', $user_id);
+
+    if( is_array( $user->roles ) ) {
+      if( in_array( 'administrator', $user->roles ) ) {
+        //Do nothing
+      } else {
+          $array = array(
+          'post_type'     => 'janitor',
+          'post_title'    => $user->nickname,
+          'post_status'   => 'publish',
+          'post_author'   => 1
+        );
+        wp_insert_post( $array );
+      }
+    }
+    
+}
+?>
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.