根据WordPress blog-URL,我需要定义多个变量(即客户组id)。有些变量是整数,有些是数组。我想在functions.php中的不同函数中使用这些变量。
我需要知道:是我的解决方案好方法,如果我想使用不同的函数内的多个变量取决于blog-URL?我不想再使用全局变量和常量,因为它们在PHP-8中似乎不再像在7.2中那样工作了。
功能一-博客URL:
变量根据博客URL的不同而变化。函数可以像这样:
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;
}
函数二
现在我想用functions.php中的其他函数访问这些变量。(这以前对常量是有效的)。例如
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 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);
我认为这是有效的,因为我可以看到输出"44">
适合我:
此函数显示WordPress Woocommerce中的产品类别,具体取决于"marketpress"。客户群体。请注意:只有当你使用插件Marketpress时,这个例子才会起作用。
功能1:
//add_action( 'wp_enqueue_scripts', 'my_site_url' );
add_action( 'after_setup_theme', 'my_site_url' );
function my_site_url(){
$site_url = get_bloginfo('wpurl');
//$site_url = get_home_url();
$stage_url = 'https://los.examplos.dk';
$stage_url_2 = 'https://los.examplos.dk';
//global $wiederverkaeufer, $hidecategory_wvk, $hidecategory_customer,
$boxkategorien, $gast_id, $kunde_id;
if ($site_url == $stage_url || $site_url == $stage_url_2 ) {
/* CONSTANTS - customer group ids for use in functions */
$wiederverkaeufer = 11111;
$gast_id = 22222;
$kunde_id = 33333;
/* Versteckte Kategorien */
$hidecategory_wvk = [11, 22, 33, 44];
$hidecategory_customer = [11, 22, 33, 44];
/* BOXEN */
$boxkategorien = = [11, 22, 33, 44];
} else {
/* CONSTANTS - customer group ids for use in functions */
$wiederverkaeufer = 3333;
$gast_id = 4444;
$kunde_id = 5555;
/* Versteckte Kategorien */
$hidecategory_wvk = = [11, 22, 33, 44];
$hidecategory_customer = = [11, 22, 33, 44];
/* BOXEN */
$box_kategorien = = [11, 22, 33, 44];];
}
$return_array = [
$wiederverkaeufer,
$gast_id,
$kunde_id,
$hidecategory_wvk,
$hidecategory_customer,
$boxkategorien
];
return $return_array;
}
函数二隐藏Woocommerce产品类别取决于MarketPress客户群体。$group_id = BM_Conditionals::get_validated_customer_group();">
add_filter( 'get_terms', 'exclude_cats_from_shop', 10, 3 );
function exclude_cats_from_shop($terms, $taxonomies, $args ){
if ( is_admin() && ! defined('DOING_AJAX') )
return $terms;
$group_id = BM_Conditionals::get_validated_customer_group();
//get the customer group ID of current user
$customer_group = my_site_url();
//call function my_site_url(), get the array, and save to $customer_group
$wiederverkaeufer = $customer_group[0];
//save customer group id to $wiederverkaeufer
$hidecategory_wvk = $customer_group[3];
$hidecategory_customer = $customer_group[4];
//get those product group ids
if ( $wiederverkaeufer == $group_id ){
//if wiederverkaeufer
$new_terms = array();
//$hide_category_wvk = HIDECATEGORY_WVK;
// Ids der Produktkategorien die ausgeschlossen werden sollen
if ( in_array( 'product_cat', $taxonomies ) && is_shop() || is_product() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->term_id, $hidecategory_wvk ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}// Ende ist Wiederverkaeufer
// if is customer group customer
else {
$new_terms = array();
// hide productcategories
if ( in_array( 'product_cat', $taxonomies ) && is_shop() || is_product() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->term_id, $hidecategory_customer ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
} //End