Function 1:
Depending on the blog-URL I define multiple variables (i.e. customer-group IDs).
Some of the varaibles are integers and some are arrays.
Function I – blog URL:
The variables change, depending on the blog URL. Function could look like this:
add_action( 'after_setup_theme', 'my_site_url' );
function my_site_url(){
$site_url = get_bloginfo('wpurl');
$stage_url = 'https://los.examplos.mx';
if ($site_url == $stage_url) {
/* - customer group ids for use in functions */
$wiederverkaeufer = 1234;
/* Versteckte Kategorien */
$hidecategory_wvk = [24, 863, 51, 87];
$return_array = [$wiederverkaeufer, $hidecategory_wvk];
} else {
/* - customer group ids for use in functions */
$wiederverkaeufer = 5678;
/* Versteckte Kategorien */
$hidecategory_wvk = [51, 48, 42];
$return_array = [$wiederverkaeufer, $hidecategory_wvk];
}
return $return_array;
}
Function II
Now I want to access those variables with some of the other functions within my functions.php. (This worked with constants before). For example
add_action( 'woocommerce_before_calculate_totals', 'cart_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
$my_ids = my_site_url();
$my_ids[0] = $wiederverkaeufer;
// Customer group current user
$group_id = BM_Conditionals::get_validated_customer_group();
// if is group "wiederverkaeufer" return
if ( $wiederverkaeufer == $group_id )
return;
//Do something
}
Function III: Test function:
I built a little test-function to check if I can call multiple variables and arrays from one function.
function my_shop_meldung(){
$a = "Moin";
$b = "Servus";
$c = "Hallo";
$d = ["22", "44", "55"];
$return_array = [$a, $b, $c, $d];
return $return_array;
}
function add_custom_text() {
?>
<div id="my_shopmeldung">
<?php
$my_results = my_shop_meldung();
$a = $my_results[0];
$b = $my_results[1];
$c = $my_results[2];
$d = $my_results[3][1];
//echo $a . $b . $c . $d;
echo $d;
?>
</div>
<?php
}
add_action('kt_beforeheader', 'add_custom_text', 10, 0);
I think this works as I could see the output “44” from the array.
Before I go on from here: is this a good method if I want to use multiple variables within different functions depending on my blog-URL? I don’t want to use globals and constants don’t seem to work anymore in PHP-8 like they did in 7.2.