Home Forums Backend Issues (wp-admin) New Custom Field Type – admin conditional enqueue scripts


New Custom Field Type – admin conditional enqueue scripts

  • I am creating a new custom field for ACF which will be used in one layout option of a Flexible Content Field.

    The Flexible Content Field exists on all pages, but the particular layout that uses the new custom field will be used very rarely and on very few pages.

    The new field relies on enqueueing a lot of JavaScript in the wp-admin to render the field editing interface, so it would be best to only load the JS when the field is used.

    I am using the input_admin_enqueue_scripts() function inside add_action('acf/include_field_types') when creating my new field as is the official example, but that loads the custom field’s scripts on all pages that have ACF fields, even pages that don’t use the new custom field.

    Trying to enqueue the javascript by using add_action('acf/render_field/type=new_custom_field') or add_filter('acf/load_field/type=new_custom_field') also enqueues the scripts even when the field isn’t selected to be used on the particular page.

    Is there a way at all to only enqueue the JavaScript only when:
    1. A particular field type is added to a page/post in wp-admin?
    2. The particular field type has already been added to a page/post in wp-admin?

  • You could try enqueuing the script to load in the footer in the render_field() method of your field type.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.