Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Here is my basic plugin code. I am just starting from scratch. I will explain the functionality. It is simple, While my plugin is active it will add a new meta box in add new post page just below the tags box. The user can add a value to my text box & i will save that value with post ID to my table. You can understand what is happening after reading my code.

<?php
/*
Plugin Name: Post Extender
Author: Jithesh Kt
Version: 1.0
*/

add_action( 'admin_menu', 'extra_meta_form' );
add_action( 'save_post', 'save_extra_meta' );
function extra_meta_form() {
    //global $theme_name;
    add_meta_box( 'post-meta-boxes', __('Extra Fileds'), 'extra_meta_form_view', 'post', 'side', 'default' );
}

function extra_meta_form_view()
{ ?>
<table class="form-table">
<p>Extra Meta filed :  <input type="text" id="custom_input" name="custom_input"/></p>
</table>
<?php }

function save_extra_meta($post_id)
{
    global $wpdb;
    $tablename = $wpdb->prefix."extra_post_meta";
    $query_result = $wpdb->insert($tablename, array('post_id'=>$post_id,
    'filed_name'=>"My Text",
    'value'=>$_POST['custom_input']
    ));

}
?>

Now after activating my plugin I clicked on Add New Added a post title, Content & my a value in newly added text box.

The result i am expecting in my table is :

Expected

But actually this what i got in my fresh table :(

Result

How did the data got duplicated ? From where did the other data came ?

This is driving me crazy. Running behind this rabbit since last two days. I tried many methods. Same result. Data is always duplicating & unknown data insertion too.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

The action save_post is also called during AJAX auto-save requests. But your value is not sent then, so you save an empty value for something that isn't set.

Start your save function with:

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;

See basic examples with more checks here and here.

share|improve this answer
    
Thanks. I did as u said. Modified my save function little bit : function save_extra_meta($post_id) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if(isset($_POST['custom_input'])) { global $wpdb; $tablename = $wpdb->prefix."extra_post_meta"; $query_result = $wpdb->insert($tablename, array('post_id'=>$post_id, 'filed_name'=>"My Text", 'value'=>$_POST['custom_input'] )); return; } } Now data duplication stopped. Still the function is running two times and creating two rows of data :( –  Jithesh Kt Apr 2 '13 at 6:29
    
You should use the regular post_meta table and update_post_meta(). –  toscho Apr 2 '13 at 6:33
    
Mate i am dealing with wpdb. I have log way to go and need to make lot of database stuff. :) –  Jithesh Kt Apr 2 '13 at 6:41
    
Then you should check first if there is already a value for this post ID. And please escape $_POST['custom_input'] before you send it to the database. –  toscho Apr 2 '13 at 6:44
    
Checking post id is waste of time i think. Because in database u can see post id is always getting some values. See this function. This also inserting values two times with fake/not existing post id : pastebin.com/FBfsmZFu –  Jithesh Kt Apr 2 '13 at 6:49
add comment

When you reload your admin page the wordpress fires the 'save_post' action through AJAX and send metaboxes values blank. Resulting the blank fields. You can retain your fields value by

    add_action('save_post', 'custom_function');

    function custom_function($postid) {
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
            return $postid;
        }
        // Your functionality
    }
share|improve this answer
    
Any reason to copy my answer? :) –  toscho Apr 2 '13 at 6:34
    
The code i have written is pretty simpler than yours and give a clear idea where to add these AUSTOSAVES –  Akanksha Sharma Apr 2 '13 at 6:39
    
Look at my code & result :( pastebin.com/FBfsmZFu i.imgur.com/HRpqDLs.jpg –  Jithesh Kt Apr 2 '13 at 6:46
    
Look @jitesh every time you run wp_insert. The problem with your code is each time you save your post, an entry is been inserted in your table. Try to use update if key exists rather than inserting. Every time your data get saved as a seperate entry because when you reload the admin page it fires a AJAX Request to your save post action and inserting record on every update. –  Akanksha Sharma Apr 2 '13 at 9:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.