• Resolved Eugene

    (@euguk007)


    Hiya.

    Trying to keep the images of listings organised, I wonder if it would be possible to dynamically change the name of images according to a pattern like:
    postCategory-postID-1.jpg
    postCategory-postID-2.jpg
    postCategory-postID-3.jpg
    if 3 images uploaded.

    If this is too hard to achieve, would it be possible to save them to appropriate folders named as categories within the uploads folder, and then name them just with postID.jpg, postID-1.jpg, postID-2.jpg etcetera…

    Would you have a tip for a snippet, please?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Stiofan

    (@stiofansisland)

    Anything is possible, i just don’t see the reasoning?

    TO change the name the filter you want is https://developer.ww.wp.xz.cn/reference/hooks/action_prefilter/

    With our action name it would look something like:

    add_filter('geodir_post_attachment_upload_prefilter', '_my_custom_upload_filter' );
    
    function _my_custom_upload_filter( $file ){
        $file['name'] = 'wordpress-is-awesome-' . $file['name'];
        return $file;
    }

    You could also use this filter to change the folder name https://developer.ww.wp.xz.cn/reference/hooks/upload_dir/

    Its not something i would recommend.

    Thanks,

    Stiofan

    Thread Starter Eugene

    (@euguk007)

    I appreciate your answer, Stiofan – Thank you!

    Thread Starter Eugene

    (@euguk007)

    Dear Stiofan,

    Is there a comprehensive list of GeoDirectory’s actions available?
    I found one for v.1: https://wpgeodirectory.com/codex/codex_project/geodirectory_actions/

    Is there an updated list?

    Many thanks!
    Eugene

    Thread Starter Eugene

    (@euguk007)

    Dear Stiofan,

    In your solution for the file name:

    add_filter('geodir_post_attachment_upload_prefilter', '_my_custom_upload_filter' );
    
    function _my_custom_upload_filter( $file ){
        $file['name'] = 'wordpress-is-awesome-' . $file['name'];
        return $file;
    }

    The $file variable doesn’t seem to hold much information, and I would like to replace the ‘wordpress-is-awesome-‘ with the parent_post slug.

    How could I get at least the current $post_id of the listing that is being edited?

    Many thanks in advance.

    Eugene

    Thread Starter Eugene

    (@euguk007)

    This is as far as I got to where I wanted. I can’t find a way to any meta keys of the post currently being created/edited by the current given user. As you can see, I even had to squeeze out the $file’s extension.

    add_filter('geodir_post_attachment_upload_prefilter', '_my_custom_upload_filter' );
    function _my_custom_upload_filter( $file ) {
    	$strArray = explode('.',$file['name']);
    	$ext = end($strArray);
        $file['name'] = 'parent-post-slug' . '.' . $ext;
        return $file;
    }

    Help would be appreciated. Thanks!

    Thread Starter Eugene

    (@euguk007)

    Hi again. Not sure whether you read this once it has been marked as resolved, but I have now resolved it completely.

    add_filter('geodir_post_attachment_upload_prefilter', '_my_custom_upload_filter' );
    function _my_custom_upload_filter( $file ) {
    
    	$fileArray = explode('.',$file['name']);
    	$ext = end($fileArray);
    
    	$this_post = get_post( intval( $_REQUEST['post_id'] ) );
    	if ($this_post->post_parent != 0) {
    		$parent_post = get_post( $this_post->post_parent );
    		$file['name'] = $parent_post->post_name . '.' . $ext;
    	} else {
    		$file['name'] = $this_post->post_name . '.' . $ext;
    	}
        return $file;
    }
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Image Upload Name’ is closed to new replies.