WordPress.org

Ready to get started?Download WordPress

Forums

Custom Upload Dir
[resolved] Hack/desired feature: %post_parent% (3 posts)

  1. frizzle
    Member
    Posted 2 years ago #

    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.

    http://wordpress.org/extend/plugins/custom-upload-dir/

  2. ulfben
    Member
    Plugin Author

    Posted 2 years ago #

    Thanks. I've rewritten much of the plugin recently and am currently testing the changes before release. I'll take a closer look on your addition next week and probably release very shortly.

  3. ulfben
    Member
    Plugin Author

    Posted 2 years ago #

    Sorry. Forgot about your thread. parent_slug is in the development version. Please test it for me.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic