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.

So I'm having an issue with my custom fields in my custom post type. For whatever reason the fields save and then clear somewhat randomly... I'm sure it's not random but I'm not sure what's triggering this to happen. Here's the code for my custom post type:

    // Custom Post Type: Strategic Giving
add_action('init', 'giving_register');

function giving_register() {

  $labels = array(
    'name' => _x('Invest Items', 'post type general name'),
    'singular_name' => _x('Item', 'post type singular name'),
    'add_new' => _x('Add New', 'giving item'),
    'add_new_item' => __('Add New Item'),
    'edit_item' => __('Edit Item'),
    'new_item' => __('New Item'),
    'view_item' => __('View Item'),
    'search_items' => __('Search Items'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
    );

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'menu_icon' => get_stylesheet_directory_uri() . '/images/cpt-giving.png',
    'rewrite' => array( 'slug' => 'giving_items' ),
    'capability_type' => 'post',
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title','thumbnail','editor'),
    'paged' => false,
    ); 

  register_post_type( 'givings' , $args );
}

register_post_type( 'givings' , $args );

add_action("admin_init", "giving_admin_init");

function giving_admin_init(){
  add_meta_box("giving_info-meta", "Item Options", "giving_info", "givings", "side", "high");
}

function giving_info(){
  global $post;
  $custom = get_post_custom($post->ID);
  $amount = $custom["amount"][0];
  $monthly = $custom["monthly"][0];
  $user_entered_value = $custom["user_entered_value"][0];
  $item_name = $custom["item_name"][0];
  $special = $custom["special"][0];
  ?>
  <div style="text-align: right;">
    <p>
      <label for="amount"><strong>Amount:</strong></label>  
      <input style="width: 180px" type="text" name="amount" id="amount" value="<?php echo $amount; ?>" />
      <em>Example: 30.00</em>
    </p>
    <p>
      <label for="monthly"><strong>Monthly?</strong></label>  
      <?php if ($monthly == 'on') { ?>
        <input type="checkbox" name="monthly" id="monthly" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="monthly" id="monthly" />
      <?php } ?>      
    </p>
    <p>
      <label for="special"><strong>Special Item?</strong></label>  
      <?php if ($special == 'on') { ?>
        <input type="checkbox" name="special" id="special" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="special" id="special" />
      <?php } ?>      
    </p>
    <p>
      <label for="user_entered_value"><strong>Allow Giver To Enter Custom Value?</strong></label>  
      <?php if ($user_entered_value == 'on') { ?>
        <input type="checkbox" name="user_entered_value" id="user_entered_value" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="user_entered_value" id="user_entered_value" />
      <?php } ?>      
    </p>
    <p>
      <label for="item_name"><strong>Item Name:</strong></label>              
      <input style="width: 180px" type="text" name="item_name" id="item_name" value="<?php echo $item_name; ?>" /><br />
      If item is a <em>per item</em> then enter the name of the singular item. <em>Example: Chair - which will be displayed as "30.00 per Chair"</em>
    </p>
    <p style="text-align: left;">
      Strategic Giving photo must be horizontal and uploaded/set as the <strong>Featured Image</strong> (see below).
      <em>Do not add photo to content area.</em>
    </p>
  </div>
  <?php }  

add_action('save_post', 'giving_save_details_amount');
add_action('save_post', 'giving_save_details_monthly');
add_action('save_post', 'giving_save_details_user_entered_value');
add_action('save_post', 'giving_save_details_item_name');
add_action('save_post', 'giving_save_details_special');

function giving_save_details_amount(){
  global $post;
  update_post_meta($post->ID, "amount", $_POST["amount"]);
}

function giving_save_details_monthly(){
  global $post;
  update_post_meta($post->ID, "monthly", $_POST["monthly"]);
}

function giving_save_details_user_entered_value(){
  global $post;
  update_post_meta($post->ID, "user_entered_value", $_POST["user_entered_value"]);
}

function giving_save_details_item_name(){
  global $post;
  update_post_meta($post->ID, "item_name", $_POST["item_name"]);
}

function giving_save_details_special(){
  global $post;
  update_post_meta($post->ID, "special", $_POST["special"]);
}

add_action("manage_pages_custom_column",  "givings_custom_columns");
add_filter("manage_edit-givings_columns", "givings_edit_columns");

function givings_edit_columns($columns){
  $columns = array(
    "cb" => "<input type=\"checkbox\" />",
    "title" => "Strategic Giving Item",
    "amount" => "Amount",
    "monthly" => "Monthly",
    "special" => "Special Item",
    "giving_image" => "Image"
    );

  return $columns;
}

function givings_custom_columns($column){
  global $post;

  switch ($column) {
    case "amount":
    $custom = get_post_custom();
    echo $custom["amount"][0];
    break;

    case "monthly":
    $custom = get_post_custom();
    $is_monthly = $custom["monthly"][0];
    if ($is_monthly == "on") {
      echo "Yes";
    };
    break;

    case "special":
    $custom = get_post_custom();
    $is_special = $custom["special"][0];
    if ($is_special == "on") {
      echo "Yes";
    };
    break;

    case "giving_image":
      echo get_the_post_thumbnail(NULL, 'staff_admin');
    break;
  }
}

function giving_amount(){
  $custom = get_post_custom();
  return $custom["amount"][0];
}

function giving_monthly(){
  $custom = get_post_custom();
  return $custom["monthly"][0];
}

function giving_special(){
  $custom = get_post_custom();
  return $custom["special"][0];
}

function giving_user_entered_value(){
  $custom = get_post_custom();
  return $custom["user_entered_value"][0];
}

function giving_item_name(){
  $custom = get_post_custom();
  return $custom["item_name"][0];
}

Update: So I did more research and figured it out. Autosave (aka Revisions) - Post metadata deletes itself.

Is it possible to turn off autosave for just a single post type and not globally?

share|improve this question
2  
did you edit the code you posted above? Because you don't have revisions in the supports array, so Autosaves should be disabled for your "Givings" post type –  onetrickpony Dec 18 '10 at 13:58
add comment

3 Answers

That's an easy one :)

add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts' );
function my_admin_enqueue_scripts() {
    if ( 'your_post_type' == get_post_type() )
        wp_dequeue_script( 'autosave' );
}
share|improve this answer
add comment

Apparently de-registering the autosave javascript will essentially stop the autosave routine running. It won't necessarily disable the ability for autosaves to happen on that post type, but it will stop the native autosave script running.

It's not a perfect solution but it should have the desired effect.

function wpse5584_kill_autosave_on_postype( $src, $handle ) {
    global $typenow;
    if( 'autosave' != $handle || $typenow != 'your-post-type-here' )
        return $src;
    return '';
}
add_filter( 'script_loader_src', 'wpse5584_kill_autosave_on_postype', 10, 2 );

Hope that helps...

EDIT: With the above code, when on the post creation screen for that type you should see the following in the source of the page..

<script type='text/javascript'>
/* <![CDATA[ */
var autosaveL10n = {
    autosaveInterval: "60",
    previewPageText: "Preview this Page",
    previewPostText: "Preview this Post",
    requestFile: "http://yoursite/wp-admin/admin-ajax.php",
    savingText: "Saving Draft&#8230;",
    saveAlert: "The changes you made will be lost if you navigate away from this page."
};
try{convertEntities(autosaveL10n);}catch(e){};
/* ]]> */
</script>
<script type='text/javascript' src=''></script>

The variables aren't what we're looking at here, it's the script at the bottom, notice the src now points nowhere (this was originally pointing at the autosave.js file).

Do you see something similar to the above, or is the src still getting written with the path to the autosave.js file?

EDIT2: This is what i see with concatenate scripts off.

<script type='text/javascript' src='http://example/wp-admin/load-scripts.php?c=0&amp;load=hoverIntent,common,jquery-color,schedule,wp-ajax-response,suggest,wp-lists,jquery-ui-core,jquery-ui-sortable,postbox,post,word-count,thickbox,media-upload&amp;ver=e1039729e12ab87705c047de01b94e73'></script>

Notice that the autosave script is being excluded still.. (so thus far i'm unable to reproduce your problem)..

Where are you placing the code i provided?

share|improve this answer
    
Thanks!! Is it possible to put multiple post types in there? Or do I have to do this for each CPT? –  Marc Dec 19 '10 at 0:15
    
Shame. It didn't work for a single cpt. I'm still getting this: cl.ly/3gTR - which is clearing the custom fields. –  Marc Dec 19 '10 at 0:21
    
Check the source of the page and see if the autosave.js file is still there, if it's still there, then the filter isn't working as expected(or i'm missing something).. QUICK EDIT: Seems to work for me just fine, did you remember to change the text in sample code to match your post type? –  t31os Dec 19 '10 at 0:29
    
Yeah I'll try again and look at the source. And what about if I want to do multiple post types? –  Marc Dec 19 '10 at 3:41
1  
You said it's not a perfect solution, but it's not even a good solution :) –  kovshenin Jun 13 '12 at 9:24
show 8 more comments

I just had a problem with this on one of the plugins I maintain, and we decided to just check if we were on our pages (wpsc-product and not on post or page) and then we simply deregistered the autosave script,, so,, out CPT is 'wpsc-product' and our function (removing unrelated code looks something like this:

function admin_include_css_and_js_refac( $pagehook ) {
    global $post_type, $current_screen;
    if($post_type == 'wpsc-product' )
    wp_deregister_script( 'autosave' );         
}
add_action( 'admin_enqueue_scripts', 'admin_include_css_and_js_refac' );
share|improve this answer
    
Please don't deregister core scripts. –  kovshenin Jun 13 '12 at 9:22
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.