开发者

Save data of custom metabox in Wordpress

I added a meta-box to the edit link page & I can't save the data whatever I put in the field. How can I only update the meta-box without saving the data in the database? Here is my code:

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

How can I sav开发者_运维技巧e or update the data of the input field of metabox? Only the data should be updated in the metabox. It should not save in database by any type of custom field.


I think it actually would be a good idea to save as a custom field, only one that doesn't show up in the custom field box. You can accomplish the latter by adding a "_" at the beginning of the custom field's name (i.e. "_my_custom_field" instead of "my_custom_field".

Here's a sample function to save your meta box data. I changed the names to match the code you have above.

:

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

And that should be it. I used this page as a reference: http://codex.wordpress.org/Function_Reference/add_meta_box


Hook the action save_post - it receives saved post ID and allows you to update the post the way you need when submitting post editor page. Don't forget that this action will be called for EVERY post saved - you need to only handle posts having your custom meta box.


you must disable this code

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

what it does is that it blocks your code below cause it detects that your doing some auto save on it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜