0

Is there a way to save meta box value when it's added to links? Here is my code:

<?php
/*
Plugin Name: Link Date
Plugin URI: http://
Description: Adds links date field.
Version: None
Author: Auth
Author URI: http://
*/

add_action( 'add_meta_boxes', 'link_date_add' );
function link_date_add()
{
    add_meta_box( 'link-date-meta-box', 'Link Date', 'link_date', 'link', 'normal', 'high'     );
    }


function link_date( $post )
{
    $values = get_post_custom( $post->ID );
    $date = isset( $values['link_date_text'] ) ? esc_attr( $values['link_date_text'][0] ) : '';
    wp_nonce_field( 'my_link_date_nonce', 'link_date_nonce' );
    var_dump($post);
    ?>
<p>
    <label for="link_date_text">Link Date</label>
    <input type="text" name="link_date_text" id="link_date_text" value="<?php echo $date; ?>" />
</p>

<?php
}


add_action( 'save_post', 'link_date_save' );
function link_date_save( $post_id )
{
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    if( !isset( $_POST['link_date_nonce'] ) || !wp_verify_nonce( $_POST['link_date_nonce'], 'my_link_date_nonce' ) ) return;

    if( !current_user_can( 'edit_post' ) ) return;

    $allowed = array(
        'a' => array(
        'href' => array() // and those anchords can only have href attribute
    )
);


if( isset( $_POST['link_date_text'] ) )
    update_post_meta( $post_id, 'link_date_text', wp_kses( $_POST['link_date_text'], $allowed ) );


}

?>

This code works, when post type changed to 'post', but for some reason it doesn't save data as 'link' type of post.

I saved some data from this field when post type was set as 'post', then updated the post type in code to 'link', and additionally updated the database (wp_postmeta) replacing the id with the id of link. Then this data was displayed in meta box in links, but still, bot able to update value of this metabox.

El Kopyto
  • 1,157
  • 3
  • 18
  • 33

2 Answers2

1

The answer is to use add_action('edit_link','save_data') and add_option('name_of_option') instead of add_post_meta view full results here MetaBox Links

David Chase
  • 2,055
  • 1
  • 18
  • 25
0

After some experiments, I figured out how to save data from custom metabox in link manager into db as post meta key/value (wp_postmeta). If someone needs, here is a working example:

action( 'add_meta_boxes', 'add_link_date' );
function add_link_date()
{
    add_meta_box( 'link-date-meta-box', 'Link Date', 'link_date', 'link', 'normal', 'high' );
}

function link_date( $link )
{
    $values = get_post_custom( $link->link_id );
    $date = isset( $values['link_date'] ) ? esc_attr( $values['link_date'][0] ) : '';
    wp_nonce_field( plugin_basename( __FILE__ ), 'link_date_nonce' );
?>
<p>
    <label for="link_date_text">Link Date</label>
    <input type="text" name="link_date_text" id="link_date_text" value="<?php echo $date; ?>" />
</p>

<?php
}


add_action( 'edit_link', 'myplugin_save_postdata' );

function myplugin_save_postdata( ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( !isset( $_POST['link_date_nonce'] ) || !wp_verify_nonce( $_POST['link_date_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    $link_id = $_POST['link_id'];
    $linkDate = sanitize_text_field( $_POST['link_date_text'] );

    add_post_meta( $link_id, 'link_date', $linkDate, true ) or
    update_post_meta( $link_id, 'link_date', $linkDate );
}
El Kopyto
  • 1,157
  • 3
  • 18
  • 33