programing

워드프레스에 http 헤더 추가

sourcejob 2023. 9. 26. 22:20
반응형

워드프레스에 http 헤더 추가

주문형 zip 파일을 주문형으로 만들려고 하는데 잘 작동하는 것 같은 코드를 찾았습니다. http://www.9lessons.info/2012/06/creating-zip-file-with-php.html

워드프레스 템플릿에 코드를 삽입했는데 유일한 것은header()

템플릿을 로드하기 전에 전송해야 합니다.

워드프레스로 어떻게 해야 합니까?

여기 머리글이 있는 코드가 있습니다.

$zip = new ZipArchive();            // Load zip library 
$zip_name = time().".zip";          // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
    $error .=  "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['files'] as $file){               
    $zip->addFile($file_folder.$file);          // Adding files into zip
}
$zip->close();
if(file_exists($zip_name)){
    // push to download the zip
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
    readfile($zip_name);
    // remove zip file is exists in temp path
    unlink($zip_name);
}

워드프레스에는 갈고리가 달려있습니다.머리글을 에 추가합니다.send_headers호출하여 후크를 걸었습니다.add_action기능.

$zip = new ZipArchive();
$zip_name = time().".zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
    $error .=  "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['files'] as $file) {               
    $zip->addFile($file_folder.$file);
}
$zip->close();
if(file_exists($zip_name)){
    add_action( 'send_headers', 'my_headers' );
    readfile($zip_name);
    // put this somewhere or return it
    // so it can be retrieved later, otherwise
    // it might print before your headers
    // are sent
    unlink($zip_name);
}

function my_headers() {
    header('Content-type: application/zip');
    header('Content-Disposition: attachment;
}

이것은 모두 당신의 기능에 들어갈 필요가 있을 것입니다.functions.php테마 폴더의 파일

워드프레스가 출력에 어떤 것을 추가하기 전에 실행되는 후크를 사용해야 합니다.그러한 후크 중 하나는 "init"입니다.

function do_my_stuff_with_headers() {
    // ...
}
add_action( 'init', 'do_my_stuff_with_headers' );

편리한 것이 있습니다.wp_headers배열을 사용하여 사용자 지정 헤더를 포함할 수 있는 필터key=>value쌍들.

필터를 사용하면 헤더 전송이 적절한 시간에 처리됩니다.

이 사용 사례의 경우:

add_filter( 'wp_headers', function( array $headers ) {
    $headers['Content-type'] = 'application/zip';
    $headers['Content-Disposition'] 'attachment';
    return $headers;
});

언급

언급URL : https://stackoverflow.com/questions/13517548/add-http-header-to-wordpress

반응형