I have tried creating a plugin. My plugin file looks like this.

function wpb_adding_scripts() {
    error_log('inside the wpb adding scripts');
    wp_register_script('trigger_build', plugins_url('js/build-trigger.js', __FILE__), array('jquery'),'1.1', true);     

function invoke_save_post(){
    error_log('inside the save post function');

add_action('save_post', 'invoke_save_post');
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );

And my javascript file is just a simple alert statement.

alert('Post has been published/updated');

But what is happening with this code is, the alert box is coming when I view any post visiting it's permalink, I want to show the alert box as soon as the post is published/updated in the edit page of WordPress only.

  • The wp_enqueue_scripts hook only fires on the front end. You need its admin counterpart if you're wanting the JS to only appear in the admin area. Also, keep in mind, save_post can fire outside the Editor - when someone uses Quick Edit, for example, or if the site allows posting by email.
    – WebElaine
    Dec 8, 2021 at 17:12
  • @WebElaine is there any alternative to save_post which I can use because I don't want to fire javascript on front-end, I only want to invoke when the post is published/updated. Dec 9, 2021 at 4:26
  • save_post won't fire on the front end, but it will fire more often than you might expect - in addition to Quick Edit etc., it also fires for autosaves. You probably want to look into enqueueing a script solely for the Block Editor - using enqueue_block_editor_assets instead of wp_enqueue_scripts - and then subscribe to one of the WP data modules so you can run whatever JS you desire right when a post is updated. See developer.wordpress.org/block-editor/reference-guides/data
    – WebElaine
    Dec 9, 2021 at 14:30

1 Answer 1


If you only want an alert then why do you write it in a separate file and enqueue it?

You can directly write it inside the invoke_save_post function using

echo "<script>alert('Api called');</script>";

or if you want to write it in a separate file then you have to write your alert inside the function and call that js function inside the PHP function so it will not execute Until the function is called

for example:

function alert_wrapper(){
     alert('Api has been called');

Here your alert is called in the front end because there may be a chance this file is loaded in the front end so every time file is loaded your code executes because it will not inside any function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.