Home › Forums › Add-ons › Options Page › 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>       ' . $screen->id . '</h1>';
}
add_action('current_screen','currentscreen_test');
The topic ‘When using 'save_post' action, how do you identify which options page?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.