admin管理员组文章数量:1379419
Searching with "Next Post ID" is giving me the Previous/Next Post Navigation, but my question is not related to that.
In WordPress Admin panel, when we're attempting to create a new post the post is been automatically saved as 'auto-draft' and the post ID is been placed within the form as a hidden field named post_id
. So when the post is submitted and the $_REQUEST['post_id']
is been isset.
I'm trying to move uploaded attachments with a Custom Post Type to it's designated directory under uploads/
like uploads/my-dir/my-item-{post_id}
. I'm following Rian Rietveld's snippet for the move. And attachments from admin panel is moving perfectly:
/**
* Set the attachment Upload path.
*
* Set a custom upload path for the post attachments for
* easy management.
*
* @param array $upload Default directory.
*
* @author Rian Rietveld
* @link
*
* @return array Defined directory for the App.
*/
public function my_attachment_dir( $upload ) {
if ( ! empty( $_REQUEST['post_id'] ) ) {
$id = $_REQUEST['post_id'];
// Check whether the posttype is ours.
if ( 'my-cpt' === get_post_type( $id ) ) {
$cpt_directory = "my-item-{$id}";
$upload['subdir'] = "/my-dir/{$cpt_directory}";
}
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
}
return $upload;
}
add_filter( 'upload_dir', 'my_attachment_dir' );
But the issue is, I can't pass a hidden field with post ID from the front end, so the $_REQUEST['post_id']
is getting unset, and the filter is not working. The attachments are uploading to its typical year-month directory.
The question I have, could be answered by any of the following:
- How can I get the upcoming post ID in front end? or,
- How to add filter to
upload_dir
with a makeshift$post_id
instead of passing through$_REQUEST
?
本文标签: uploadsHow to get the upcoming post ID from front end
版权声明:本文标题:uploads - How to get the upcoming post ID from front end? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744484353a2608358.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论