Wordpress - Create a page when creating a user - wordpress

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

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.

Get the value of a field in gravity forms and use that value as a php parameter?

I am trying to dynamically populate two dropdown fields in a Gravity Forms form. The first field dynamically populates with the terms available in a custom post type. I want the second dynamically populated field to contain the list of all post titles within the custom post type AND have those titles filtered by the term selected in the previous dropdown. Is it possible to get the value of a dropdown within Gravity Forms and pass that value as a parameter in $args to use the get_posts($args) function?
I started using the following tutorial as a guide. https://docs.gravityforms.com/dynamically-populating-drop-down-fields/
add_filter( 'gform_pre_render_3', 'populate_procedures' );
add_filter( 'gform_pre_validation_3', 'populate_procedures' );
add_filter( 'gform_pre_submission_filter_3', 'populate_procedures' );
add_filter( 'gform_admin_pre_render_3', 'populate_procedures' );
function populate_procedures( $form ) {
// Procedure Category Dropdown
foreach ( $form['fields'] as &$field ) {
The first field. The following code populates a dropdown field containing a list of all of the terms within a custom post type (procedure):
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedure_categories' ) === false ) {
continue;
}
$terms = get_terms( array(
'taxonomy' => 'procedure_category',
'orderby' => 'name',
'order' => 'ASC',
) );
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
//$posts = get_posts( 'post_type=procedure&numberposts=-1&post_status=publish' );
$choices = array();
foreach ( $terms as $term ) {
$choices[] = array( 'text' => $term->name, 'value' => $term->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure Category';
$field->choices = $choices;
The second field. The following code dynamically populates the field with all of the the post titles of the custom post type (procedure). I want to filter these results based upon the value selected above.
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedures' ) === false ) {
continue;
}
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => 'cardiovascular',
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure';
$field->choices = $choices;
}
return $form;
}
The second dynamically populated field successfully pulls in the filtered list of post titles based on the $args if I explicitly listed the term (in the example above I used 'cardiovascular'). What I am wondering is if there is a way to grab the value of the previous field and use that to filter the results of the second field (without having to reload the page). Any ideas? Does Gravity Forms have a functionality like this built in?
Using this method, you would need to use multiple pages and add the second Drop Down field to the second page on the form. Then, when the user submits the first page, you can access the value of the first Drop Down from the $_POST. Gravity Forms has a helper method for doing this called rgpost(). Here's what your $args might look like:
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => rgpost( 'input_FIELDID' ),
);
Replace FIELDID with the field ID of your first Drop Down.
With that said, if you want to accomplish this without having to touch any code, try Gravity Forms Populate Anything.
https://gravitywiz.com/documentation/gravity-forms-populate-anything/

Wordpress plugin for course registration

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

Wordpress Create Custom Post on New User Register

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.

Adding User Profile Fields to Wordpress Buddypress to Include "Favorites"

I'm trying to figure out a way for members on my Wordpress (Buddypress) site to pick their "favorite movies, books, etc."
It would be nice if, instead of members simply typing a list of these things, they could select from books already in the system, and add more as the please in the future.
I'm hoping that there is an easy answer to this, such as a plugin that I can use, or, at least, modify. Does anyone know of anything that I can look into?
Your title asks how to add a user profile field. Here is the code to add as many fields as you like. Once you add the field, you can easily place additional inputs or options on the custom tab page for users to enter their own favorites.
function my_test_setup_nav() {
global $bp;
$parent_slug = ‘test’;
$child_slug = ‘test_sub’;
//name, slug, screen, position, default subnav
bp_core_new_nav_item( array(‘name’ => __( ‘Test’ ),’slug’ => $parent_slug,’screen_function’ => ‘my_profile_page_function_to_show_screen’,'position’ => 40,’default_subnav_slug’ => $child_slug ) );
/* Add the subnav items to the profile */
// name, slug, parent_url, parent slug, screen function
bp_core_new_subnav_item( array( ‘name’ => __( ‘Home’ ), ‘slug’ => $child_slug, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen’ ) );
bp_core_new_subnav_item( array( ‘name’ => __( ‘Random Page’ ), ‘slug’ => ‘random’, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen234′ ) );
}
function my_profile_page_function_to_show_screen() {
//add title and content here – last is to call the members plugin.php template
add_action( ‘bp_template_title’, ‘my_profile_page_function_to_show_screen_title’ );
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen_title() {
echo ‘wptaskforce title’;
}
function my_profile_page_function_to_show_screen_content() {
echo ‘wptaskforce content’;
}
//random page content:
function my_profile_page_function_to_show_screen234() {
//add content here – last is to call the members plugin.php template
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen234_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen234_content() {
echo ‘This is a random page.’;
}
add_action( ‘bp_setup_nav’, ‘my_test_setup_nav’ );

Resources