从函数外部调用变量

时间:2017-09-08 09:19:01

标签: php wordpress function

如何从函数外部调用~A { delete b; }

$attach_id

2 个答案:

答案 0 :(得分:0)

当你从函数中获取值然后你需要调用这个函数这是你的代码,这是返回12的演示代码

<?php
function kv_handle_attachment($file_handler, $post_id)
{
    return 12;
}

$attach_id = kv_handle_attachment('1', 12);
echo $attach_id;

所以像这样更新您的代码

<?php
function kv_handle_attachment($file_handler, $post_id, $set_thu = false)
{
    // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attach_id = media_handle_upload($file_handler, $post_id);
    return $attach_id;
}

所以请调用你的函数

$attach_id = kv_handle_attachment($file_handler, $post_id);

或者这是更明确的选择

$attach_id = kv_handle_attachment($yourdatahere); //$yourdatahere update as your parameter which you need to send but your first 2 parameter is required so need to send this must but not empty 
echo $attach_id; 

答案 1 :(得分:-1)

您可以使用全局变量。

function kv_handle_attachment($file_handler,$post_id,$set_thu=false) {
    global $attach_id;
    // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attach_id = media_handle_upload( $file_handler, $post_id );
    return $attach_id;
}

global $attach_id;
echo $attach_id;