The function called on the wp head hook becomes null

Question

Sample code:

function test_loaded() {

require_once 'file.php';
$ins = Sample_Class::instance();

require_once 'file2.php';

}

add_action('plugins_loaded', 'test_loaded');

in file.php

class Sample_Class {

public static $instance = null;

public static function instance() {
if(null == self::$instance) {
self::$instance = new Sample_Class();
}
return self::$instance;
}

public static function sample($a1) {

$a2 = get_option($a1);

return $a2;
}

}

in file2.php

$a3 = Sample_Class::instance();

function sample2() {
global $a3;
echo $a3->sample('test');
}

add_action('wp_head', 'sample2');

function sample3() {
global $a3;
echo $a3->sample('test2');
}

add_action('wp_footer', 'sample3');

Problem:

Sample2 expected output head section: get_option(‘test’) -> this value
But $a3 is output null (i use var_dump($a3) in function)
No error appears on sample3 function side and outputs $ a3 is object. (i use var_dump())

Anyone have any ideas? Why am I having trouble using the wp_head hook.

My workaround is:
To redefine $a3 inside the sample2 function instead of using global. It works this way. But I don’t know why I should use it like that.

0
Akin Ocak 4 months 0 Answers 23 views 0

Leave an answer