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.
Related
I'm building a WordPress Page for course registration. All I want the plugin to do is send the filled in form details to my email ID and send an email to the user that he/she has successfully registered for the course. I don't need users to signup with username and password.
I've tried my luck with WP Forms but it only seems to have the option to forward the email to me and not the user.
Any suggestion on which plugin I should use?
As #Hughes mentioned, you cant use wpcf7, and just hook on it to insert custom post on every query.
// Hook on wpcf7
add_filter( 'wpcf7_mail_components', 'do_on_cf7_submit', 50, 2 );
function do_on_cf7_submit($mail_params, $form = null) {
// Empty post content
$content = '';
// set post content if field not empty
if ($_POST['field-name'] != '') {
$content .= 'Field Name Label: '.$_POST['field-name'] ;
}
// insert post if content not epmty
if ($content != '') {
insertQueryPost($_POST['email'], $content);
}
// allow cf7 to do his stuff
return $mail_params;
}
// insert custom post type "query", don't forget to setup your custom post type first
function insertQueryPost($title, $content) {
// insted of proper post slug, just make a hashed slug, when setting custom post type, set it to not public and not search-able
$t = time();
$thash = md5($t);
$my_query = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $content,
'post_type' => 'query',
'post_name' => $thash,
'post_status' => 'publish',
'post_author' => 1
);
$data = wp_insert_post( $my_query );
}
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.
I have custom plugin for first adding lists to Wishlist plugin (Woocommerce) by users.
I have custom step by step form, where user can choose number of lists (from 1 to 10) and enter titles and descriptions for these new lists.
There is Ajax request on the last step of my form.
How do I add these lists to database?
I'm trying to add by wp_insert_post( $my_post ) but I should add settings for postmeta table too.
You could save the form results as custom post type with the results as custom fields.
Set the post type as not public and not set to not be in search results.
If they is no logic preformed on the wishlist you could set the data to array, and save it serialized in one field.
To send the data from front end (user page) to back end (server) you could or use wp ajax admin or through wp-rest api
Save fields to custom post type example. Could be the field name is different in your site so set to according to your fields key
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'event'
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
// Updating the meta data (custom fields values)
if ( isset( $_POST['_wishlist_email'] ) ) {
update_post_meta( $post_id, '_wishlist_email', sanitize_text_field( $_POST['_wishlist_email'] ) );
}
More info about saving custom fields in docs
If this wishlist is from a ready plugin, you could look in the plugin code to see how the plugin handles the saving wishlist data.
I have found:
WC_Wishlists_Wishlist::create_list($tittle));
I'm using NinjaForm plugin on wordpress. Here how to search and retrieve data:
<?php
$args = array(
'form_id' => $form_id,
'user_id' => $user_id,
'fields' => array(
'34' => 'checked',
'54' => 'Hello World',
),
);
// This will return an array of sub objects.
$subs = Ninja_Forms()->subs()->get( $args );
// This is a basic example of how to interact with the returned objects.
// See other documentation for all the methods and properties of the submission object.
foreach ( $subs as $sub ) {
$form_id = $sub->form_id;
$user_id = $sub->user_id;
// Returns an array of [field_id] => [user_value] pairs
$all_fields = $sub->get_all_fields();
// Echoes out the submitted value for a field
echo $sub->get_field( 34 );
}
What I want to do is searching by DateTime fields. How do I do that?
I have tried change args like this but result same.
$args = array(
'form_id' => 5,
'date_modified'=> '2015-07-25 3:19:09'
);
or like this
$args = array(
'form_id' => 5,
'date_modified'=> '< 2015-07-25 3:19:09'
);
Did I do wrong?
Find Ninja DB Table:
Go into your database using phpmyadmin or something and find the table Ninja Forms is using. Hopefully they're using their own table. If not, you can search each wp table for some of the arg data that you know returns a form from Ninja_Forms(). Or go into the Ninja plugin code and try and find where they interact with the db to find which table they write into.
Write your own mysql search code:
Instead of using Ninja's class to search, use wordpress's built in mysql search and throw in the table you found in step 1.
GLOBAL $wpdb;
$wpdb->get_results($wpdb->prepare("SELECT * FROM `ninja_table` WHERE `date_modified` = %s", $strDate));
I haven't tested, but this would be my course of action.
Use begin_date and end_data parameters to get the submissions
$args = array(
'form_id' => $form_id,
'begin_date' => '2015-07-20 0:00:00',
'end_date' => '2015-07-25 3:19:09'
);
$subs = Ninja_Forms()->subs()->get( $args );
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