How can I force a file download in the WordPress backend?

Hello everybody I have problem this code must give file to download in wordpress backend but it give me jeson code anybody can help here my code

         $oprion_name_theme =  $this->theme->options['theme_options_field'];

         $shadyssa_options  =  $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE option_name like '%$oprion_name_theme%'");

         $resul = $shadyssa_options[0] ;

         $result = (array) $resul ;
         ob_start() ;
         $output =   json_encode($result) ;
         header("Content-type: application/octet-stream") ;
         header("Content-Disposition: attachment; filename='test.json'");
         header( 'Content-Length: ' . strlen( $output ) );   
         echo $output;

thanks in advance

Answers


typo here:

$result = (array) $resul ;

you want a file download? but you are outputting as json

 header("Content-type: application/octet-stream") ;
 header("Content-Disposition: attachment; filename='test.json'");

which is it that you want?


Need Your Help

Ask and use user input

powershell input

I am trying to make a script that will prompt the user for their computer name, then use that info in a Get-EventLog line. I am new to powershell and I use to do it with batch scripts however I am ...