Title: Conditional send_headers
Last modified: August 21, 2016

---

# Conditional send_headers

 *  [A31](https://wordpress.org/support/users/a31/)
 * (@a31)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/)
 * 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 7 replies - 1 through 7 (of 7 total)

 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/#post-4693636)
 * My guess: To call get_the_title() you need the $post object not the database 
   object $wpdb.
 *     ```
       global $post;
   
       $MyTitle = get_the_title( $post->ID );
   
       if( $MyTitle === 'ViewPDF' ) {
   
       	add_action( 'send_headers', 'viewPDF' );
   
       }
       ```
   
 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/#post-4693772)
 * Another guess^^ The sent_headers hook is being executed before the post is even
   being loaded. Therefore, at the time when your function is running there won’t
   be a post (title) available yet.
 * A bit dirty, but what you might do is to hook into the redirect hook, for example.
   Then, in your function, check for the proper title and if the title is right 
   not only set the headers but also do the output.
 *     ```
       add_action( 'template_redirect', 'viewPDF' );
   
       function viewPDF()
       {
   
         global $post;
         global $wpdb;
         global $FileContent;
   
         $MyTitle = get_the_title( $post->ID );
   
         if ( $MyTitle !== 'viewPDF' )
         {
           return;
         }
   
         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);
   
         // eg readfile( $File ) or whatever your file is
   
         exit;
   
       }
       ```
   
 *  Thread Starter [A31](https://wordpress.org/support/users/a31/)
 * (@a31)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/#post-4693776)
 * Hi Chris,
 * Your last post helped a lot and it is working! Thank You!!
 * Can you perhaps help with the next phase?
 * How do I pass a variable into that function from another form?
 * I need to set the WHERE conditions (ID) of the document that needs to be displayed.
 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/#post-4693800)
 * How do you get the ID? Is it being sent via the form or do you detect it after
   the form has been send and according to the submitted data?
 *  Thread Starter [A31](https://wordpress.org/support/users/a31/)
 * (@a31)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/#post-4693801)
 * Preferably it is sent from a form.
 * The user selects the document he /she wishes to view, that then sends the ID 
   of the doc and is then displayed.
 * Hope that makes sense?
 *  Thread Starter [A31](https://wordpress.org/support/users/a31/)
 * (@a31)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/#post-4693803)
 * What i have done now, is to write a unique string into the database when the 
   user clicks view for a specific document, then redirects to the ViewPDF page,
   and there pull from the database the criteria.
 * This does work, but seems like a long long way around just to pass a variable?
 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/#post-4693806)
 * It is. I would stick with sending the ID along with the form data. Then you could
   simply check $_GET or $_POST (depending which method you’re using to send the
   form).
 *     ```
       ...
   
         $uid = ( isset( $_GET['uid'] ) ) ? (int) $_GET['uid'] : 0;
         // Or $_POST depending on how you're sending the form and what the name for the ID parameter is.
         // When you do a redirect to /ViewPDF/ just add the ID as parameter /ViewPDF/?uid=123 and you can check $_GET
   
         if ( $uid < 1 )
         {
           // Error handling: Missing or invalid parameter
         }
   
         // Always treat external inputs as potentially harmful and add another layer of security by "preparing" db queries
         $query      = $wpdb->prepare( "SELECT * FROM em_purchaserequest_additionalinfo_tbl WHERE ID = %d", $uid );
   
         $mydataset  = $wpdb->get_row( $query );
   
         if ( $mydataset === null )
         {
           // Error handling: The query is valid and has been secured but the ID simply does not exist
         }
   
         $recordID = $mydataset->ID;
         ...
       ```
   

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Conditional send_headers’ is closed to new replies.

## Tags

 * [BLOB](https://wordpress.org/support/topic-tag/blob/)
 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [header](https://wordpress.org/support/topic-tag/header/)
 * [hook](https://wordpress.org/support/topic-tag/hook/)
 * [if](https://wordpress.org/support/topic-tag/if/)
 * [pdf](https://wordpress.org/support/topic-tag/pdf/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 7 replies
 * 2 participants
 * Last reply from: [Chris](https://wordpress.org/support/users/zenation/)
 * Last activity: [12 years, 2 months ago](https://wordpress.org/support/topic/conditional-send_headers/#post-4693806)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
