[Plugin: Custom Upload Dir] Hack/desired feature: %post_parent%
-
I’ve been using CUD for a site where it would be really useful to have images sorted by page parent. So I made it so that CUD could accommodate this very easily, including with posts that have multiple parents.
It’s pretty easy. In custom_upload_dir.php, after “$want_name = (strpos($customdir, ‘%post_name%’) !== false);”, add the following code:
$want_parent = (strpos($customdir,'%post_parent%') !== false); if($want_parent) { if($post->post_parent) { $post_parent_slug = basename(get_permalink($post->post_parent))."/"; } elseif (get_post_type_object($post->post_type)->rewrite["slug"]) { $post_parent_slug = (get_post_type_object($post->post_type)->rewrite["slug"])."/"; } else { $post_parent_slug = ""; } }
And in the list of “$custom_dir =” calls, add these two:
$customdir = ($want_parent) ? str_replace('%post_parent%/', $post_parent_slug, $customdir) : $customdir; $customdir = ($want_parent) ? str_replace('%post_parent%', substr($post_parent_slug,0,strlen($post_parent_slug)-1), $customdir) : $customdir;
The funny multiple calls and so on because it tries to auto-detect whether backslashes already exist or not.
If the post in question is a custom post type, it uses the custom post type slug name instead of the parent name (because there will probably not be a parent).
Hope that helps someone… I release the above lines of code into the public domain, of course.
- The topic ‘[Plugin: Custom Upload Dir] Hack/desired feature: %post_parent%’ is closed to new replies.