0

I followed instruction by @pat-j and @cjbj in this answer: Dynamically Override Fancy Title and I got the result of override title. Now I need to dynamically change this title. I have written this function:

add_filter ('presscore_get_page_title','netbooking_presscore_get_page_title',10,1);
function netbooking_presscore_get_page_title ( $title ) {

if(  is_page( '1057' ) ){

    $GLOBALS["content"] = $content;

// This reads input considering a default value in case of error

$INPUT = function($param, $default, $prefix = true){
            $key = $prefix ? 'netbooking_structure_'.$param : $param;
            return isset($GLOBALS["content"][$key]) ? $GLOBALS["content"][$key] : $default;
        };

// This read GET input

$language = $INPUT('language', 'it', false);
        $lang = substr($language, 0, 2);
        $upwd = $INPUT('upwd', '8UDW37tF', false);
        $cryptedID = $INPUT('crypted_id', 'Nel6LjH9xZ4=', false);
        $structure_id = $INPUT('sid', 268, false);
        $accomodation_id = $INPUT('aid', '4894', false);

// This calls and gets the XML with accomodation data

$today = getdate();
        $api = 'http://url';
        $query = http_build_query([
            'get' => 'getaccomodation',
            'upwd' => $upwd,
            'id' => $structure_id,
            'idacc' => $accomodation_id,
            'type' => 'p',
            'mode' => 'xml',
            'ukey' => 'create',
            'l' => $lang,
            'dstart' => $today['mday'],
            'mstart' => $today['mon'],
            'ystart' => $today['year'],
        ]);
        $xml = new SimpleXMLElement(file_get_contents($api. '?' . $query));
        $accomodation = $xml->accomodation;
    // Ottengo l'XML del prezzo della accomodation
    $api = 'http://netbooking.naturalbooking.it/getnbdata.php';
    $query = http_build_query([
        'get' => 'accomodationminprice',
        'upwd' => $upwd,
        'id' => $structure_id,
        'idacc' => $accomodation_id,
        'type' => 'p',
        'mode' => 'xml',
        'ukey' => 'create',
        'l' => $lang,
    ]);
    $priceData = new SimpleXMLElement(file_get_contents($api. '?' . $query));

// Here we get link for current accomodation

$link = 'http://url';
        $query = http_build_query([
            'cmd' => 'bookstep1',
            'id' => $cryptedID,
            'type' => 'p',
            'l' => $lang,
            'idacc' => $accomodation_id
        ]);
        $link = $link. '?' . $query;

// Local settings

setlocale(LC_TIME, $language);

// Get the title

    $accomodation->id;
    if ($accomodation){

          foreach($accomodation as $accomodation) {
         $title = $accomodation->name_struct." ".$accomodation->name;
         }
         }
    }   
  return $title;
         }

It always returns the name of the first accomodation in list instead of the current accomodation name.

EDIT I identified the issue to be inside the get function. but I can't understand why the same inside a page template works, whereas inside the hook it doesn't work. This is the last version of the function:

// ACCOMODATION TITLE
add_filter ('presscore_get_page_title','netbooking_presscore_get_page_title',10,1); // filtro che aggiunge la funzione per sovrascrivere il title
function netbooking_presscore_get_page_title ( $title ) { // title function
if(  is_page( '1057' ) ){ // conditional output

        global $content;
        $GLOBALS["content"] = $content;

        // This reads input considering a default value in case of error

        $INPUT = function($param, $default, $prefix = true){
            $key = $prefix ? 'netbooking_structure_'.$param : $param;
            return isset($GLOBALS["content"][$key]) ? $GLOBALS["content"][$key] : $default;
        };

        // Read GET input
        $language = $INPUT('language', 'it', false);
        $lang = substr($language, 0, 2);
        $upwd = $INPUT('upwd', '8UDW37tF', false);
        $cryptedID = $INPUT('crypted_id', 'Nel6LjH9xZ4=', false);
        $structure_id = $INPUT('sid', 268, false);
        $accomodation_id = $INPUT('aid', '4894', false);

        // Build XML Accomodation
        $today = getdate();
        $api = 'http://myurl.it';
        $query = http_build_query([
            'get' => 'getaccomodation',
            'upwd' => $upwd,
            'id' => $structure_id,
            'idacc' => $accomodation_id,
            'type' => 'p',
            'mode' => 'xml',
            'ukey' => 'create',
            'l' => $lang,
            'dstart' => $today['mday'],
            'mstart' => $today['mon'],
            'ystart' => $today['year'],
        ]);
        $xml = new SimpleXMLElement(file_get_contents($api. '?' . $query));

        // Imposto il locale
        setlocale(LC_TIME, $language);



$nomestruttura=(string)$xml->accomodation->name_struct;
$nome=(string)$xml->accomodation->name;

$title=$nomestruttura." ".$nome;

    }   
  return $title;
         }
1

I finally got it. The final version of the function is:

<?php 

// ACCOMODATION TITLE
add_filter ('presscore_get_page_title','netbooking_presscore_get_page_title',10,1);
function netbooking_presscore_get_page_title ( $title ) {
if(  is_page( '1057' ) ){

        $GLOBALS["content"] = $content;

        // This reads input considering a default value in case of error


$INPUT = function($param, $default, $prefix = true){
                $key = $prefix ? 'netbooking_structure_'.$param : $param;
                return isset($GLOBALS["content"][$key]) ? $GLOBALS["content"][$key] : $default;
            };

        // Read input GET
        $language = $INPUT('language', 'it', false);
        $lang = substr($language, 0, 2);
        $upwd = $INPUT('upwd', '8UDW37tF', false);
        $cryptedID = $INPUT('crypted_id', 'Nel6LjH9xZ4=', false);
        $structure_id = $INPUT('sid', 268, false);
        $accomodation_id = $INPUT('aid', '4894', false);

        // XML accomodation
        $today = getdate();
        $api = 'http://';
        $query = http_build_query([
            'get' => 'getaccomodation',
            'upwd' => $upwd,
            'id' => $structure_id,
            'idacc' => $accomodation_id,
            'type' => 'p',
            'mode' => 'xml',
            'ukey' => 'create',
            'l' => $lang,
            'dstart' => $today['mday'],
            'mstart' => $today['mon'],
            'ystart' => $today['year'],
        ]);
        $xml = new SimpleXMLElement(file_get_contents($api. '?' . $query));



        // Imposto il locale
        setlocale(LC_TIME, $language);

 $url="htp://";

$xml = simplexml_load_file($url);

$nomestruttura=(string)$xml->accomodation->name_struct;
$nome=(string)$xml->accomodation->name;

$title=$nomestruttura." ".$nome;

    }   
  return $title;
         }
         ?>

I understood the mistake is in the function to get the xml. Now I have to solve this. EDIT & SOLVED

    // ACCOMODATION TITLE
add_filter ('presscore_get_page_title','netbooking_presscore_get_page_title',10,1); // filter for title
function netbooking_presscore_get_page_title ( $title ) { // function to override title
if(  is_page( icl_object_id('1057')) ){ // Conditional output with ICL support
// Added Query 

                global $wp_query; 

    // Get Values
    $content = [];
    $content["crypted_id"] =   get_option('netbooking_'.'home'.'_crypted_id_meta');
    $content["upwd"] =         get_option('netbooking_'.'home'.'_upwd_meta');
    $content["structure_id"] = get_option('netbooking_'.'home'.'_structure_id_meta');
    $content["language"] = netbooking_get_language();
    $content["sid"] = isset($_GET['sid']) ? $_GET['sid'] : $wp_query->query_vars['sid'];
    $content["aid"] = isset($_GET['aid']) ? $_GET['aid'] : $wp_query->query_vars['aid'];
    foreach ($GLOBALS['structure'.'-options'] as $key => $option) {
        $content[$key] = get_option($key);
}


        $GLOBALS["content"] = $content;

        // Function to read default values in case of error
        $INPUT = function($param, $default, $prefix = true){
            $key = $prefix ? 'netbooking_structure_'.$param : $param;
            return isset($GLOBALS["content"][$key]) ? $GLOBALS["content"][$key] : $default;
        };

        // GET Input
        $language = $INPUT('language', 'it', false);
        $lang = substr($language, 0, 2);
        $upwd = $INPUT('upwd', '8UDW37tF', false);
        $cryptedID = $INPUT('crypted_id', 'Nel6LjH9xZ4=', false);
        $structure_id = $INPUT('sid', 268, false);
        $accomodation_id = $INPUT('aid', '4894', false);

        // Accomodation XML 
        $today = getdate();
        $api = 'http://myurl.it';
        $query = http_build_query([
            'get' => 'getaccomodation',
            'upwd' => $upwd,
            'id' => $structure_id,
            'idacc' => $accomodation_id,
            'type' => 'p',
            'mode' => 'xml',
            'ukey' => 'create',
            'l' => $lang,
            'dstart' => $today['mday'],
            'mstart' => $today['mon'],
            'ystart' => $today['year'],
        ]);
        $xml = new SimpleXMLElement(file_get_contents($api. '?' . $query));

        // Imposto il locale
        setlocale(LC_TIME, $language);


$nomestruttura=(string)$xml->accomodation->name_struct;
$nome=(string)$xml->accomodation->name;

$title=$nomestruttura." ".$nome;

    }   
  return $title;
         }

This finally Works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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