update_post_meta not working in template_redirect action
Question
I have a problem
I’m trying to listen to a webhook, and I can do it, I check with the help wp_mail function and I get all the data
But I want to make a add data in the database, and for some reason update_post_meta or add_post_meta do not work in this condition
if (trim ($ _ SERVER ['REQUEST_METHOD']) == 'POST')
See my code bellow
add_action( 'template_redirect', 'my_hook' );
function my_hook() {
if( is_wc_endpoint_url( 'order-received' ) ) {
global $wpdb;
$wc_key = $_GET['key'];
$result = $wpdb->get_results(
$wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' && meta_value LIKE %s", $wc_key)
);
$post_id = $result[0]->post_id;
if ( trim($_SERVER['REQUEST_METHOD']) == 'POST') {
$data = file_get_contents("php://input");
$json_data = json_decode($data, true);
if ( $json_data['event'] == 'document.complete' ) {
$document_id = $json_data['document_id'];
//$headers = 'From: My Name <myname@mydomain.com>' . "rn";
//wp_mail('test@gmail.com', 'Theme', strval($document_id), $headers);
update_post_meta((int)$post_id, '_pleasesign_string', strval($document_id));
}
}
}
}
UPDATE
add_action( 'template_redirect', 'my_hook' );
function my_hook() {
if( is_wc_endpoint_url( 'order-received' ) ) {
global $wpdb;
$wc_key = $_GET['key'];
$result = $wpdb->get_results(
$wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' && meta_value LIKE %s", $wc_key)
);
$post_id = $result[0]->post_id;
add_post_meta((int)$post_id, '_pleasesign_string', 'false', true);
if ( trim($_SERVER['REQUEST_METHOD']) == 'POST') {
$data = file_get_contents("php://input");
$json_data = json_decode($data, true);
if ( $json_data['event'] == 'document.complete' ) {
$document_id = $json_data['document_id'];
//$headers = 'From: My Name <myname@mydomain.com>' . "rn";
//wp_mail('test@gmail.com', 'Theme', strval($document_id), $headers);
update_post_meta((int)$post_id, '_pleasesign_string', strval($document_id));
}
}
}
}
0
phpmyadmin, wp-update-post, wpdb
4 years
2019-11-23T06:39:03-05:00
2019-11-23T06:39:03-05:00 0 Answers
93 views
0
Leave an answer