Home Forums General Issues Generate unique slug from text field


Generate unique slug from text field

  • I have an idea for all of my ACF Blocks to have a cloned field called “Anchor” that lets the editor add a label for each block that then would be used to generate a unique slug that can be used for anchor linking.

    So there would be a text field called anchor_label where the editor could write “Wonderful section”. I’ve then set up a second text field called anchor_id and made that read-only by doing this:

    add_filter('acf/prepare_field/name=anchor_id', function($field) {
    	$field['readonly'] = true;
    	return $field;

    I’ve used name= instead of key= as the key would be different for each block as it’s a clone within the block, so to simplify this I targeted the field by name.

    What I now want to do is that if the editor enters for example “Wonderful section” in the anchor_label field, the anchor_id field should be populated automatically in real-time (using Ajax I assume) with a slug. So the anchor_id in this example would be “wonderful-section”.

    It would be very nice if this function that creates and populates the ID could check for fields of the same name on the current page being edited and look for any identical input. So if there’s already a anchor_id field with “wonderful-section” entered, the new field in this example would be “wonderful-section-2”.

    Is this possible and how would I do it?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.