Insert data of custom metabox to database - php

How can I insert data values of custom metaboxes into a database to the corresponding field in a table?
This is how I tried to get the values for each form:
$l = $_POST['liens'];
$post_id = $_POST['post_ID'];
$langue = $_POST['lang'];
$qual = $_POST['qual'];
$type = $_POST['type'];
if (isset($l) and !empty($l) )
mysql_query("insert into blog_liens values('','".$post_id."','".$l."','".$langue."','".$qual."','".$type."',now(),'','1') ");
But it's not working.

Always use $wpdb when handling database in WordPress but that's not applicable here anyway.
Add this to the same file as your metabox.
function my_metabox_save_value( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! current_user_can( 'edit_post' ) ) return;
if ( isset( $_POST['lang'] ) )
update_post_meta( $post_id, 'lang', sanitize_text_field( $_POST['lang'] ) );
}
add_action( 'save_post', 'my_metabox_save_value' );
Repeat update_post_meta for each value you want to save and add your own checks if needed such as ! empty().
Ideally you would also be using a nonce but that's beyond the scope of the question. For further info please see: http://codex.wordpress.org/WordPress_Nonces and http://codex.wordpress.org/Function_Reference/add_meta_box

please escape the post parameters for example with mysql_real_escape_string
dont use mysql_* functions -> use mysqli or PDO for your Queries
Have you tried to debug? ;)
$result = mysql_query('SELECT foo FROM bar');
if(!$result ){
echo mysql_error();
exit;
}

Related

Save Post and Add Post Meta Executing Twice

I'm trying to add or update two post meta on saving a post. Add if it's a new post and update if existing, but this function creates four in the database instead of two meta.
add_action( 'save_post', 'add_rewards');
global $WCFM, $WCFMmp;
function add_rewards ($product_id){
if($product_id){
$post_type = get_post_type($product_id);
if($post_type == 'product'){
$product = wc_get_product( $product_id );
$reg_price = $product->get_regular_price();
$sal_price = $product->get_sale_price();
$pric = $product->get_price();
add_post_meta($product_id,'main_reward', $reg_price);
add_post_meta($product_id,'sub_reward', $sal_price);
}
}
}
As it explains in the save_post manual, you should use save_post_{$post->post_type} which minimizes the save_post calls on other post types. It's also a good idea to check for autosave.
Also, if you use update_post_meta instead of add_post_meta you'll end up with only one instance of each. As it explains in the manual for that function, it says:
If the meta field for the post does not exist, it will be added and its ID returned.
Can be used in place of add_post_meta().
add_action( 'save_post_product', 'so71077799_add_rewards', 99, 1 );
function so71077799_add_rewards( $product_id ) {
// Check to see if we are autosaving, if so, exit.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['_regular_price'] ) ) {
update_post_meta( $product_id, 'main_reward', number_format( floatval( $_POST['_regular_price'] ), '2' ) );
}
if ( isset( $_POST['_sale_price'] ) ) {
update_post_meta( $product_id, 'sub_reward', number_format( floatval( $_POST['_sale_price'] ), '2' ) );
}
}

Add, update or remove product custom field in WooCommerce

I use the following lines to save a Value to the database. I have one question regarding this code, when I delete input it still keeps it. The only way to delete that is to put (space) as input.
$field_key_pills_1 = 'custom_text_field_category_pills';
if ( isset( $_POST[$field_key_pills_1] ) && ! empty( $_POST[$field_key_pills_1] ) ) {
$attribute_pills_1 = wc_get_product( $post_id );
$attribute_pills_1->update_meta_data( $field_key_pills_1, sanitize_text_field( $_POST[$field_key_pills_1] ) );
$attribute_pills_1->save();
} else {
$attribute_pills_1 = wc_get_product( $post_id );
$attribute_pills_1 = delete_post_meta( $post_id, 'custom_text_field_category_pills' );
}
Please give me any tip that you think, can solve this problem.
Try instead the following revisited code using WC_Data delete_meta_data() method to remove product meta data (meta key + value):
$key_pills_1 = 'custom_text_field_category_pills';
$product_pills1 = wc_get_product( $post_id );
// Check that the product and the product input field exists
if ( is_a($product_pills1, 'WC_Product') && isset($_POST[$key_pills_1]) {
if ( ! empty($_POST[$key_pills_1]) ) {
$product_pills1->update_meta_data( $key_pills_1, sanitize_text_field($_POST[$key_pills_1]) ); // Set or update
} else {
$product_pills1->delete_meta_data( $key_pills_1 ); // remove
}
$product_pills1->save(); // Sync and save to database
}
It should work.

Bulk update custom post type at once in WordPress automatically

I have created a custom post type songs which has all the songs and now I want to sort it according to alphabets, which I have done using
function alphaindex_save_alpha( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
$slug = 'songs';
$letter = '';
if ( isset( $_POST['post_type'] ) && ( $slug != $_POST['post_type'] ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
$taxonomy = 'alpha';
if ( isset( $_POST['post_type'] ) ) {
$title = strtolower( $_POST['post_title'] );
// Get the first letter of the title
$letter = substr( $title, 0, 1 );
}
//set term as first letter of post title, lower case
wp_set_post_terms( $post_id, $letter, $taxonomy );
}
add_action( 'save_post', 'alphaindex_save_alpha' );
now this creates a taxonomy alpha which maps every songs based on the first alphabet for eg. Adele is mapped to A and so on.
I created this alphabet index after I had already put 2000 songs in my word press website and now for the songs to get mapped to the taxonomy I have to individually go to every post page and update the post and only then it appears.
But, doing this is a very tedious task and want a code that can do it automatically from the functions.php file.
I would recommend using posts_where filter instead of taxonomy. You do not need to add any extra data to your database to accomplish this.
add_filter('posts_where', 'starts_with_filter');
function starts_with_filter($sql){
if ( ! $query->is_main_query() || ! isset(get_query_var('starts_with')) {
return $query;
}
global $wpdb;
$startswith = substr ( get_query_var( 'starts_with' ), 0, 1);
$sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $startswith.'%' );
return $sql;
}

Wordpress bulk post meta update

I am trying to add the ability to update a meta value through Wordpress's bulk post edit menu. I have it working on a single post, but I can't figure out why it's not working for bulk.
// Add our text to the quick & bulk edit box
add_action('quick_edit_custom_box', 'on_quick_edit_custom_box', 10, 2);
add_action('bulk_edit_custom_box', 'on_quick_edit_custom_box', 10, 2);
function on_quick_edit_custom_box($column_name, $post_type) {
global $post;
$postMeta = get_post_meta( $post->ID, 'wpsl_locatorID', true );
if ('wpsl_locatorID' == $column_name && get_post_type() == 'location' ) {
echo "<fieldset class=\"inline-edit-col-right\" style=\"margin-top: 0;\">";
echo "<div class=\"inline-edit-col\">";
echo "<div class=\"inline-edit-group\">";
echo "<label class=\"alignleft\">";
echo "<span class=\"title\" for=\"wpsl_locatorID\">Movie ID</span>";
echo "<input type=\"text\" name=\"wpsl_locatorID\" id=\"wpsl_locatorID\" value=\"{$postMeta}\" />";
echo "</label>";
echo "</div>";
echo "</fieldset>";
};
}
// Update the post meta
add_action( 'save_post', 'locationMetaUpdate', 10, 2 );
function locationMetaUpdate( $post_id ) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
// if un-commented than the value won't update at all. I'm guessing b/c of WP's ajax function vs. reloading the page.
//if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if ( ! current_user_can( 'edit_posts' ) ) return;
// Make sure that it is set.
if ( ! isset( $_POST['wpsl_locatorID'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['wpsl_locatorID'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'wpsl_locatorID', $my_data );
};
Like I said above, this works for a single quick edit but not on bulk edit. I've tried to find a solution, but I'm sure it's something simple I'm missing.
So honestly I'm not sure why it's now working; but all I did was condense the action like on WP's save_post codex page.
// Update the post meta
add_action( 'save_post', 'locationMetaUpdate', 10, 3 );
function locationMetaUpdate( $post_id, $post, $update ) {
//print_r($_POST); die();
// if our current user can't edit this post, bail
if ( ! current_user_can( 'edit_posts' ) ) return;
// Make sure that it is set.
if ( isset( $_REQUEST['wpsl_locatorID'] ) ) {
update_post_meta( $post_id, 'wpsl_locatorID', sanitize_text_field( $_REQUEST['wpsl_locatorID'] ) );
}
}

How to make the custom meta boxes support for the visual composer in WordPress?

I am using the visual composer for the WordPress posts and pages actually for over all. But I want to make some custom meta boxes under the screen of the post editor. Actually already I have made the fields. But now I want to make those fields available in the visual composer. Actually I want to add those fields in the visual editor. How can I do that? Please help me with your valuable knowledge.
Here is my code of the meta boxes
<?php
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
function myplugin_meta_box_callback( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}
function myplugin_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
I see that you are echoing your fields as inputs. You need to use the wp_editor() function instead. It will take care of the wysiwyg (visual editor) field creation for you.

Categories