如何在Wordpress中访问另一个函数和插件中的变量

时间:2019-06-05 14:27:23

标签: php wordpress woocommerce

我正在尝试从另一个函数访问变量,但没有任何结果。

第一个功能是:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
global $my_field_name;
$my_field_name = get_post_meta( $order->get_id(), 'my_field_name', true );
if( $my_field_name == 1 )
    echo '<p><strong>Фирменный пакет: </strong> <span style="color:red;">Добавить</span></p>';
    global $form_name;
$form_name = "Test form 1";
echo $form_name;
}

我正尝试在此处访问 form_name

if( function_exists( 'YITH_PDF_Invoice' ) ){
if( ! function_exists( 'yith_ywpi_print_document_notes' ) ){
    function yith_ywpi_print_document_notes( $notes, $document ){
        if( is_object( $document ) && ! empty( $document->order ) && $document->order instanceof WC_Order){
            /**
             * @var $order WC_Order
             */
            $order = $document->order;
            $shipping_info = $form_name;
            $notes .= $shipping_info;
           }
        return $notes;

    }
}
add_filter( 'yith_ywpi_print_document_notes', 'yith_ywpi_print_document_notes', 10, 2 );

还有其他方法可以声明全局变量吗?还是通过其他插件访问$ form_name的另一种方式?

1 个答案:

答案 0 :(得分:0)

您可以这样设置全局变量:

$GLOBALS['foo'] = 'bar'

然后您应该能够从其他函数中访问该变量,例如:

$foo = $GLOBALS['foo']

详细了解PHP here

中的全局变量