How can I trigger a javascript function after a post update on edit post screen in wordpress admin panel?

I use WordPress 5.2.2 and I develop a custom theme.

EDIT : On my theme I added a metabox who show an input type=”text”.

Code in functions.php of my theme

if ( ! function_exists( 'vn_add_metabox' ) ) {
    function vn_add_metabox () {
        add_meta_box('vn_youtube_single_metabox', __('Youtube link', WP_THEME_TEXTDOMAIN), 'show_vn_youtube_link_metabox', array('post', 'page'), 'side', 'low');

    }
    add_action( 'add_meta_boxes', 'vn_add_metabox' );
}

This metabox call show_vn_youtube_link_metabox who display an input text and youtube video iframe

if (!function_exists('show_vn_youtube_link_metabox')) {
    function show_vn_youtube_link_metabox()
    {
        global $post;
        show_vn_input_text('vn_youtube_link'); // <input type="text" name ="vn_youtube_link" value="VALUE_POST_META" />
        echo '<br/>'.vn_get_youtube_iframe(get_post_meta( $post->ID, 'vn_youtube_link', true )); /*<iframe class="youtube-video-iframe" src="'.VIDEO_LINK.'?rel=0" width="790" height="496" allowtransparency="true" style="width:100%;" frameborder="0" allowFullScreen allow="encrypted-media" ></iframe> */
    }
}

I save value of postmeta on action save_post

if ( ! function_exists( 'vn_save_metabox_value' ) ) {
    function vn_save_metabox_value ( $post_id, $post ) { // Enregistrement des données dans la base WordPress.
        //évite de perdre des données à cause de l'enregistrement automatique
        if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || isset( $_REQUEST[ 'bulk_edit' ] ) ) {
            return $post->ID;
        }
        // Vérification des droits de l'utilisateur.
        if ( ! current_user_can( 'edit_post', $post->ID ) ) {
            return $post->ID;
        }

        vn_save_meta_box_content($post, 'vn_youtube_link');

        return $post->ID;
    }

    add_action( 'save_post', 'vn_save_metabox_value', 1, 2 );
}

I use a function to control value of postmeta and save it

if ( ! function_exists( 'vn_save_meta_box_content' ) ) {
    function vn_save_meta_box_content ( $post, $meta_box_id, $input_name = '' ) {
        if ( ! isset( $input_name ) || empty( $input_name ) ) {
            $input_name = $meta_box_id;
        }

        if ( ! isset( $_POST[ $input_name ] ) || empty( $_POST[ $input_name ] ) ) {
            delete_post_meta( $post->ID, $meta_box_id );
        } else {
            if ( get_post_meta( $post->ID, $meta_box_id, true ) ) {
                update_post_meta( $post->ID, $meta_box_id, $_POST[ $input_name ] );
            } else {
                add_post_meta( $post->ID, $meta_box_id, $_POST[ $input_name ] );
            }
        }
    }
}

I want to execute a JS function (AJAX or other) after the AJAX done by WordPress to update post (XHR POST http://localhost/wp_uimm/wp-json/wp/v2/pages/5?_locale=user) on admin post edit screen. This javascript function will be used to display the iframe or link of the youtube video without refreshing the post edit screen.

Is there a callback to execute a function when wp POST has done ?

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How can I trigger a javascript function after a post update on edit post screen in wordpress admin panel?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.