Home › Forums › Add-ons › Options Page › acf/save_post for specific options page
Hi together,
I’m using a save_post action in order to clear the transient-data of some values saved in a specific options page. The point is that the action will always run when I save any kind of options page. Is it possible to get more specific in there?
function clear_advert_main_transient($post_id) {
if ( $post_id == 'options' ) {
delete_transient('advert_main_transient1');
delete_transient('advert_main_transient2');
delete_transient('advert_main_transient3');
}
//return $post_id;
}
add_action('acf/save_post', 'clear_advert_main_transient', 20);
I found a solution after reading this post: http://support.advancedcustomfields.com/forums/topic/when-using-save_post-action-how-do-you-identify-which-options-page/
While using WP get_current_screen()-function you can check if the ID in the output-array contains the slug of your specific option page.
http://codex.wordpress.org/Function_Reference/get_current_screen
For example you have an options page called “adverts” and want run the function mentioned above when you save this specific option page:
function clear_advert_main_transient() {
$screen = get_current_screen();
if (strpos($screen->id, "acf-options-adverts") == true) {
delete_transient('advert_main_transient1');
delete_transient('advert_main_transient2');
delete_transient('advert_main_transient3');
}
}
add_action('acf/save_post', 'clear_advert_main_transient', 20);
Now WP Transients make even more fun and will save you tons of queries if you have complex acf-fields and loops! 🙂
I have a many option page. For example i have the:
Product Page
Service Page
Theme Option Page
With many fields. Is possible applicate your code in my specific case?
Of course. I have five different option pages.
Create a function for each option page with the the condition above and replace the string “acf-options-adverts” with the slug of the attached option page (you may have a look in the url-bar of each page).
In your case for example I could imagine the code to look like this:
if (strpos($screen->id, "acf-options-product") == true) {…
if (strpos($screen->id, "acf-options-service") == true) {…
if (strpos($screen->id, "acf-options-theme-option") == true) {…
sorry for my answer, but this:
delete_transient('advert_main_transient1');
delete_transient('advert_main_transient2');
delete_transient('advert_main_transient3');
What call?
These functions are just placeholders. You can replace them by any code you would like to run when saving the specific option page.
In my case I clear three transients when I save my option page.
The topic ‘acf/save_post for specific 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.