Posible desactivar el guardado automático para un único tipo de publicación personalizada

10

Por lo tanto, tengo un problema con mis campos personalizados en mi tipo de publicación personalizada. Por el motivo que sea, los campos se guardan y luego se borran de forma aleatoria ... Estoy seguro de que no es aleatorio, pero no estoy seguro de qué está provocando que esto suceda. Aquí está el código para mi tipo de publicación personalizada:

    // 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];
}

Actualización: Así que investigué más y lo resolví. Guardado automático (también conocido como Revisiones) - Los metadatos de la publicación se eliminan a sí mismos

¿Es posible desactivar el guardado automático para un solo tipo de publicación y no globalmente?

    
pregunta Marc 18.12.2010 - 14:40

3 respuestas

14

Eso es fácil :)

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' );
}
    
respondido por el kovshenin 13.06.2012 - 11:22
4

Aparentemente, al desregistrar el javascript de guardado automático esencialmente se detendrá la ejecución de la rutina de guardado automático. No necesariamente deshabilitará la posibilidad de que se realicen guardados automáticos en ese tipo de publicación, pero detendrá la ejecución del script de guardado automático nativo.

No es una solución perfecta, pero debería tener el efecto deseado.

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 );

Espero que ayude ...

EDITAR: Con el código anterior, cuando estés en la pantalla de creación de publicaciones para ese tipo, deberías ver lo siguiente en la fuente de la página ...

<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>

Las variables no son lo que estamos viendo aquí, es el script en la parte inferior, note que src ahora no apunta a ninguna parte (esto originalmente apuntaba al archivo autosave.js).

¿Ves algo similar al anterior, o aún se está escribiendo el src con la ruta al archivo autosave.js?

EDIT2: Esto es lo que veo con los scripts de concatenación desactivados.

<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>

Observe que el script de guardado automático aún se está excluyendo ... (hasta ahora no puedo reproducir su problema) ..

¿Dónde está colocando el código que proporcioné?

    
respondido por el t31os 18.12.2010 - 16:08
1

Acabo de tener un problema con esto en uno de los complementos que mantengo, y decidimos simplemente verificar si estábamos en nuestras páginas (wpsc-product y no en la publicación o la página) y luego simplemente desregistramos el script de guardado automático, por lo tanto, nuestro CPT es 'wpsc-product' y nuestra función El código se ve algo como esto:

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' );
    
respondido por el user3130 14.02.2011 - 04:40

Lea otras preguntas en las etiquetas