John Davidson

php - Setting object value off class included via template include WP

0 comments
Message:


I'm creating a custom plugin for a wordpress website.
The wp-content/my-plugin/index.php contains multiple includes of files.


Example:


include('lib/WhiseApiSeo.php'); // this is the WhiseApiSeo class

When a visitor opens the website and the query_var estatePurpose is used index.php also includes a view_estates.php page as below


if ( get_query_var( 'estatePurpose' )  ) {
add_filter('template_include', function () {
return plugin_dir_path(__FILE__) . 'view_estates.php';
});
}

Via the the WhiseApiSeo.php code I made a setter for the meta title and meta description.


<?php
class WhiseApiSeo {

public $headTitle;
public $headDescription;


function __construct()
{



}

public function setHeadTitle($Value)
{
$this->headTitle = $Value;
}


public function setHeadDescription($Value)
{
$this->headDescription = $Value;
}


public function setMetaTags()
{

function set_page_title()
{
return $this->headTitle;
}

add_filter('wp_title', 'set_page_title');

function set_page_desc()
{
return $this->headDescription;
}

add_filter('wp_metadesc', 'set_page_desc');

}

}
?>


The view_estates.php contains all the code for displaying the estates content.
In the view_estates.php file I call the WhiseApiSeo class as follow:


$objectWhiseApiSeo = new WhiseApiSeo();
$objectWhiseApiSeo->setHeadTitle('Test Title');
$objectWhiseApiSeo->setHeadDescription('Test description');
$objectWhiseApiSeo->setMetaTags();
echo $objectWhiseApiSeo->headTitle;
echo $objectWhiseApiSeo->headDescription;

It seems like view_estates.php which is loaded via the Wordpress add_filter function, is not setting the title and description or am I doing something totally wrong?
There is no content displaying when I echo the title and description.


Love to hear from you.


Thanks.



My Answer:
It looks like the issue might be related to the scope of the functions set_page_title() and set_page_desc() within the setMetaTags() method of the WhiseApiSeo class.

Since these functions are defined within the setMetaTags() method, they are not accessible outside of that method. Therefore, when you try to use add_filter() to hook them into the wp_title and wp_metadesc filters, they are not being called.

To fix this issue, you can define the set_page_title() and set_page_desc() functions outside of the setMetaTags() method, and then pass the $this object to them as a parameter. This way, they will have access to the $headTitle and $headDescription properties of the WhiseApiSeo class.

Here's an updated version of your WhiseApiSeo class:

php
class WhiseApiSeo {

public $headTitle;
public $headDescription;

function __construct()
{
}

public function setHeadTitle($Value)
{
$this->headTitle = $Value;
}

public function setHeadDescription($Value)
{
$this->headDescription = $Value;
}

public function setMetaTags()
{
add_filter('wp_title', array($this, 'set_page_title'));
add_filter('wp_metadesc', array($this, 'set_page_desc'));
}

public function set_page_title($title)
{
return $this->headTitle;
}

public function set_page_desc($desc)
{
return $this->headDescription;
}
}
?>


With this updated code, the set_page_title() and set_page_desc() functions are now defined as public methods of the WhiseApiSeo class, and they can access the $headTitle and $headDescription properties. When you call setMetaTags() in your view_estates.php file, the filters should now be properly hooked up and the title and description should be set correctly.

I hope this helps! Let me know if you have any other questions.

Rate this post

3 of 5 based on 1645 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us