How to automatically open pdf when user open specific page? - wordpress

What im trying to do is when user enter specific page to automatically open pdf instead that page? How can i achive that?

Didn't test, but try adding this to the function.php in your theme:
add_action('init', 'show_pdf');
function show_pdf() {
if (is_page($page_id)) {
header('Content-type:application/pdf');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
readfile($file_path);
exit;
}
}
and replace $page_id with the ID of the page and $file_path with the local path to the PDF file.

Related

Download pdf after submission Contact form 7

I am currently using the mail to sent the pdf and I want them to auto-download once the contact form is filled.
How can I achieve this?
I have already tried this, and it does not cater my need.
I also tries on_sent_ok: location ="pdf url" but nothing helps.
Put this code into your htaccess
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
add this code in your Additional Setting tab in contact form 7
on_sent_ok: "location = 'your pdf file path';"
Try this I thin its word for you
The use of on_sent_ok has been deprecated since 2017.
Instead you need to add a function to your functions.php file. Here is an example.
Replace the contactFormId (999) and the download path respectively for your needs.
add_action( 'wp_footer', 'example_download' );
function example_download() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '999' == event.detail.contactFormId ) {
window.open('https://www.example.com/wp-content/uploads/2021/05/my-document.php', '_self');
}
}, false );
</script>
<?php
}

Send pdf from wp_remote_get to browser

I'm integrating a wordpress site with an external API. I have a form that posts to my server and I call a function on my server that makes a wp_remote_get call to the external API.
The external API returns a PDF, with headers like:
[date] => Fri, 18 Aug 2017 15:59:19 GMT
[x-powered-by] => Servlet/3.0
[cache-control] => no-cache
[content-type] => application/pdf;charset=utf-8
[content-language] => en-US
And the response body is the PDF in nasty string format, which seems like a good start.
How do I pass this PDF to the user's browser?
ie,
$response = wp_remote_get( $url, array ( //stuff the request needs));
if (is_wp_error ($response)) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
//What here?
}
I have to hit my server first, cannot post the form directly to the external API.
I managed this by using javascript to redirect my form submit to a new window on my site, passing along the form information as URL params.
ie,
in my HTML:
<form onsubmit="return qrsDownload()">
And then in javascript:
function qrsDownload() {
// a bunch of jquery and processing to build the URL..
window.open(url, 'My Title', 'width=800, height=600');
}
The page I opened was a single-use page template I created, that I omitted the standard WP templates in (so no header, no footer, no wordpress loop), and in that page's php file:
<?php
if (isset($_GET['someParam'])) {
// started off with logic to verify that I had the params needed
// because anybody could just jump directly to this page now
}
$url = "whateverurl.com/myendpoint";
// additional logic to set up the API call
$server_response = wp_remote_get($url, $args);
if (is_wp_error( $server_response) ) {
// do something to handle error
} else {
// PASS OUR PDF to the user
$response_body = wp_remote_retrieve_body( $server_response);
header("Content-type: application/pdf");
header("Content-disposition: attachment;filename=downloaded.pdf");
echo $response_body;
}
} else {
get_header();
$html = "<div class='content-container'><p>A pretty error message here.</p></div>";
echo $html;
get_footer();
}
?>
The approach is essentially to pass the result from the API straight back out the user, but you need the headers to specific it's a PDF, and the headers have to be set before you write out any output. An easier way to ensure this was to do it in a new window, rather than strictly on the form postback.

Force download PDF file from URL in wordpress

In WordPress, how do I create a link to a file such as "file.pdf" and force it to download instead of opening the file in the browser?
Just add download attribute ( download) on your a tags and leave it empty as a default file name of a file.
Example:
< a href="http://wordpress.org/download/download.pdf" target="_blank" download>WordPress Download PDF</a>
make download.php in theme folder
<?php
$url = $_REQUEST['file_url'];
$filename = basename($url);
$filetype = filetype($url);
header('Content-Disposition: attachment; filename=' . $filename);
header("Content-type: " . $filetype); // act as image with right MIME type
ob_clean();
flush();
readfile($url);
exit;
then hit anchor link from html page. in this code your file url may be same domain or diffrent domain does not matter
click to download

Custom download functionality for wordpress plugin

I am trying to create functionality for a custom made wordpress plugin where when user clicks on the download button appropriate file from the directory should be downloaded related to that post.
I don't want the file to be directly accessible from the URL and want only authorize user to be allowed to download file.
class DownloadM{
function __construct(){
}
function setDownload($file){
//$file = ROOT_DIR_PATH."wp-content/uploads/2016/07/PDF.zip";
echo "<a href='".$file."'>Click here to download</a>";
ob_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=PDF.zip");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
flush();
ob_clean();
readfile($file);
}
}
The file is properly getting downloaded if I placed the link in the href tag or accessed it from the URL directly.
But when I put the file in the header to auto download it doesn't work
Simple solution for you:
header("Content-Type: application/force-download");

How to have a PHP redirect on WordPress?

I want to have a URL like this www.example.com/redirect.php?www.google.com in my WordPress website:
Every time I change www.google.com to another URL it will redirect to that page. I also want to have some urls to show as example.com/redirect.php?www.google.com but redirects to affiliate.google.com.
If there is any plugin please mention that.
create redirect.php and upload it on your wordpress root folder
the and this file
<?php
$query = $_SERVER['QUERY_STRING'];
if (!preg_match("~^(?:f|ht)tps?://~i", $query)) {
$url = "http://" . $query;
header('Location: '.$url);
}
<?php
$links=array
(
'www.google.com'=>'http://google.com',
'google.co'=>'http://google.co.uk',
'google.af'=>'http://google.com.af'
// etc... last line without comma
);
if (array_key_exists($_GET[id],$links))
{
header("HTTP/1.1 301 Moved Permanently");
header("Location:" . $links[$_GET[id]]);
}
else {echo "bad url";}
exit(); ?>
here is the code I was able to find, create a file called redirect.php or other type paste the code and then when you write www.yoursite.com/redirect.php?id=google.co it will redirect to google.co.uk

Resources