attachment_fields_to_save first parameter is null
I have the following code in a self-written plugin.
In the function saveHomepageGalleryCheckbox
the first param ($post
) is always null. Did I something wrong? The second parameter ($attachment
) has the correct value.
- Wordpress Version: 5.3.2
- PHP Version: 7.3.15
Code:
class MediaView
{
private $key = 'include_in_homepage_gallery';
public function __construct()
{
add_filter('attachment_fields_to_edit', [$this, 'renderHomepageGalleryCheckbox'], 10, 2);
add_filter('attachment_fields_to_save', [$this, 'saveHomepageGalleryCheckbox'], 10, 2);
}
public function renderHomepageGalleryCheckbox(array $fields, WP_Post $post)
{
$meta = get_post_meta($post-ID, $this-key, true);
$checked = (boolval($meta) === true) ? 'checked="checked"' : '';
$id = "attachments[{$post-ID}][{$this-key}]";
$fields[$this-key] = [
'label' = 'Homepage Gallery',
'input' = 'html',
'html' = "input type=\"checkbox\" name=\"$id\" id=\"$id\" value=\"1\" {$checked} /"
];
return $fields;
}
public function saveHomepageGalleryCheckbox($post, array $attachment) // Here is $post === null
{
if (isset($attachment[$this-key])) update_post_meta($post['ID'], $this-key, 1);
else update_post_meta($post['ID'], $this-key, 0);
return $post;
}
}
Topic attachment-fields-to-edit post-thumbnails attachments images Wordpress
Category Web