Search code examples
wordpresshyperlinkbookmarks

Meta box in links / Wordpress


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.


Solution

  • 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