-1

H Guys, I have a page details.php in WordPress which is run like this [domain.com/product-details?id=706494861]

And I want to show the product title as in the head. I have a listable WordPress theme. I have tried these solutions as well. but the title is not changing still showing my default page title which is Product Details

My details.php The product data I receiving

    <?php /* Template Name: product-details */

get_header();
?>


<!DOCTYPE html>
<html>

<body>


  <?php
  $found = "0";
  $url = 'https://domain.com/xml';
  $rss = simplexml_load_file($url);
  $custom_title = $item->title;
  foreach ($rss->channel as $channel) {
    foreach ($channel->item as $item) {
      if ($_GET['id'] == $item->id) {
        $found = "1";
        echo '<div class="image-container"><img src="' . $item->image_link . '" > </div>';
        echo '<div class="contentspace"></div>';
        echo '<div class="content"><h1>' . $item->title . '</h1>';
        echo '<br><b>Id</b>: ' . $item->id;
        echo '<br><b>SKU</b>: ' . $item->SKU;
        echo '<br><b>Merchant</b>: ' . $item->merchant;
        echo '</div>';
  }
    }
  }
  if ($found == "0") {
    echo 'No results found;
  }
  ?>

</body>

</html>

<?php
get_footer();
?>

here are the variable $item->title which stores the product title, and it is coming by POST call from another page. And I can use it in the title If I get a proper solution.

You can check here

This code I tried this in my function.php

    function wpse62415_filter_wp_title( $title ) {
    if ( is_page_template( 'details.php' ) ) {
        return 'I m the boat details page';
    }
    return $title;
}
add_filter( 'wp_title', 'wpse62415_filter_wp_title' );

The issue is the value of Simply I just want to change the title with my product title name.

2
  • You’re already displaying the title in the template, so you know what the code is. You just need to use it inside the wp_title filter. Jun 17, 2023 at 2:14
  • @JacobPeattie First of all Thank you for commenting, Yes you are right I'm displaying the title in the template but I want to show the dynamic value which is coming from $item->title, But I don't know how to use it inside wp-filter for dynamic value.
    – SaMee Ch
    Jun 18, 2023 at 11:10

1 Answer 1

0

You can make your life a little easier by moving the details fetching to an earlier point in the WordPress loading sequence and wrapping the data in a class. The object initialized from the class can then be used to provide the data to WordPress and to your template with filters.

For example you could have a handler class akin to this.

// Separate file or in functions.php
class My_Product_Details_Handler
{
    protected $product = null;

    public function __construct(int $product_id)
    {
        $this->setupProduct($product_id);
    }

    protected function setupProduct(int $product_id): void
    {
        $rss = simplexml_load_file('https://domain.com/xml');

        foreach ($rss->channel as $channel) {
            foreach ($channel->item as $item) {
                if ($item->id !== $product_id) {
                    continue;
                }

                $this->product = $item;
                break 2; // break both foreach loops
            }
        }
    }

    public function filter_wp_title(string $title, string $sep, string $seplocation): string
    {
        return $this->product ? $this->product->title : $title;
    }

    public function render_product_title(): void
    {
        echo $this->product ? $this->product->title : '';
    }

    public function provide_product_details()
    {
        return $this->product;
    }
}

An object could be created from the class for example on the init action. template_redirect could work too for providing the data to the front end.

// functions.php
add_action('init', 'init_my_product_details_handler');
function init_my_product_details_handler(): void {
    // Are we on the right template?
    if (! is_page_template('product-details.php')) {
        return;
    }
    
    // Get id from the url
    $product_id = !empty($_GET['id']) && is_numeric($_GET['id'])
        ? (int) $_GET['id']
        : 0;

    // Do nothing, if there's no ID
    if (! $product_id) {
        return;
    }

    // Init handler
    $handler = new My_Product_Details_Handler($product_id);

    // Attach handler to relevant hooks
    add_filter( 'wp_title', [$handler, 'filter_wp_title'], 10, 3 );
    
    add_filter( 'my_product_details_data', [$handler, 'provide_product_details'] );
}

Then a small tweak to the template file. Using a filter to get the product data makes the template file a little cleaner looking.

// product-details.php template
<body>
    <?php
        $product_details = apply_filters( 'my_product_details_data', null );
        if ($product_details) {
            // render the details
        } else {
            echo 'No results found;
        }
    ?>
</body>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.