Skip to content

Enhancement: Add new acf/fields/icon_picker/{tab_name}/icons filter #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Jun 17, 2025

Work in progress. Testing instructions to follow.

PHP changes correspond to this changeset: https://plugins.trac.wordpress.org/changeset/3289772/advanced-custom-fields/tags/6.4.1/includes/fields/class-acf-field-icon_picker.php

Testing Instructions

Add the following code as a plugin, and enable it:

Code
<?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName -- This file contains procedural code for now.
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- This file contains both procedural and object-oriented code for now.
/**
 * Secure Custom Fields Test Plugin
 *
 * Plugin Name:       Secure Custom Fields Test Plugin
 * Plugin URI:        https://developer.wordpress.org/secure-custom-fields/
 * Version:           0.0.1
 * Author:            WordPress.org
 * Author URI:        https://wordpress.org/
 * Text Domain:       secure-custom-fields-test
 * Domain Path:       /lang
 * Requires PHP:      7.4
 * Requires at least: 6.0
 *
 */

function add_tab_to_icon_picker_tab( $tabs ) {
    $tabs['custom'] = 'My custom Icons';
    return $tabs;
}
add_filter( 'acf/fields/icon_picker/tabs', 'add_tab_to_icon_picker_tab' );

function add_icons_to_scf_icon_picker_tab ( $icons, $field ) {
    return array(
        array(
            'key'   => 'archive',
            'label' => 'Archive',
            'url'   => '/wp-includes/images/media/archive.svg',
        ),
        array(
            'key'   => 'audio',
            'label' => 'Audio',
            'url'   => '/wp-includes/images/media/audio.svg',
        ),
        array(
            'key'   => 'code',
            'label' => 'Code',
            'url'   => '/wp-includes/images/media/code.svg',
        ),
        array(
            'key'   => 'default',
            'label' => 'Default',
            'url'   => '/wp-includes/images/media/default.svg',
        ),
        array(
            'key'   => 'document',
            'label' => 'Document',
            'url'   => '/wp-includes/images/media/document.svg',
        ),
    );
}
add_filter( 'acf/fields/icon_picker/custom/icons', 'add_icons_to_scf_icon_picker_tab', 10, 2 );

In the SCF Field Groups panel, add an Icon Picker field to a fieldset that's used for a given post type. Make sure to enabled the checkbox for the "My custom icons" tab.

image

Create or edit an instance of that post type. Verify that searching and selecting an icon from the icon selector works as before, and that the selected icon is persisted when saving.

Finally, select the new "My custom icons" tab and verify that you can also select an icon there.

icon-selector-new

$custom_icons = apply_filters( 'acf/fields/icon_picker/' . $name . '/icons', array(), $field );

if ( is_array( $custom_icons ) && ! empty( $custom_icons ) ) {
$this->render_icon_list_tab( $name, $custom_icons );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPStan is right about this: The function only supports one argument.

Since I carried the code over from upstream, it looks like it's a bug there.

This means that the newly introduced acf/fields/icon_picker/{tab_name}/icons filter won't actually work there. This leaves us with two options:

  • We punt this PR for now (until it's fixed upstream), or
  • we fix it here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's fix it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that the newly introduced acf/fields/icon_picker/{tab_name}/icons filter won't actually work there.

I misspoke, it's actually working -- jQuery is doing all of the heavy lifting.

Simply removing the extra arg should be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cbravobernal cbravobernal added this to the 6.5.0 milestone Jun 19, 2025
@ockham ockham marked this pull request as ready for review June 19, 2025 16:21
Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and work as expected. Cannot comment on code as is a backport.

@cbravobernal cbravobernal merged commit a15ed1a into WordPress:trunk Jun 20, 2025
5 checks passed
@ockham ockham deleted the add/filter-acf/fields/icon_picker/tab_name/icons branch June 20, 2025 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants