Support

Account

Home Forums Feature Requests Hooks on options pages Reply To: Hooks on options pages

  • So, your question caused me to look into this some more, since the discussion you linked to is 2 years old. What I found is that ACF is showing options pages and adding custom fields to options pages in a completely different way that it use to.

    Now:

    ACF adds field groups to options pages using add_meta_box() https://developer.wordpress.org/reference/functions/add_meta_box/

    ACF shows field groups by calling do_meta_boxes() https://developer.wordpress.org/reference/functions/do_meta_boxes/

    There is also a hook in ACF named “acf/input/admin_head” and we can use this hook with a priority of <10 or >10 to add our own custom metaboxes before or after the ACF field groups.

    I added an options page with the slug of “test-options-page” and this is the result of my preliminary testing.

    
    <?php 
      
      new add_boxes_to_options_page();
      
      class add_boxes_to_options_page {
        
        public function __construct() {
          add_action('acf/input/admin_head', array($this, 'add_boxes_before'), 1);
          add_action('acf/input/admin_head', array($this, 'add_boxes_after'), 20);
        } // end public function __construct
        
        public function add_boxes_before() {
          $screen = get_current_screen();
          if ($screen->id == 'toplevel_page_test-options-page') {
            add_meta_box('custom-mb-before-acf', 'CUSTOM MB BEFORE ACF', array($this, 'callback_top'), 'acf_options_page', 'normal', 'high');
          }
        } // end public function add_boxes_before
        
        public function add_boxes_after() {
          $screen = get_current_screen();
          if ($screen->id == 'toplevel_page_test-options-page') {
            add_meta_box('custom-mb-after-acf', 'CUSTOM MB AFTER ACF', array($this, 'callback_top'), 'acf_options_page', 'normal', 'high');
          }
        } // end public function add_boxes_after
        
        public function callback_top($post, $args=array()) {
          ?><div><?php echo '<pre>'; var_dump($post); var_dump($args); echo '</pre>'; ?></div><?php 
        } // end public function callback_top
        
        public function callback_bot($post, $args=array()) {
          ?><div><?php echo '<pre>';  var_dump($post); var_dump($args); echo '</pre>'; ?></div><?php 
        } // end public function callback_bot
        
      } // end class add_boxes_to_options_page
      
    ?>