0

I have several PHP pages that I want to appear on a WordPress site.

I'm using the Allow PHP in Posts and Pages plugin, and adding the WordPress pages with code such as:

[php]include $_SERVER['DOCUMENT_ROOT'] . '/brand_details.php';[/php]

I'm doing it this way because I want the solution to be as portable as possible (so I'd rather not edit functions.php, which could be overwritten in the next WordPress update), and I want it to be something that relatively non-technical WordPress folks can get their heads around.

It works reasonably well with most pages, but I'm struggling with pages who's content depends on a URL parameter. In particular, I'm struggling to set the page title in this situation.

For example, I have a page called brand_details.php that displays details (name, address, website, etc.) of Brands.

It's called with a URL parameter, such as ?brand_url=acehardware

From that parameter, I look up the Brand details in a MySQL table and display the results.

This works OK, but I lose the WordPress header and footer. To get around this, I tried adding the following code to the start and end of my PHP page:

At the start:

require $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
get_header();

At the end:

get_footer();

This kinda worked, although I ran into several styling issues - but maybe that's to do with the theme I'm using (which is Divi).

But what I can't do is display the brand name in the page title. To use the example above, I want to display a title such as "Ace Hardware Brand Details". This will obviously need to change depending on the brand that's selected.

So my next step was to strip out this code and try using the wp_title filter instead. Here's what I did:

  1. I made a copy of page.php in my theme folder called page-custom.php with the get_header(); call commented out and with Template Name:Custom inserted at the top.

  2. I made a copy of header.php in my theme folder called header-custom.php with the following line of code inserted into the <head> section:

    <?php add_filter( 'wp_title', 'set_custom_title' ); ?>
    
  3. I added the following code to the brand_details.php:

    function set_custom_title($data)
    {
     return "$brand_name Details Page";
    }
    get_header('custom');
    
  4. I created a WordPress page based on the Custom page template that calls the brand_details.php page.

What I was hoping would happen was for the page to appear as a standard WordPress page, but with the page title set according to the brand being displayed.

What actually happened was that the WordPress header and footer appeared properly but the $brand_name variable mentioned above didn't get used by the set_custom_title function, so the page title ended up as "Details Page" without the brand name.

The (simplified for readability) code where I look up the brand name, just before invoking this function, is as follows:

$brand_url = $_REQUEST["brand_url"];
$sql = "SELECT * FROM brand_details WHERE brand_url = '$brand_url' LIMIT 1";
$stmt = $db_conn->prepare($sql);
$stmt->execute(array());
$row = $stmt->fetch();
$brand_name = $row['brand_name'];

I know this works because I use the results in the main page.

I've tried modifying the last line to read global $brand_name = $row['brand_name']; in the hope that the $brand_name variable becomes available in the set_custom_title function, but to no avail.

I welcome any advice on how to achieve this, and I'm open to alternative solutions to get the job done.

Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
Android63
  • 45
  • 1
  • 1
  • 7

1 Answers1

0

I've solved this. Here's how I did it:

  1. I created a page template, based on the Divi theme's page.php, called page-brand_details.php and added the code from my php page to it. I also deleted get_header(); from the start and added the following code at the end:

    function set_custom_title($data) { global $brand_name; return $brand_name." Brand Details Page"; } get_header('custom');

  2. I created a header template based on the Divi theme's header.php, called header-custom.php, where I replaced the line <title><?php elegant_titles(); ?></title> with the following lines:

    <?php add_filter( 'wp_title', 'set_custom_title' ); ?>
    <title><?php wp_title(); ?></title>
    
  3. I created a blank WordPress page using the template created above.
Android63
  • 45
  • 1
  • 1
  • 7