WordPress – Copy image title to ACF custom field on image upload

Here’s one little snippet that took a while to work out so I thought I’d post it here in case its useful for others.

I had a custom field using ACF (Advanced custom fields plugin) that added an extra field to uploaded images that I wanted to use to sort them in a gallery. Adding the extra field via ACF was very easy but I wanted to copy the title of the image and use that in my custom field.

There are many different hooks for manipulating uploaded media such as ‘edit_attachment’ and ‘attachment_fields_to_save’ but now of them seemed to work when the image was uploaded, only when some of the other attachment data was edited and the ajax save was fired. What I wanted was the image to upload and automatically populate the custom field. After a lot of searching around I came across the ‘add_attachment’ hook which seemed to do the trick with my test code.

The next problem was automating the copy of the title text over to my custom field. The WordPress function ‘update_post_meta’ seemed like it might be a good option but it just would not work. It turned out that ACF deals with its custom fields in a slightly different way and in fact has its own update function called ‘update_field’. This did the trick and the following snippet of code now copies the title to my custom field as an image is uploaded.

function copy_title_to_custom_field( $post_ID ) {

$attachment_title = get_the_title( $post_ID );
update_field( 'image_order_title', $attachment_title, $post_ID );

add_action( 'add_attachment', 'copy_title_to_custom_field' );

Now the clever bit I need this for is to use a custom field to order a set of gallery images by numeric values. I have a photography website were I want the user to be able to name a set of photos, upload them in one batch and have them all display in the correct order. WordPress doesn’t seem to deal with this very well so it’s taken a bit of hammering into shape to get it to work properly and the only way to successfully order numeric file names is to use ‘orderby=meta_value_num’ BUT you can only seem to do this with custom fields! What a headache!

Paul Ledbrook

If you have a new project or existing website that needs improvement, email us or call 0845 3631162 , we’d love to hear from you.

What’s new?

10 Things to Consider When Designing a High-Converting web app UI design

Your web app’s user interface (UI) is the first thing that users will see and interact with, so it’s important to make a good impression. A well-designed web app UI design can help you attract and retain users, and even increase conversions. Here are 10 things to consider when designing a high-converting web app UI...

How to Use Web Design to Improve User Experience

Introduction Your website is your online storefront. It’s the first impression that potential customers will have of your business, so it’s important to make sure it’s designed in a way that is user-friendly and easy to navigate. Good web design can help you improve your user experience (UX) in a number of ways. It can...

We’ve been featured on The Best Music Website Designs by DesignRush

A huge thank you to the guys at DesignRush for featuring the website design work we did with Go To Hear on their ‘Best Music Website Designs’ feature. Head over to the Best Design Trends page now to see our designs along with all the other fantastic music website designs. If you want to read...

SEO for web design – How to Optimise Your Website for Search Engines

Search engine optimisation SEO for web design is crucial for any business with an online presence. Optimising your website for search engines can increase its visibility, drive more traffic, and boost conversions. Follow these tips to make your website more discoverable and rank higher in search results: Choose the Right Keywords for SEO for web...

Website Design – 10 Things to Consider When Planning Website

What are the most important things to consider when designing a website? In today’s digital age, having a website is no longer optional; it’s a necessity. Research reveals that businesses without a website risk losing 70-80% of potential customers. Therefore, creating a well-designed and engaging website design is the crucial first step. But how can...

The Power of Professional WordPress Website Design Services for Business Success

In today’s digital age, a well-designed website is crucial for establishing a strong online presence. This is where our professional WordPress website design services and e-commerce website design shine. With our expertise in web design and development, our WordPress website design services play a pivotal role in helping your business create captivating online experiences that...

How-to create an effective E-commerce website design for online stores

Introduction: Understanding the Significance of E-commerce Website Design In today’s digital landscape, the design of an e-commerce website plays a pivotal role in shaping the success of online businesses. A well-crafted website design can significantly impact user experience, conversion rates, and overall brand image. By implementing effective design strategies, businesses can create visually appealing and...

Five of the most common UX design errors that can drive customers away

Are you unintentionally causing users to leave your website? Here are five of the most common UX design errors that can drive customers away. UX design is accountable for providing a satisfying experience to the user. To build an effective UX, it must be attractive, actionable and guide the user on a frictionless journey, from...