custom meta box data not saved
After two day of reasearch and questions without answer, today someone has suggested me a way to add a custom metabox field to the wordpress images. My scope is to add an url when the image are uploaded or modified and for now I’m only able to display the input field where user can add the url, but the input will not be saved and I’m unable to get it back on the front-end.
I want to use this custom metabox function with the get_post_gallery()
that will output the urls of the images I’ve added inside a gallery, but I don’t know how to combine this, and I need to fix the saving of the inserted url.
Can anyone help me or guide trought the implementation?
<?php
function add_image_link()
{
add_meta_box(
'image_link_box',
'Link image to url',
'image_link_field',
'attachment'
);
}
add_action( 'add_meta_boxes_attachment', 'add_image_link' );
function image_link_field( $post )
{
?>
<label for="image-link">Link URL</label>
<input type="text" name="image_link" id="image-link" class="postbox">
<?php
}
function save_image_link( $post_id )
{
if( array_key_exists( 'image_link', $_POST ) ){
update_post_meta(
$post_id,
'image_link_url',
$_POST['image_link']
);
}
}
add_action( 'save_post_attachment', 'save_image_link' );
?>
Leave an answer