Support

Account

Home Forums Feature Requests Hooks on options pages

Solving

Hooks on options pages

  • It would be nice if the options page(s) had hooks to insert content. I just read This thread and wanted to chime in with the same desire.

    In my case, I would like to add a link or button to the options page to clear the cache of an API request. It’s the most natural place to add it, as other settings for this API are there too.

  • 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
      
    ?>
    
  • Thanks for this! I was kind of hoping things would have changed/improved since the previous thread.

    I ended up adapting your example in that old thread, where you searched and replaced the admin page output. I wanted a button to reside in the middle of a field group, so I added a “Message” field in the group, where the message text is something like FLUSH_API_CACHE, which I then replace with HTML for the button/link, which just checks for a URL parameter and deletes the transient if present.

    But I’ll make sure to remember this thread for later use.

  • I have done this using this add on https://wordpress.org/plugins/acf-enhanced-message-field/

    I have added “buttons” that fire ajax requests and other things. It has not been updated in a while and I’m getting a little worried about it.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.