I am creating a new blog post Plugin in Wordpress form Created Successfully how to Store the form data in database
register_activation_hook(__FILE__,'create_books_table');
register_deactivation_hook(__FILE__,'truncate_book_table');
add_action('init','book_assets');
add_action("admin_menu", "my_book_plugin_menus");
Here is a example of how to insert data to database using $wpdb.
global $wpdb;
$wpdb->insert(
'table_name_here',
array(
'column1' => 'value1',
'column2' => 123
),
array(
'%s',
'%d'
)
);
Visit https://codex.wordpress.org/Creating_Tables_with_Plugins for more .
Related
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.
I'm using a plugin called woo-get to add a product attribute called 'prod_hsn_id' which add a filed called HSN code at product edit page,
I'm also using a pdf invoice plugin called woocommerce pdf invoice to generate pdf invoice.
Now I want to display the HSN code on the invoice.
I am having a very hard time to get it to work, I tried searching online and contacting the plugin author, and he said that it can be retrieved using the WordPress get post meta.
But
This the function the plugin use to create the custom field.
public function fn_add_product_custom_meta_box() {
woocommerce_wp_text_input(
array(
'id' => 'hsn_prod_id',
'label' => __('HSN Code', 'woocommerce' ),
'description' => __( 'HSN Code is mandatory for GST.', 'woocommerce' ),
'custom_attributes' => array( 'required' => 'required' ),
'value' => get_post_meta( get_the_ID(), 'hsn_prod_id', true )
)
);
}
In the Invoice Template file I'm trying to display the HSN Code using
<?php $meta = get_post_meta( $post_id , 'hsn_prod_id', true ); ?>
<?php $meta = get_post_meta( get_the_ID(), 'hsn_prod_id', true ); ?>
Sources :
Support
1. https://wordpress.org/support/topic/print-hsn-in-invoice/
https://wordpress.org/support/topic/which-pdf-plugin-will-display-the-hsn-field-and-gst-number/
Product details will be under line_items of order. Can you share your invoice template code where you are trying to access product id?
You will have order object, using that object you can retrieve all products related to that order. You need to execute on foreach loop (that should be there already).
You can see $product at line 77 of original template file of plugin. You can get id using $product->get_id().
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 am wondering how to insert form input value to wordpress database.
In a wordpress theme, I want to add an input element and insert it to database.
Clicking the submit button, I think there is no effect to insert for the following codes.
wordpress_theme_file.php
<form id=”form_reply” action=”database.php” method=”post”>
…
<input type=”text” id=”newValue” name=”newValue” />
…
</form>
database.php
<?php
global $wpdb;
$inputValue = $_POST[‘ newValue ‘];
$wpdb->insert(‘wp_table_name’, ‘field_name’=>$inputValue); // wp_table_name and field_name in database
?>
You can insert to wordpress table like this
global $wpdb;
$inputValue = $_POST['newValue'];
$wpdb->insert(
'table_name',
array(
'column1' => $inputValue
),
array(
'%s' // if the field type is string
)
);
for more info
Sample code which help you to insert data to database in wordpress :
<?php
global $wpdb
$address = 'address';
$firstn = "First Name';
$wpdb->insert( 'users', array( 'address' => $address, 'first_name' => $firstn ), array( '%s', '%s' ) )
?>
In above "users" is table name and Fields are : address and first name.
Reference : http://codex.wordpress.org/Function_Reference/wpdb_Class#INSERT_rows