[英]WordPress do_action and add_action; running arguments in add_action call
[英]pass variables of add_action" to do_action - Wordpress
我有這個 add_action function,它通過以下方式創建一個數組。
add_action( 'mixedcart', 'checkoutincluyenevio', 1, 2 );
function checkoutincluyenevio() {
$mensaje['label'] = "Incluye Envío";
$mensaje['price'] = null;
$mensaje['style'] = "background-color:#e4f4fd;font-weight:initial;";
$mensaje['class'] = "mixedcar";
return $mensaje;
}
此代碼在我的主題的 functions.php 文件中。
在我的主題模板中,我想通過這樣做來檢索值。
$checkoutincluyenevio = do_action( 'mixedcart' );
結果我得到變量 $checkoutincluyenevio 為 NULL
預期的結果將是變量$mensaje
及其所有內容。
我究竟做錯了什么?
我發現變量的答案更好地使用過濾器 apply_filters 和 add_filter
它會保持這種狀態
add_filter( 'mixedcart', 'checkoutincluyenevio', 10, 2 );
function checkoutincluyenevio($mensaje) {
$mensaje['label'] = "Incluye Envío";
$mensaje['price'] = null;
$mensaje['style'] = "background-color:#e4f4fd;font-weight:initial;";
$mensaje['class'] = "mixedcar";
return $mensaje;
}
在模板中
$checkoutincluyenevio = apply_filters( 'mixedcart', $mensaje );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.