最合适的回答,由SO网友:Michael N. 整理而成
下面是我如何实现一个简单的媒体工作流的。安全检查和消毒取决于每个人。
基于Wordpress版本:4.9.8
创建附件帖子|处理介质
if (!empty($_FILES) && isset($_POST[\'postid\'])) {
media_handle_upload("attachments", $_POST[\'postid\']);
}
使用时,附件不可能处于挂起状态
media_handle_upload
. 在这种情况下,需要使用过滤器。使用前应添加此筛选器
media_handle_upload
.
add_filter(\'wp_insert_attachment_data\', \'SetAttachmentStatusPending\', \'99\');
function SetAttachmentStatusPending($data) {
if ($data[\'post_type\'] == \'attachment\') {
$data[\'post_status\'] = \'pending\';
}
return $data;
}
现在,附件帖子添加了挂起的post\\u状态。
在媒体库中显示挂起的帖子
Worpress媒体库仅显示具有private或inherit post\\u状态的帖子。要显示具有挂起状态的帖子,我们可以挂接
add_action(\'pre_get_posts\', array($this, \'QueryAddPendingMedia\'));
function QueryAddPendingMedia($query)
{
// Change query only for admin media page
if (!is_admin() || get_current_screen()->base !== \'upload\') {
return;
}
$arr = explode(\',\', $query->query["post_status"]);
$arr[] = \'pending\';
$query->set(\'post_status\', implode(\',\', $arr));
}
操作和批量操作要完成工作流,我们需要发布挂起的媒体。为此,我们可以向媒体库添加(批量)操作。
添加批量操作
add_filter(\'bulk_actions-upload\', \'BulkActionPublish\');
function BulkActionPublish($bulk_actions)
{
$bulk_actions[\'publish\'] = __(\'Publish\');
return $bulk_actions;
}
添加行操作要添加到行操作的链接,此代码很有用
add_filter(\'media_row_actions\', \'AddMediaPublishLink\', 10, 3);
function AddMediaPublishLink(array $actions, WP_Post $post, bool $detached)
{
if ($post->post_status === \'pending\') {
$link = wp_nonce_url(add_query_arg(array(\'act\' => \'publish\', \'itm\' => $post->ID), \'upload.php\'), \'publish_media_nonce\');
$actions[\'publish\'] = sprintf(\'<a href="%s">%s</a>\', $link, __("Publish"));
}
return $actions;
}
处理操作
add_action(\'load-upload.php\', \'RowActionPublishHandle\');
add_filter(\'handle_bulk_actions-upload\', \'BulkActionPublishHandler\', 10, 3);
function BulkActionPublishHandler($redirect_to, $doaction, $post_ids)
{
if ($doaction !== \'publish\') {
return $redirect_to;
}
foreach ($post_ids as $post_id) {
wp_update_post(array(
\'ID\' => $post_id,
\'post_status\' => \'publish\'
));
}
return $redirect_to;
}
function RowActionPublishHandle()
{
// Handle publishing only for admin media page
if (!is_admin() || get_current_screen()->base !== \'upload\') {
return;
}
if (isset($_GET[\'_wpnonce\']) && wp_verify_nonce($_GET[\'_wpnonce\'], \'publish_media_nonce\')) {
if (isset($_GET[\'act\']) && $_GET[\'act\'] === \'publish\') {
wp_update_post(array(
\'ID\' => $_GET[\'itm\'],
\'post_status\' => \'publish\'
));
}
}
}