Change post_content and post_title labels on front end ACF form (acf_form)

The acf_form() functionality is a pretty amazing tool. It allows you to quickly and easily create front end forms for users to do a wide variety of things from submitting posts and custom post types to editing current posts. In this tutorial we’ll go over how to modify the lables for the post_content and post_title fields.

For the most part, there will be no need to edit these labels, but in a recent project I was using a variety of forms throughout the site to create and edit custom post types. For this specific project there were forms to submit and edit two different custom post types; vendors and products.

Rendering the ACF Front End Form

Here is the basic code that renders the form:

<?php
 /*
  * Render acf_form for editing vendor details
  *
  * @uses https://www.advancedcustomfields.com/resources/acf_form/
  *
  */
acf_form( array( // front end edit form via ACF
     'post_title'     => true, // default is false
     'post_content'   => true, // default is false
     'submit_value'   => __( "Update Vendor", 'acf' ),
     'update_message' => __( "Vendor Updated", 'acf' ),
     'uploader'       => 'basic', // disable access to WordPress library
     'kses'           => false // disable to allow for embed uploads
));

I won’t go in to all the details on what this does, you can check that out here.

Modifying the post_content and post_title Labels of the ACF Form

The function below, placed in your functions.php (or other function file) will help you modify the post_title and post_content labels of the form:

<?php
// Modify ACF Form Label for Post Title Field
function wd_post_title_acf_name( $field ) {
     if( is_singular( 'vendors' ) ) { // if on the vendor page
          $field['label'] = 'Vendor Name';
     } else {
          $field['label'] = 'Product Name';
     }
     return $field;
}
add_filter('acf/load_field/name=_post_title', 'wd_post_title_acf_name');

// Modify ACF Form Label for Post Content Field
function wd_post_content_acf_name( $field ) {
     if( is_singular( 'vendors' ) ) { // if on the vendor page
          $field['label'] = 'Vendor Description';
     } else {
          $field['label'] = 'Product Description';
     }
     return $field;
}
add_filter('acf/load_field/name=_post_content', 'wd_post_content_acf_name');

You’ll see both functions run on the acf/load_field filter. I have two functions, one for the post_content and one for the post_title. This is controlled with the name= that follows the acf/load_field/.

One thing to note is that this will affect ALL acf_form() instances on the site, so for more granular control of which labels are changed we need to do some conditional checks. In my case there were a variety of product edit/submit forms, but only one vendor related form. Since that form resides on the single vendor page we just check for that page using is_singular( 'vendors' ) to set the label for that page, then set the label for all other forms in the else statement.

If there were additional variations needed, we would do additional conditional checks to ensure the labels were correct throughout the site.

And that’s all there is to it. A few simple functions to help you modify the post_content and post_title labels of front end ACF forms!

Comments

  1. Feel says:

    Hi!

    Thanks.

    For tips you can use $field[‘instructions’] = “some instructions”.

    But i dont know how set chars limit to text fields

Leave a Reply

Your email address will not be published. Required fields are marked *