如何从函数中检索变量?



我知道网上有一些关于如何实现这一点的项目,但我不能让它工作…谁能给我指个正确的方向?

我目前有两个函数。我想在function2中使用function1中的变量($new_data)。

功能1:

function manipulate_form_submission( $record, $ajax_handler ) {
$form_name = $record->get_form_settings( 'form_name' );
$form_data = $record->get( 'fields' );

//change the names of fields before we send them somewhere
$new_data = array(
'email'         => isset( $form_data['email']['value'] ) ? $form_data['email']['value'] : '',
'url'           => isset( $form_data['url']['value'] ) ? $form_data['url']['value'] : ''
)
}

功能2:

add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){
$order = wc_get_order( $order_id );
$response = wp_remote_post( 'https://mywebsite.com/insert_values.php', array( 'body' => $new_data) );

}

提前感谢!

就像这样在两个函数之外初始化变量:

<?php
$new_data = false;
function manipulate_form_submission( $record, $ajax_handler ) {
global $new_data;
$new_data = "New value";
}
function so_payment_complete( $order_id ){
// The variable is accessible here
global $new_data;
echo $new_data;
}
?>

最新更新