Run PHP Function When Updating WordPress ACF Options Page

Run PHP Function When Updating WordPress ACF Options Page

I needed to run a custom PHP function that updates custom fields (using Advanced Custom Fields) attached to users when an admin clicks “update” on a back-end options page. The way I did this was using the acf/save_post action.

By itself, the function would be triggered when updating any options page. I didn’t want this. I only wanted the function to run when updating one specific options page. To fix this, I wrapped the function’s code in an if statement that checks if get_current_screen() was equal to my options page.

add_action('acf/save_post', 'update_users_reporting_log'); function update_users_reporting_log() {     $screen = get_current_screen();     if (strpos($screen->id, "reported-content") == true) {         // Function code here:               endif;     } }

strpost() will check to see if a particular string occurs inside another. Actually, it will find the position of the occurance, but I didn’t care about the position, only if it did occur. And the string I was checking inside of is the return of get_current_screen(). The name of my options page is “reported-content”.

Now, when a user clicks the “update” button on that particular options page, my custom function will run, updating custom fields elsewhere on the site.

Leave a Reply

Your email address will not be published. Required fields are marked *