如何在函数wordpress中使用函数声明的变量

时间:2017-12-15 15:27:28

标签: php wordpress function

我有这个功能,我想检查$ item_field是否等于url参数来执行某些操作,但我不知道如何在函数中使用此变量。

function update_buynow_on_cancellation($entry_id, $form_id) {

    //update policy transaction type when entry on cancellation
    if ($form_id == 12) {
        $item_field = $_POST['item_meta'][366]; // get the value of item_meta

        $to = 'stanislav@goliveuk.com';
        $subject = 'Cancellation';
        $headers = 'MIME-Version: 1.0'. "\r\n";
        $headers.= 'Content-type: text/html; charset=iso-8859-1'. "\r\n";
        $message = '<a href="caravan.dev.golivesolutions.co.uk?accept='.$item_field.'">Accept</a><br><a href="caravan.dev.golivesolutions.co.uk?decline">Decline</a>';
        wp_mail($to, $subject, $message, $headers);
    }
}

$accept = $_GET['accept'];

if ($accept = $item_field) {

    global $wpdb, $frmdb;

    $wpdb -> update($wpdb -> prefix.'frm_item_metas', array('meta_value' => 'CX'), array('item_id' => $item_field, 'field_id' => '435'));
    $new_entry_id = FrmEntry:: duplicate($item_field);
    $wpdb -> update($wpdb -> prefix.'frm_item_metas', array('meta_value' => 'CX'), array('item_id' => $item_field, 'field_id' => '390'));

} else {
    //do something
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试在全局变量中使用,以便可以访问变量外部函数

   function update_buynow_on_cancellation($entry_id, $form_id)
   {

    //update policy transaction type when entry on cancellation
    if ($form_id == 12 ) {
       $GLOBALS['item_field'] = $_POST['item_meta'][366]; //here i made it as GLOBAL variable

      $to = 'stanislav@goliveuk.com';
      $subject = 'Cancellation';
      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      $message ='<a href="caravan.dev.golivesolutions.co.uk?accept='.$item_field.'">Accept</a><br><a href="caravan.dev.golivesolutions.co.uk?decline">Decline</a>';
      wp_mail($to, $subject, $message, $headers); 
    }
}

  $accept = $_GET['accept'];

  if($accept = $item_field){

        global $wpdb, $frmdb;

    $wpdb->update($wpdb->prefix.'frm_item_metas', array('meta_value' => 'CX'), array('item_id' => $item_field, 'field_id' => '435'));
       $new_entry_id = FrmEntry::duplicate( $item_field );
$wpdb->update($wpdb->prefix.'frm_item_metas', array('meta_value' => 'CX'), array('item_id' => $item_field, 'field_id' => '390'));



    }else{
    //do something
    }