Support

Account

Home Forums Add-ons Options Page Add custom message to option page

Solved

Add custom message to option page

  • Hi,

    I need to add a custom message to the top of option page.
    I need to use some php code to display some values in the message box, there is any way to do it?

    Thank yuo

  • There isn’t anything built into ACF that will let you alter the options page to add a message to the top. A while back I posted an example of how this can be accomplished in my example acf filters and function repo on github https://github.com/Hube2/acf-filters-and-functions/blob/master/customized-options-page.php

  • Hi John!

    Thank you for your reply, it is exactly what i was looking for. I’ve got a small question. If i use it on top level page, it works perfectly, but i can’t make it works with sub option page.

    My code is:

    
    
    if( function_exists('acf_add_options_page') ) {
    
        acf_add_options_page(array(
            'page_title' 	=> 'Impostazioni',
            'menu_title'	=> 'Impostazioni',
            'menu_slug' 	=> 'theme-trends-settings',
            'capability'	=> 'edit_posts',
            'redirect'	=> false
        ));
    
        acf_add_options_sub_page(array(
            'page_title' 	=> 'Forms',
            'menu_title'	=> 'Forms',
            'parent_slug'	=> 'theme-trends-settings',
        ));
    
        acf_add_options_sub_page(array(
            'page_title' 	=> 'Gestione livelli abbonamenti',
            'menu_title'	=> 'Gestione livelli abbonamenti',
            'parent_slug'	=> 'theme-trends-settings',
            'menu_slug'     => 'gestione-livelli-abbonamento'
        ));
    
    	add_action('theme-trends-settings_page_gestione-livelli-abbonamento', 'before_acf_options_page', 1);
    function before_acf_options_page() {}
    add_action('theme-trends-settings_page_gestione-livelli-abbonamento', 'after_acf_options_page', 20);
    function after_acf_options_page() {}
    
    

    What i’m doing wrong?
    Thank you

  • Some of this is guess work, but the hook should be what you have.

    
    theme-trends-settings_page_gestione-livelli-abbonamento
    

    If you want to do some testing, open the ACF file
    advanced-custom-fields-pro/pro/admin/options-page.php
    on line 459 you’ll find the function html

    
    function html() {
    		// add this line to test
    		echo '<br><br>',current_filter(),'<br><br>';
    

    if you temporarily add the above test code the hook that is being used to display the options page will be output at the top of your options page. I’d be curious to know what it is.

  • Thank you John!!
    Using the current_filter() i’ve founded was i was doing wrong. The right one is:
    impostazioni-fashion_page_gestione-livelli-abbonamento

    Basically it doesn’t use the “menu_slug” but use “menu_title”. (Maybe is my fault because i’ve used two different names).

    Anyway thank you very much!

  • That’s curious, when I test this it uses the menu slug every time.

  • So basically it’s a WordPress hook (not ACF hook), reference:

    
    Top level pages	admin.php?page={page_slug}	toplevel_page_{page_slug}
    Sub pages	admin.php?page={page_slug}	{parent_slug}_page_{page_slug}

    https://codex.wordpress.org/Plugin_API/Admin_Screen_Reference

    Thank you @hube2 for this ! 🙂


    @roby94
    reference says it is using slugs, and your final code does not corresponde with your initial code, so I just don’t believe it started using titles, that would be horrible approach.

  • @jave-web , @roby94 is actually right. This was really annoying for me so I tracked it down.

    wp-admin/includes/plugin.php on line 1328 (5.5.3)
    $admin_page_hooks[ $menu_slug ] = sanitize_title( $menu_title );

    current_filter will return the correct filter but the correct way is
    sanitize_title( $parent['menu_title'] ) . '_page_' . $child['page_slug']

  • @storm Well, in wp-admin/includes/plugin.php the get_plugin_page_hook()‘s doc.comments specifically state it’s a slug, get_plugin_page_hookname() also mentions those are slugs, and the code you’ve posted yourself is just storing the title – but – it is storing it under a slug – so the key of the hook is from the slug.

  • Actually, it’s still more complicated than all that. It changes based on the parent page’s “redirect” setting. If that parameter is “true”, it treats the first subpage as the top-level page, so then the first subpage’s hook name will be “toplevel_page_acf-options-{$subpage_slug_built_from_sanitized_title}”, where “toplevel” is the actual literal value. Subsequent subpages’ hook names will be “{$parent_slug}_page_acf-options-{$subpage_slug_built_from_sanitized_title}”.

    So for example, using this code:

    if ( function_exists( 'acf_add_options_page' ) ) {
    	
    	acf_add_options_page( [
    		'page_title' 	=> 'Permalink Manager Transfer',
    		'menu_title'	=> 'Permalink Manager Transfer',
    		'menu_slug' 	=> 'permalink-manager-transfer',
    		'redirect'      => true
    	] );
    	
    	acf_add_options_sub_page( [
    		'page_title' 	=> 'Permalink Manager Transfer - Import',
    		'menu_title'	=> 'Import',
    		'parent_slug'	=> 'permalink-manager-transfer',
    	] );
    	
    	acf_add_options_sub_page( [
    		'page_title' 	=> 'Permalink Manager Transfer - Export',
    		'menu_title'	=> 'Export',
    		'parent_slug'	=> 'permalink-manager-transfer',
    	] );
    	
    }
    
    add_action( 'toplevel_page_permalink-manager-transfer', function() { 
    	echo "<br>toplevel_page_permalink-manager-transfer<br>"; 
    } );
    
    add_action( 'toplevel_page_acf-options-import', function() { 
    	echo "<br>toplevel_page_acf-options-import<br>"; 
    } );
    
    add_action( 'toplevel_page_acf-options-export', function() { 
    	echo "<br>toplevel_page_acf-options-export<br>"; 
    } );
    
    add_action( 'permalink-manager-transfer_page_acf-options-import', function() { 
    	echo "<br>permalink-manager-transfer_page_acf-options-import<br>"; 
    } );
    
    add_action( 'permalink-manager-transfer_page_acf-options-export', function() { 
    	echo "<br>permalink-manager-transfer_page_acf-options-export<br>"; 
    } );

    …with the parent menu’s “redirect” setting = true, you will see this output when clicking on the parent, import, and export options:

    toplevel_page_acf-options-import
    toplevel_page_acf-options-import
    permalink-manager-transfer_page_acf-options-export

    …and with the parent menu’s “redirect” setting = false, you will see this:

    permalink-manager-transfer_page_acf-options-import
    permalink-manager-transfer_page_acf-options-import
    permalink-manager-transfer_page_acf-options-export

    Also, as of Pro version 5.12.2, if the redirect setting is false, it replicates the top level menu selection and adds it as the first submenu item, so that you are able to use both of them to get to the parent page. For this example, that means that even though you only created two submenus, three are displayed.

    All in all, it’s not a very good way to run a railroad.

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

The topic ‘Add custom message to option page’ is closed to new replies.