2011-09-26 70 views
0

我在编辑链接页面添加了一个元框&无论我在该字段中放置了什么,我都无法保存数据。我如何只更新元框而不保存数据库中的数据?这是我的代码:在Wordpress中保存自定义metabox的数据

// backwards compatible 
add_action('admin_init', 'blc_add_custom_link_box', 1); 

/* Do something with the data entered */ 
add_action('save_link', 'blc_save_linkdata'); 

/* Adds a box to the main column on the Post and Page edit screens */ 
function blc_add_custom_link_box() { 
    add_meta_box( 
     'backlinkdiv', 
     'Backlink URL', 
     'blc_backlink_url_input', 
     'link', 
     'normal', 
     'high' 
    ); 
} 

/* Prints the box content */ 
function blc_backlink_url_input($post) { 
    // Use nonce for verification 
    wp_nonce_field(plugin_basename(__FILE__), 'blc_noncename'); 

    // The actual fields for data entry 
    echo '<input type="text" id="backlink-url" name="backlink_url" value="put your backlink here" size="60" />'; 

    #echo "<p> _e('Example: <code>http://Example.org/Linkpage</code> &#8212; don&#8217;t forget the <code>http://</code>')</p>"; 
} 

如何保存或更新metabox输入字段的数据?只有数据应在metabox中更新。它不应该通过任何类型的自定义字段保存在数据库中。

回答

0

挂钩操作save_post - 它接收已保存的帖子ID,并允许您在提交帖​​子编辑器页面时以您需要的方式更新帖子。不要忘记,这个动作将被称为每个帖子保存 - 你只需要处理有你的自定义元框的帖子。

4

我认为这实际上是一个好主意,保存为自定义字段,只有一个不会显示在自定义字段框中。您可以通过在自定义字段名称的开头添加一个“_”(即“_my_custom_field”而不是“my_custom_field”)来完成后者。

下面是一个用于保存元框数据的示例函数。匹配你有上面的代码

:。

<?php 

    function blc_save_postdata($post_id){ 

     // Verify this came from the our screen and with proper authorization, 
     // because save_post can be triggered at other times 
     if (!wp_verify_nonce($_POST['blc_noncename'], plugin_basename(__FILE__))) { 
     return $post_id; 
     } 

     // Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want 
     // to do anything 
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 


     // Check permissions to edit pages and/or posts 
     if ('page' == $_POST['post_type'] || 'post' == $_POST['post_type']) { 
     if (!current_user_can('edit_page', $post_id) || !current_user_can('edit_post', $post_id)) 
      return $post_id; 
     } 

     // OK, we're authenticated: we need to find and save the data 
     $blc = $_POST['backlink_url']; 

     // save data in INVISIBLE custom field (note the "_" prefixing the custom fields' name 
     update_post_meta($post_id, '_backlink_url', $blc); 

    } 

    //On post save, save plugin's data 
    add_action('save_post', array($this, 'blc_save_postdata')); 
      ?> 

这应该是它我用这个页面作为参考:http://codex.wordpress.org/Function_Reference/add_meta_box

0

您必须禁用该代码

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

它所做的是阻止下面的代码,因为它会检测到您正在做一些自动保存。

相关问题