• Resolved wordpressask

    (@wordpressask)


    hi

    how to change post type Post to Product in code?

    function wp_modify_uploaded_file_names($image_name) {
    
        // Get the parent post ID, if there is one
        if( isset($_GET['post_id']) ) {
            $post_id = $_GET['post_id'];
        } elseif( isset($_POST['post_id']) ) {
            $post_id = $_POST['post_id'];
        }
    
        // Only do this if we got the post ID--otherwise they're probably in
        //  the media section rather than uploading an image from a post.
        if(is_numeric($post_id)) {
    
            // Get the post slug
            $post_obj = get_post($post_id);
            $post_slug = $post_obj->post_name;
    
            // If we found a slug
            if($post_slug) {
    
                $random_number = rand(10000,99999);
                $image_name['name'] = $post_slug . '-' . $random_number . '.jpg';
    
            }
    
        }
    
        return $image_name;
    
    }
    add_filter('wp_handle_upload_prefilter', 'wp_modify_uploaded_file_names', 1, 1);
Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi,

    Could you please provide more details here? Do you need this image name change to work for product post type only or you want to change post type in addition to the image name?
    In first case you cann add a conditional like this:
    if ( 'product' === get_post_type( $post_id )
    right after
    if(is_numeric($post_id))
    to select products only.

    Thanks

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi there @wordpressask

    I do have to ask the question of why you would like to change the name?

    Thread Starter wordpressask

    (@wordpressask)

    hi
    yes need support product post type
    I don’t know coding very much

    can you provide full code with you lines?

    may be I edit incorrectly but it gives errors

    • This reply was modified 6 years, 10 months ago by wordpressask.
    Thread Starter wordpressask

    (@wordpressask)

    hi Jose

    can you explain what you mean by change name

    username?

    the full code should look like this then:

    
    function wp_modify_uploaded_file_names($image_name) {
    
        // Get the parent post ID, if there is one
        if( isset($_GET['post_id']) ) {
            $post_id = $_GET['post_id'];
        } elseif( isset($_POST['post_id']) ) {
            $post_id = $_POST['post_id'];
        }
    
        // Only do this if we got the post ID--otherwise they're probably in
        //  the media section rather than uploading an image from a post.
        if(is_numeric($post_id) && 'product' === get_post_type( $post_id ) ) {
    
            // Get the post slug
            $post_obj = get_post($post_id);
            $post_slug = $post_obj->post_name;
    
            // If we found a slug
            if($post_slug) {
    
                $random_number = rand(10000,99999);
                $image_name['name'] = $post_slug . '-' . $random_number . '.jpg';
    
            }
    
        }
    
        return $image_name;
    
    }
    add_filter('wp_handle_upload_prefilter', 'wp_modify_uploaded_file_names', 1, 1);
    

    I checked it on my local – works as expected.

    Thread Starter wordpressask

    (@wordpressask)

    this code rename file name to post title and generate random number

    how to modify this code to just rename to post title but not generate numbers?

    How you would handle several images for one post in this case?

    Thread Starter wordpressask

    (@wordpressask)

    what you mean?

    Thread Starter wordpressask

    (@wordpressask)

    nevermind

    resolved

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘custom post type in code’ is closed to new replies.