0

I am trying to create a button on the back end to call a php function in functions.php. I added the button as so:

 add_action( 'edit_form_after_title', 'update_my_data');


function update_my_data( $post ) {
    if( 'item' == $post->post_type )
        echo '<a href="#" class="button-primary">Update PDF</a>';
}

and the function I need to execute on click is this:

function mysave($id, $post) 
{

    if ($post->post_type != 'item'){
        return;
    }

     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return;
    }

    if ( (defined( 'REST_REQUEST' ) && REST_REQUEST ) ){    
     return; 
    }

   


    if (wp_is_post_revision($id)) {
        return;
    }

    if (wp_is_post_autosave($id)) {
        return;
    }
    
    if ( 'trash' === $post->post_status ) {
        return;
    }

     if ( 'draft' === $post->post_status ) {
        return;
    }



    $dig = get_post_meta($id, 'wpcf-digitisation-project-no')[0];
    #$hmml = get_post_meta($id, 'wpcf-hmml-project-no')[0];
    $inv = get_post_meta($id, 'wpcf-inventory-no')[0];
    
    if($dig!=""){
        $command = escapeshellcmd('python3 '. dirname(__file__). '/create_pdf.py digital '.$inv);
    }else{
        $command = escapeshellcmd('python3 '. dirname(__file__). '/create_pdf.py hl'.$inv);
     }
    
    exec( $command, $output, $ret_code);   
    
    
    

}

Right now this function is associated with the hook save_post but I want the function of update_post_meta and this function to be seperate ie: the original update button will update the meta data as originally intended, and create a new button Update PDF which will call the mysave function

Martha
  • 31
  • 5
  • Instead of having a link that just reloads the current page, it might make more sense if you did this via AJAX. https://developer.wordpress.org/plugins/javascript/ajax/, https://codex.wordpress.org/AJAX_in_Plugins – CBroe Jul 19 '22 at 06:58
  • Check this Ajax example, should do what you want: https://stackoverflow.com/a/44013363/1287812 (and if you search for "ajax" on my profile, you'll find other working examples) – brasofilo Jul 19 '22 at 14:20

0 Answers0