remove domain suffix from image filename

[ad_1]

Hi @jacklamond,

You can try to use the following filter:

/**
 * Allow filtering the data, when offloading images to Cloudflare.
 *
 * @sice 1.2.0
 *
 * @param array      $data  Data.
 * @param int|string $id    Image ID.
 */
$data = apply_filters( 'cf_images_upload_data', $data, $id );

The $data['id'] will hold the custom image ID. You can modify it to your liking.

Best regards,
Anton

Hi Anton,

Thanks very much for your quick response, however I can’t seem to get this to work. I’m placing the filter as a new snippet in ‘Code Snippets’ and have tried a couple of variations but I can’t get it to alter the filename at all. Example attached.

Oh, sorry, that was just the hook. You’ll need to code the functionality. For example, if you want to replace examplewebsite.local with examplewebsite, then it will look something like this:

add_filter( 'cf_images_upload_data', function( $data ) {
	$data['id'] = str_replace( 'examplewebsite.local', 'examplewebsite', $data['id'] );
	return $data;
}, 15 );

Best regards,
Anton

Hey @vanyukov

No no, it’s my bad! I’m not too proficient in php unfortunately so can get easily confused. I’ve messed around with the code provided, but unfortunately continue to be faced with the following error when trying to offload the image:

{ “result”: null, “success”: false, “errors”: [ { “code”: 5411, “message”: “The Custom ID is invalid. Custom IDs can include 1024 characters or less, any number of subpaths, and support the UTF-8 encoding standard for characters. Enter a new Custom ID and try again: Must not be an empty string” } ], “messages”: [] }

Got it! Needed to activate the ‘keep media file library structure’ option.

Thanks so much @vanyukov, you’re awesome.

Currently trying to take it a step further buy automatically grabbing the site url without the protocol e.g ‘example.local’ and returning the site name ‘example’ so this can be used on multiple websites without having to manually edit the code on each

I haven’t tested this, but I think it should work as expected, assuming that sites have a *.local domain:

add_filter( 'cf_images_upload_data', function( $data ) {
	$site_url   = wp_parse_url( get_option( 'siteurl' ), PHP_URL_HOST );
	$site_name  = str_replace( '.local', '', $site_url );
	$data['id'] = str_replace( $site_url, $site_name, $data['id'] );
	return $data;
}, 15 );

Best regard,
Anton

@vanyukov Wow! that works perfectly.

Thanks so much again, Anton. Is there somewhere I can tip you a coffee?

Jack

@jacklamond, cool! Happy that it worked out for you. There’s a donate button on the plugin page, but there’s really no need.

I’ll mark this as resolved. Feel free to open a new thread if you have any other questions.

Best regards,
Anton

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer