Wordpress Create Custom Post on New User Register - wordpress

In Wordpress I'm Trying to Create Custom Post on New User Register of specific user role "author"
For This I try to figure out this Code in Function.php
add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles=$user_info->roles;
if ($user_roles == 'author') {
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_type' => 'CustomPost', // <- change to your cpt
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
But Not Success. After I add above code no error working fine but it not triggering automatic and not creating new custom post
I simple want that whenever I add new Author / New Author Register it create one Custom Post with title as same as username. and publish it

The role your are checking with is not correct, $user_info->roles returns an array, not a string. Find the modified code below,
add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles = $user_info->roles;
// New code added
$this_user_role = implode(', ', $user_roles );
if ($this_user_role == 'author') {
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'CustomPost', // <- change to your cpt
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
Hope this helps.

Related

Automatically create a post for each user on wordpress site

I'm trying to automatically great a custom post (users) upon registering a new user to my site. I'm just barely familar with php, but I've been working off another question from stackoverflow: Automatically create a post for each user using wp_insert_post
I would ideally love to create a page upon registering or updating user information that carries over custom fields. I've used ACF to create custom fields associated with users (listed in the code as lowercase variables) and custom fields associated with the to be created custom posts (listed in the code as uppercase variables).
Thank you for any help you can offer!
function create_authors_page( $user_id ) {
$the_user = get_userdata( $user_id );
$new_user_name = $the_user->user_login;
$PostSlug = $user_id;
$PostGuid = home_url() . "/" . $PostSlug;
$member_bio = get_field('member_bio');
$contact_info = get_field('contact_info');
$member_affiliation = get_field('member_affiliation');
$my_post = array( 'post_title' => $new_user_name,
'post_type' => 'users',
'post_content' => '',
'post_status' => 'publish',
'post_theme' => 'user-profile',
'guid' => $PostGuid );
$NewPostID = wp_insert_post( $my_post );
$Member_Bio = $member_bio;
$Contact_Info = $contact_info;
$Member_Affiliation = $member_affiliation;
update_post_meta( $NewPostID, $Member_Bio, $Contact_Info, $Member_Affiliation );
return $NewPostID;
}
add_action('publish_members', 'create_authors_page');
Your hook is incorrect, use user_register action hook which fires after a user has registered and passes $user_id as a variable:
add_action('user_register', 'create_authors_page');
function create_authors_page( $user_id ) {
// do your stuff
}
You can also use profile_update hook that trigger each time user update profile.

Update Post Title when you create new Custom Post Type with ACF

I have removed the default Title field from a specific Custom Post Type in Wordpress. But on the post table, it is shown as (no title). Obviously :P
What I try to do is to use an ACF field as the title. At the moment, I do it like this
function my_acf_update_value( $value, $post_id, $field ) {
global $_POST;
$new_title = get_field('username', $post_id);
$new_slug = sanitize_title( $new_title );
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug
);
// Update the post into the database
wp_update_post( $my_post );
return $value;
}
add_filter('acf/update_value/name=username', 'my_acf_update_value', 10, 3);
The problem is that it only works when I update a custom post type and not when I create one. I wasn't able to find a proper filter on ACF documentation. Any idea?

How do I add a new page using code thru a Wordpress plugin?

I followed the chosen answer here -> How to create new page in wordpress plugin?
and I added the following code in a new Wordpress plugin folder and file and then activated in the Wordpress admin menu. Yet I don't have a new page created when I go to the slug demosite.com/custom/
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page('custom_page', 'custom', 'administrator','custom', 'custompage');
remove_menu_page('custom');
}
Do I have to do something special to make my Wordpress plugin code work? I really need to be able to add a new page using my plugin functionality.
For create fronted page when plugin activation used register_activation_hook() like below.
register_activation_hook() function registers a plugin function to be run when the plugin is activated.
The first thing we do on activation is check that the current user is allowed to activate plugins. We do this using the current_user_can function
Finally, we create our new page, after we check that a page with the same name does not exist
register_activation_hook( __FILE__, 'register_newpage_plugin_activation' );
function register_newpage_plugin_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
global $wpdb;
if ( null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = 'new-page-slug'", 'ARRAY_A' ) ) {
$current_user = wp_get_current_user();
// create post object
$page = array(
'post_title' => __( 'New Page' ),
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'page',
);
// insert the post into the database
wp_insert_post( $page );
}
}
Here is a full list of parameters accepted by the wp_insert_post function
After plugin active successfully you can access your page using demosite.com/new-page-slug/
I'm not sure if you intended for the page to be created only once if so you should do it during plugin activation.
You might want to consider the following pseudo-ish code:
register_activation_hook( __FILE__, 'moveFile' );
function moveFile(){
if( check if post exists ){
wp_insert_post() # obviously title is "whatever", following convention
#move the file to themes folder
$source = plugin_dir_path(__FILE__) . "page-whatever.php";
$destination = get_template_directory() . "/page-whatever.php";
$cmd = 'cp ' . $source . ' ' . $destination;
exec($cmd);
}
}
It's similar to the code answered by Ankur, but this sample let you have a custom page. Caveat, my method uses exec() command.
I hope this helps.
/* If you want create a page with syn page template on plugin activation so see below example */
register_activation_hook( __FILE__, 'activate' );
function activate() {
$the_slug = 'our-services';
$args = array(
'name' => $the_slug,
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
if(empty($my_posts)){
$my_post = array(
'post_title' => 'Our Services',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
'post_name' => 'our-services'
);
// Insert the post into the database
$post_id=wp_insert_post( $my_post );
update_post_meta( $post_id, '_wp_page_template', 'page-templates/our-services.php' );
}
}
/* page-template - our-services.php* /
<?php
/*
* Template Name: our-services
*/
get_header();
get_footer();
?>

Wordpress: wp_insert_post() firing multiple times

I'm building a simple WP plugin that's supposed to create a new post and save some meta for the post. I created a function with the functionality and for the time being I hooked it to the 'init' event to check if it works.
add_action( 'init', 'my_func' );
function my_func() {
$my_post = array(
'post_title' => 'Some Post Title',
'post_name' => 'some-post-title',
'post_type' => 'custom-post-type',
'post_status' => 'publish'
);
$inserted_post_id = wp_insert_post($my_post);
if($inserted_post_id != 0) {
add_post_meta($inserted_post_id, 'some-key', 'some-value');
add_post_meta($inserted_post_id, 'some-other-key', 'some-other-value');
echo 'SUCCESS';
} else {
echo 'ERROR';
}
}
Now, whenever I reload the admin page, I get the 'SUCCESS' message echoed out, and I also get 4-6 new post called 'Some Post Title', and 4-6*2 new entries in the postmeta table. The 'SUCCESS' message echoes only once, meaning the function runs only once, still I get the data inserted to the database multiple times. What am I doing wrong?
Check before going to insert new post if already exists or not. So get_page_by_title('Some Post Title') == false then only insert new post.
add_action( 'init', 'my_func' );
function my_func() {
$my_post = '';
if( get_page_by_title('Some Post Title','OBJECT','custom-post-type') == NULL )
$my_post= array(
'post_title' => 'Some Post Title',
'post_name' => 'some-post-title',
'post_type' => 'custom-post-type',
'post_status' => 'publish'
);
wp_insert_post( $my_post );
}

Wordpress - Create a page when creating a user

I would like to create a function that creates a page in wordpress that uses a specific page title, specific URL, and is under an already exisiting parent page, when ever a new user is created.
An example:
Creating a new user call user1, will also create a new page called User1's Page with a slug of user1s-page under a specified parent page ID.
I didn't test this while writing but it should do the trick. Make sure you set $post_parent to the ID of the page you want to use as the parent. It's currently set to 0 so no parent.
function wpse_user_registration_create_page( $user_id ) {
// Get the new user by their ID.
$new_user = get_user_by( 'id', $user_id );
// Check a user was found.
if ( ! $new_user )
return;
// Create the post title. E.g. User1's Page
$post_title = ucfirst( $new_user->display_name ) . "'s Page";
// SET THE ID OF THE PARENT PAGE HERE!!!
$post_parent = 0;
$post_args = array(
"post_name" => sanitize_title( $post_title ),
"post_title" => $post_title,
"post_type" => "page",
"post_status" => "publish",
"post_author" => $new_user->ID,
"post_parent" => $post_parent,
);
wp_insert_post( $post_args );
}
add_action( 'user_register', 'wpse_user_registration_create_page' );
You will need to hook into the register user hook, here;
http://codex.wordpress.org/Plugin_API/Action_Reference/user_register
Then build a function which inserts a post (page) using this;
http://codex.wordpress.org/Function_Reference/wp_insert_post

Resources