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 ?
Topic wp-update-post wp-admin Wordpress
Category Web