Ok. I made one more script:
wp-content/templates/elegant/page-pdf-2.php
@session_start();
$name_file = $_SESSION['session_name'];
if (mime_content_type($file) == 'application/pdf')
{
define('WP_USE_THEMES', false); // These lines I add only in WP script file
status_header(200); // These lines I add only in WP script file
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $name_file . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
@readfile($file);
}
The problem is that I didn’t get the SESSION value when I am going directly yo the script: my-page-url-link.com/wp-content/templates/elegant/page-pdf-2.php
-
This reply was modified 4 years ago by
phpwpuser.
I spent 2-3 days with this issue.
I had to load external PHP. It was no problem. Problem was that external script doesn’t get SESSION from WordPress. Spending few days with this issue got me a very simple solution. In the beginning of the external (same domain, but not in WP structure) PHP file you need too add two things:
1. Load wp-load.php file. That allows to work with WP settings and SESSIONS.
2. Add template comment session if this file is inside the template directory. In the beginning below, comment is not full. You should use your comment section according to your template.
Nobody helped me. But I will leave this solution if anybody needs it in the future.
SOLUTION:
<?php
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
/**
* The template for displaying all pages.
*
.....
-
This reply was modified 4 years ago by
phpwpuser.
I’m glad you found a solution. Marking as resolved.
FWIW, requiring wp-load.php as a way to load the WP environment is strongly discouraged because it’s not 100% portable between WP sites. This is because the file’s location on the server is ambiguous since theme and plugin files could optionally reside elsewhere on the server. If you do this for your own site and don’t intend to use the plugin elsewhere, then it’s OK to require wp-load.php since you know where it is on your server.
For anyone inclined to submit their plugin or theme to the WP repository, know that requiring wp-load.php in a PHP file is grounds for rejection due to non-portability. There are limited acceptable ways to invoke the WP environment. Mainly by running requests through admin-ajax.php or admin-post.php. Or place your PHP code on a dedicated page template or within an appropriate action or filter hook.