Multiple get_icons custom sets on same admin page
-
Hi Matt,
Useful plugin.
I have created a custom set in functions.php and it is working perfectly. Problem is I need to have a second custom set on a different options page in the admin.
What is the best way to load the custom set for a specific options page? I see get_options_page but not sure how to use it.
Thanks.
-
When you say you created a ‘custom set’ in functions.php, are you saying that you are filtering the available icons to create your own paired down list of available ones?
If that is what you are saying, I wonder if you would be better served by the built in icon set builder (available in the plugin settings). You can create multiple custom sets of icons using that tool, and select which set you want to use when you add your FontAwesome field to your ACF field group.
If that is not what you are saying, then please rephrase as I am not following you correctly.
Yes, exactly, with the get_icons filter.
I can’t use the plugin settings as I need to roll out this as a finished solution for my client, i.e. I can’t ask them to pick a set for in a given option page. It has to be ready to go.
So I need something like
if_is_optionpage('slug') {}
Thanks.
I see. I haven’t run into that use case, but my first instinct would be to try the get_current_screen() function within the get_icons filter to see if it provides me the information I need.
You can read more about it in the codex here: https://developer.www.ads-software.com/reference/functions/get_current_screen/
This should return an object of the current admin screen you are on, and ideally you could use some of the information from that object in your logic to determine which fields you want to return.
If you get something working with this solution please share a code sample here for other in your situation to follow.
Matt Keys
Not the
acf/get_options_page
hook?I am really bad at all this but isn’t that what is suggested here? https://www.acf-extended.com/features/modules/dynamic-options-pages
If it is, I unfortunately don’t know how to put this all together.
It looks like the
acf/get_options_page
hook is used when you are filtering the ACF settings for your options page.What you are trying to filter is the available icons, so you should continue using the get_icons filter you are using now.
Within the function for that filter, you could try using the
get_current_screen()
function I recommended to see *where* you are at in the admin, and return different icons depending on where you are.That makes sense.
I checked the use of
get_current_screen
with an options page but for some reason this isn’t working.function get_icons( $results ) { $screen = get_current_screen(); if (strpos($screen->id, "my-slug") == true) { $results = array( ... ); } return $results; } add_filter( 'ACFFA_get_icons', 'get_icons', 10, 1 );
Thanks so much for your help.
- This reply was modified 4 years, 2 months ago by dbrabyn.
It can be very helpful in these situations to see what is being returned in that $screen variable to understand why something isn’t validating true in your if statement.
There are lots of different ways developers do this, a common method is:
var_dump( $screen );
which will print the data out to the screen when you visit the page the code is being executed on.Personally I often use a little function that creates a log.txt file in the root of wordpress installation (document root):
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/log.txt', print_r($screen, true));
Seeing the value of ‘id’ inside of ‘screen’ should help debug why your code isn’t working.
I did check using the function mentioned at the end of this post.
And the slug is indeed there. Your 2 methods also show the same:
public 'id' => string 'toplevel_page_race-calendar'
And I am checking with:
if (strpos($screen->id, "race-calendar") == true) {}
The OP was created with ACF Extended maybe that is relevant although I don’t quite see why it would be.
It looks like that will be an effective method of checking the page you are on then. “toplevel_page_race-calendar” will be unique to the ‘race-calendar’ page. Your other options page should have a different id.
You can put another log inside of your IF statement to verify that the code has ‘entered’ that statement (that it evaluated true as you expected). If that second log happens then you can move past the question of how you will determine which page you are on.
If you are still having issues at that point then you will need to debug the next part/remaining code of your function to see where it is failing.
You’re right, the page check is working.
So the fail would come from here (ellipsis for more of the same):
$results = array( "list" => array( "fas" => array( "fas fa-trophy" => "<i class=\"fas\"></i> trophy", ... "fas fa-grin-squint-tears" => "<i class=\"fas\"></i> grin-squint-tears", ), ), "details" => array( "fas" => array( "fas fa-birthday-cake" => Array( "hex" => "\f1fd", ) ), ... "fas" => array( "fas fa-award" => Array( "hex" => "\f559", ) ), ) );
Used in a sub_field in a repeater.
Could it be a priority thing? I have
add_filter( 'ACFFA_get_icons', 'get_icons', 10, 1 );
The sub_field is showing all icons.
Just occurred to me that the field may not in fact be working perfectly as I checked it was before adding the second field.
And indeed it isn’t. After removing the OP ID check, the set on options page A gets filtered as expected but the set on options page B gives “no results found”.
If I deactivate the group field located on option page A, the set on options page B still gives “no results found”.
Is there an issue with using the get_icons function across multiple field groups?
Hello guys,
ACF Extended developer here! I saw your mention, so I permit myself an intervention. It looks like you’re trying to target a specific ACF Options Page in order to apply a hook.
I had that problem in the past, and after digging around with
$screen->id
and having inconsistent results, I found the easiest way to achieve this.You can use the
current_screen
hook in conjunction with the$plugin_page
global WP variable to check the current ACF Options Page. Here is a code example:/* * Current Screen (only executed in the admin) */ add_action('current_screen', 'my_acf_options_page_screen'); function my_acf_options_page_screen(){ // Get $plugin_page global $plugin_page; // Bail early if there is no $plugin_page if(!$plugin_page) return; // Get the current Options Page (if any) $page = acf_get_options_page($plugin_page); // Bail early if the Options Page menu slug is not 'my-options-page' if(acf_maybe_get($page, 'menu_slug') !== 'my-options-page') return; // ACFFA Get Icons add_filter('ACFFA_get_icons', 'my_acf_options_page_icons'); } /* * ACFFA Get Icons filter */ function my_acf_options_page_icons($results){ // Do something... // Return return $results; }
Hope it helps!
Have a nice day ??
Regards.
Thanks hwk.
Unfortunately that has resulted in no filtering at all.
Both icon fields are repeater sub_fields. Does that change things?
Hello,
I’m not sure how the ACFFA filter is supposed to work, or if that’s the good hook to achieve what you’re trying to do (I’ll leave that to @mattkeys). But the method to apply a specific code/filter in a specific Options Page admin is working.
Here is the code + log: https://i.imgur.com/95YtSpM.jpg
Here is the options Page screen: https://i.imgur.com/Ehizioh.jpgThe
current_screen
hook is executed right afteradmin_init
in the main/wp-admin/admin.php
file, which is pretty high in the hooks fire sequence. See screenshot: https://i.imgur.com/hOIUl2d.jpgYou can also try to directly use the
$plugin_page
andacf_get_options_page($plugin_page)
in your ACFFA filter, maybe that would work.Regards.
Hey @hwk-fr, thanks for lending your expertise here.
@dbrabyn, I do not expect the fact that these are repeater sub fields to have any effect here.
As for why the filters recommended by @hwk-fr didn’t work, I am guessing that the ACFFA_get_icons filter is called before the current_screen filter is called. So your function hasn’t been registered yet. ACFFA_get_icons gets called around the same time as acf/include_field_types, which gets called very early (although I haven’t checked through the code to compare it to current_screen specifically).
Back to the question of why this isn’t working for you … it is hard to say from where I sit. The priority on your hook looks fine to me. The logging that you did earlier suggests that the functions are being called correctly. It sounds like you are settings and returning the array of values you want for your field. This is going to take some further debugging of your code and following it through the plugin (probably in /fields/acf-font-awesome-v5.php) to understand where things are failing. This isn’t something that I can easily walk you through, but by reading the code and logging out values along the way you can see what is happening, and ideally pinpoint where the failure is.
- The topic ‘Multiple get_icons custom sets on same admin page’ is closed to new replies.