Support

Account

Home Forums Add-ons Options Page When using 'save_post' action, how do you identify which options page?

Solved

When using 'save_post' action, how do you identify which options page?

  • I currently have two options pages. I’m using the save_post hook to work with the field values when saving, but I need to be able to identify which options page is actually being updated. When using ACF on regular posts this is easily done as $post_id is passed as an argument to the function I”m hooking in:

    function my_acf_save_post($post_id) {
        var_dump($post_id); // Returns ID of post (INT)
        die();
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
    

    However when I save an options page, $page_id is (obviously) not a post ID, since it’s I’m not dealing with an actual post, it’s just a string (‘options’). Is there any way of identifying which options page is actually being saved?

  • I’m also wondering what to use in place of $post_id when using save_post with an options page.

    I found the following on a question related to acf_form and options. Not sure if it applies, but I’m going to try it:

    Just set the post_id param to ‘options’.

    I found it on this post: Using acf_form with options plugin.

    My understanding is that all options pages use the options identifier, so I don’t know how you would distinguish between multiple options pages. Perhaps you could test for a unique field for each options page to determine which is which.

    I will test using options in place of $post_id and let you know what I find.

  • Try the get_current_screen() function. http://codex.wordpress.org/Function_Reference/get_current_screen. Not sure that’s the answer, but it may be a place to start.

  • I’ve refactored my function so I no longer need to distinguish between the different option screens. However, I believe Hube2’s suggestion of using get_current_screen() would also have worked

  • Just for your information the get_current_screen function solved the problem in my case. Check my post for an example how to use it:

    http://support.advancedcustomfields.com/forums/topic/acfsave_post-for-specific-options-page/

  • Can I have your contact email? I have some questions I would like to ask you about ACF, of course paying the disturbance. Is available?

  • Just to clarify. For options page acf/save_post works as follows:

    
    function my_acf_save_post($post_id) {
        var_dump($post_id); // returns: 'options'
        var_dump($screen->base); // returns: '[parent-slug]_page_[your-menu-slug]'
        exit();
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
  • Here’s a quick one to get the current screen slug…

    function currentscreen_test(){
    	$screen = get_current_screen();
    	echo '<br><h1>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;' . $screen->id . '</h1>';
    }
    add_action('current_screen','currentscreen_test');
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘When using 'save_post' action, how do you identify which options page?’ is closed to new replies.