WooCommerce: add a few lists to wishlist by custom plugin - wordpress

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

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.

Call function in Wordpress after a custom post type updated/created with ACF

I want to call a function every time there is a change in a custom post type. Either publish, update or delete. In that function, I fetch all posts from that custom post type and create a json file that I export in a file.
add_action( 'transition_post_status', 'get_resources_data', 10, 3 );
function get_resources_data($new_status, $old_status, $post ) {
if ($post->post_type == 'resources') {
$args = array (
'post_type' => 'resources',
'post_status' => 'publish',
'posts_per_page' => -1
);
$queryResults = new WP_Query( $args );
if ( $queryResults->have_posts() ) {
//do my stuff here
//fetch acf fields with get_field()
//create json file
//export json file
}
}
}
The problem is that the custom post type has a few advanced custom fields which I include in the JSON file. However, when a new post is created, all ACF are null, while fields such as title and creation data are available. If I update a post, all ACF are fetched.
My impression is that transition_post_status is hooked before the ACF are stored in the Database. Should I use another action or do it with another way?
ACF actually supplies you with an action hook for exactly just that.
add_action('acf/save_post', 'get_resources_data'); - if you set the priority to below 10, the action is applied before the data is saved, if you emit the prio, or have it above 10, it is applied after the data is saved.
You can read more about the hook here : https://www.advancedcustomfields.com/resources/acf-save_post/

Dynamically add taxonomy term to custom post type when post is created

I've created a custom post type and taxonomy using Custom Post Type UI plugin. There is a form on the frontend that when filled out creates a new post in the custom post type Survey. I'd like for the title of the post (this is dynamically added based on another post) to be the taxonomy term, custom taxonomy is survey_category. I found this:
add_action('publish_survey', 'add_survey_term');
function add_survey_term($post_ID) {
global $post;
$survey_post_name = $post->post_name;
wp_insert_term( $survey_post_name, 'survey_category');
}
but it doesn't seem to work. I need it to insert the term and if the term already exists I need it to update the number of posts associated with that term.
You can use wp_set_object_terms() to add taxonomy to your post dynamically. This function will insert the term and if the term already exists, it will update the number of posts associated with that term. I have done small example with post name & taxonomy name you can try below code:
$name = $_POST['value']['post_name'];
$tax_name = $_POST['value']['tax_name'];
$args = array(
'post_title' => $name,
'post_type' => 'your_post_type',
'post_status' => 'publish',
'comment_status' => 'close',
'ping_status' => 'closed'
);
$pid = wp_insert_post($args);
wp_set_object_terms($pid, $tax_name, 'your_tax_slug', false);
For more reference of wp_set_object_terms() you can check this link.
Hope this helps you.

Add a user along with one custom post type

I am developing this plugin that admin can add a user in the backend and when user is created, plugin can automatically generate one custom post which I have added to the theme. The custom post will store user ID that is just created (or if it is possible make that user an author of the post)
I wonder if what I have mentioned above is possible practically. If anybody has any better advice, I am open for any suggestions.
Thank you in advance
I'm not sure that a unique custom post type per user is the best way to implement what you're wanting to achieve. If you have 100 users, you will have 100 custom post types making the wp-admin a nightmare as the left menu would grow with so many menu-items.
Wouldn't it be easier to just use a normal post type and then have the page that shows the user's dashboard filter the posts to only show posts where the user is the post_author? You could then add a hook to catch when a user registers and create the example post, you could modify the code below and add it to your functions.php:
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$userPostsCategory = 3;
// Create post object
$my_post = array(
'post_title' => 'Sample Story' ),
'post_content' => 'You can edit this or create a new story',
'post_status' => 'publish',
'post_author' => user_id,
'post_category' => array( $userPostsCategory )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
This method will lower the number of customisations you'd have to do to your themes and make management of the posts a little easier.
Further reading on this:
User registration hook
Inserting a post using wp_insert_post

Wordpress ACF import by script needs save for Repeater field

I just made a script for importing a JSON feed to ACF custom fields pro.
When I insert a field which is just a normal field a 'save' action by click is not needed after import.
update_field('rating', $route['rating'], $post_id);
Everything goes well.
However when its a repeater field I use this code:
$startlocation = array();
$startlocation[] = array(
'lat' => $route['startingSpot']['location']['lat'],
'lng' => $route['startingSpot']['location']['lng'],
'description' => $route['startingSpot']['nl'],
'direction' => $route['startingSpot']['direction']
);
update_field('startlocation', $startlocation, $post_id );
It needs a click after the creation of the imported post. When I run the import again, the fields are filled in.
Does anyone know if i need a different script, or give a programmed click of the save, to add all the custom field, after a created post by script?
Thanks in advance,
Peter
For a repeater field of act you have to use the field_key not the name.
This was the answer to my question.
$startlocation = array();
$startlocation[] = array(
'lat' => $route['startingSpot']['location']['lat'],
'lng' => $route['startingSpot']['location']['lng'],
'description' => $route['startingSpot']['nl'],
'direction' => $route['startingSpot']['direction']
);
update_field('field_dhjawhdwkwkhd', $startlocation, $post_id );

Resources