0

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 ?

  • 1
    Is this in the classic editor or the block editor? Note that there are hooks in the block editor for this that don't exist in the classic editor. But considering what you're doing, is it not easier to just do this in PHP on the save hook instead of adding the complexity and fragility of a second HTTP request? Doing this is not a good idea – Tom J Nowell Jun 25 '19 at 15:07
  • Hi thank you for you answer. I use gutemberg but but this is not related to the recording. My post meta was saved with save_post action. I explain what I want to do : User write a link inside an input text, I take the URL and I want to display <a href="URL_WRITTEN_BY_USER" without refresh admin, so I need to use a javascript function after update post action (when user click to update button and the update has done). – Nouniz Jun 25 '19 at 15:25
  • Some context as to what you're trying to do would be useful, right now you're only explaining how it would work, not what it is. I have no idea which metabox or keys you're referring to, and you've provided no code for your metabox either. You explained what you want to do, but the explanation is incomplete – Tom J Nowell Jun 25 '19 at 16:24
  • I suspect though that what you're wanting to do can be done entirely from the theme, and that you're trying to display the author by putting it in the post content – Tom J Nowell Jun 25 '19 at 16:25
  • My code is not important, I added a mebox ( add_meta_box() ) AND function to display metabox that contain an input type text THEN function to save post_meta with add_action( 'save_post', 'vn_save_metabox_value', 1, 2 ); All of this works very well, but on admin panel when I update my post, update was done on ajax, so if I want to add some HTML after my input I can't do this on PHP side, because to display it I need to refresh post edition. I already do it on PHP in show metabox function but I need to do with JS – Nouniz Jun 25 '19 at 16:52
0

I think those articles will help you to deal with Gutenberg or WordPress v5.0 Metaboxes

Adapt Your Plugin for Gutenberg Block API - Part 1

Adapt Your Plugin for Gutenberg Block API - Part 2

The Final Project Hello Gutenberg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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