Can't display errors in attachment_fields_to_save
I am writing a plugin for adding a custom field to the image attachment dialogue box, the plugin works for storing valid input data. I have a problem for showing the error message "This is not a valid URL".
I think that I followed the documentation correctly but maybe there is something that I still missing.
class my_plugin {
public function __construct( ) {
$this-url_pattern = "/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?" .
"([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|" .
"%[a-fA-f\d]{2,2})*)*(\?(?([-+_~.\\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]" .
"|%[a-fA-f\d]{2,2})*)?$/";
add_filter("attachment_fields_to_edit", array($this, "videourl_image_attachment_fields_to_edit"), 10, 4);
add_filter("attachment_fields_to_save", array($this, "videourl_image_attachment_fields_to_save"), 10, 5);
}
public function videourl_image_attachment_fields_to_edit($form_fields, $post) {
$form_fields["videourl"]["label"] = __("Video URL");
$form_fields["videourl"]["helps"] = "Set a video URL for this image";
$form_fields["videourl"]["input"] = "text";
$form_fields["videourl"]["value"] = get_post_meta($post-ID, "_videourl", true);
return $form_fields;
}
function videourl_image_attachment_fields_to_save($post, $attachment) {
if( isset($attachment['videourl']) ){
if( mb_strlen ( trim($attachment['videourl']) ) 0 preg_match($this-url_pattern, trim($attachment['videourl']))){
update_post_meta($post['ID'], '_videourl', $attachment['videourl']);
}elseif(mb_strlen ( trim($attachment['videourl']) ) == 0){
delete_post_meta($post['ID'], '_videourl');
}else{
$post['errors']['videourl']['errors'][] = __('This is not a valid URL.');
}
}
return $post;
}
}
Topic attachment-fields-to-edit custom-field Wordpress
Category Web