form input submit to database using $wpdb - wordpress

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

Related

Get all values of a ACF field in all posts and the links to the posts

I have created an ACF field where I can add 1 keyword per post. I want to get now a list of all keywords set in all my posts and sort it by alphabet and add a link to the post where it was found. Every keyword will be unique. So it would be a kind of table of content. How can I do that programatically? I have no idea right now on how to loop through posts and get field values.
Place the following function in functions.php . Use it directly in your template or as shortcode or w/e
function keywords_post_list() {
//We build our query for posts containing any value in meta field
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'keyword', //change with your meta key
'value' => '',
'compare' => '!='
)
);
$query = new WP_Query($args);
global $post;
$items = array();
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
//Looping each post we collect the keyword and the link for a post.
//Grab any other information if you need and add in the array
$keyword = get_post_meta($post->ID,'keyword',true);
$link = get_the_permalink($post->ID);
//Our array
$items[] = array('keyword'=> $keyword,'link' => $link);
endwhile;
endif;
// We need to sort results by keyword currently ASC
array_multisort(array_column($items, 'keyword'), $items);
// If we need to sort DESC uncommnet bellow coment above
// array_multisort(array_column($items, 'keyword'),SORT_DESC, $items);
// error_log(print_r($items,true));
if($items):
echo '<ol class="keyword-list">';
foreach($items as $item):
$keyword = $item['keyword'];
$link = $item['link'];
echo '<li>'.$keyword.'</li>';
endforeach;
echo '</ol>';
endif;
}

Adding an internal value to all woocommerce products

I simply want to add a field to all my products on woocommerce which isn't visible to customers.
This will be useful when exporting the product list to csv and calculating based on this value later.
Probably overlooked something obvious.
Thanks in advance.
First, you need to get all of your products. After that, check this meta key is already exists. If yes, then update the value; otherwise, add it as a new meta key.
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$products = new WP_Query( $args );
while ( $products->have_posts() ) : $products->the_post();
if(!empty(get_post_meta($post->ID, '_your_custom_meta_key', true))){
update_post_meta($post->ID, '_your_custom_meta_key', 'Your custom value');
}else{
add_post_meta($post->ID, '_your_custom_meta_key', 'Your custom value');
}
endwhile;
wp_reset_query();
?>

Insert form data in database using wordpress wpdb

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 .

How to hook in a custom query to the loop with Wordpress

I want to create a loop and query posts by their author role. And display the results of a search term based on the authors role.
I've tried creating a function to alter the query's where clause:
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);
// If the query has data
if($query->have_posts() ) :
// Post loop
while ($query->have_posts() ) :
// Setup post data
$query->the_post();
?>
<!-- Do HTML markup and template tags here, eg. the_content(), the_title() etc.. -->
<h1>You're a post from administrator - <?php the_title(); ?></h1>
<?php get_template_part( 'template-parts/content', 'search' ); ?>
<?php
endwhile;
// End "If the query has data"
endif;
I'm trying to add a WP_Query to the loop but this is where I get stuck with the results not getting filtered by role so I'm fairly certain I must be implementing this wrong - this is the first time I've tried to do something like this so sorry if it's a dump question but I can't find an answer to my question so if anyone can point me in the right direction that would be amazing!
Any advice welcome, thank you!
You can try the posts_where
hook.
UPDATE:
Why don't you use the default WP_Query author arg instead hook.
You query will be :
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);

$wpdb->insert() is giving error undefined function

I have created a new file inside wp-content/theme/mytheme folder.
Inside the file I have written simple query
global $wpdb;
$insert= $wpdb->insert('wp_test', array(
'orderID' =>$_GET['orderID'],'amount'=>$_GET['amount'],'acceptance'=>$_GET['ACCEPTANCE'],'status'=>$_GET['STATUS'],
));
I am getting error "Call to undefined function". Do I have to include file inside this file?
Try this : include wp-load.php at beginning of file.
File is located at theme folder.
require_once('../../../wp-load.php'); //<-----please include this
global $wpdb;
$insert= $wpdb->insert('wp_test', array(
'orderID' =>$_GET['orderID'],'amount'=>$_GET['amount'],'acceptance'=>$_GET['ACCEPTANCE'],'status'=>$_GET['STATUS'],
));
According to this document : https://codex.wordpress.org/Class_Reference/wpdb
you must change your code to this:
global $wpdb;
$wpdb->insert(
'wp_test',
array(
'orderID' => $_GET['orderID'],
'amount' => $_GET['amount'],
'acceptance' => $_GET['ACCEPTANCE'],
'status' => $_GET['STATUS'],
),
array( '%d', '%d', '%s','%s' )
);
By these ways , You can do it easily,
First define $wpdb globally as like global $wpdb;
require_once('../wp-load.php'); // relative path
<?php
$wpdb->insert("wp_submitted_form", array(
"col_nmae" => $value,
));
?>
Second Way is
$sql = $wpdb->prepare(
"INSERT INTO `wp_submitted_form`
(col_nmae)
values ($val)");
$wpdb->query($sql);
Third is
$sql = "INSERT INTO `wp_submitted_form`
(col_nmae)
values ($val)";
$wpdb->query($sql);
If you are beginner then read more here https://codex.wordpress.org/Class_Reference/wpdb

Resources