Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

A31 on "Conditional send_headers"

$
0
0

Hi all,

I am trying to display a PDF file that has been stored in the database.
If I use the following code it wants to display the PDF on every page:

<?php
	global $wpdb;
//*******************************************************************************************************************************************

function viewPDF()
	{
		global $wpdb;
		global $FileContent;

		$mydataset = $wpdb->get_row("SELECT * FROM customTest_tbl WHERE ID = 28");

		$recordID = $mydataset->ID;
		$FileType = $mydataset->FileType;
		$FileSize = $mydataset->FileSize;
		$FileName = $mydataset->FileName;
		$FileContent = $mydataset->FileContent;

		header("Content-Type: ". $FileType);
		header("Content-Length: ". $FileSize);
		header("Content-Disposition: attachment; filename=". $FileName);

	}

add_action( 'send_headers', 'viewPDF' );

//*******************************************************************************************************************************************

So obviously I would like to add an IF statement that will only run the function if the Page Title = ‘ViewPDF’.
So I tried this code:

<?php
	global $wpdb;
//*******************************************************************************************************************************************
	$MyTitle = get_the_title();

	function viewPDF()
	{
			global $wpdb;
			global $FileContent;

				$mydataset = $wpdb->get_row("SELECT * FROM em_purchaserequest_additionalinfo_tbl WHERE ID = 28");

				$recordID = $mydataset->ID;
				$FileType = $mydataset->FileType;
				$FileSize = $mydataset->FileSize;
				$FileName = $mydataset->FileName;
				$FileContent = $mydataset->FileContent;

				header("Content-Type: ". $FileType);
				header("Content-Length: ". $FileSize);
				header("Content-Disposition: attachment; filename=". $FileName);

	}

if ($MyTitle == 'ViewPDF')
{
	add_action( 'send_headers', 'viewPDF' );
}

//*******************************************************************************************************************************************
?>

And also tried:

<?php
	global $wpdb;
//*******************************************************************************************************************************************

	function viewPDF()
	{
			global $wpdb;
			global $FileContent;

			$MyTitle = get_the_title();

			if ($MyTitle == 'ViewPDF')
			{
				$mydataset = $wpdb->get_row("SELECT * FROM em_purchaserequest_additionalinfo_tbl WHERE ID = 28");

				$recordID = $mydataset->ID;
				$FileType = $mydataset->FileType;
				$FileSize = $mydataset->FileSize;
				$FileName = $mydataset->FileName;
				$FileContent = $mydataset->FileContent;

				header("Content-Type: ". $FileType);
				header("Content-Length: ". $FileSize);
				header("Content-Disposition: attachment; filename=". $FileName);
			}
	}

	add_action( 'send_headers', 'viewPDF' );

//*******************************************************************************************************************************************
?>

But either two tries does not open the PDF file, only the first code, where there is no IF statement.
I also have checked and confirmed that $MyTitle = get_the_title(); really is = ‘ViewPDF’
Can anyone please help point out my mistake?

Thank you so much!!


Viewing all articles
Browse latest Browse all 8245

Trending Articles