2016-10-27 17 views
0

ある関数から別の関数にいくつかの変数を渡すのが難しいです。WordPressである関数から別の関数に変数を渡す

私はほとんど成功していないそれらをグローバルにしようとしました。

私が渡したい変数は、send_email_notificationsからsend_email_notification_functionのものです。

function send_email_notifications($ID, $post) { 
    global $notificationTitle; 
    global $notificationPermalink; 

    $notificationTitle = $post->post_title; 
    $notificationPermalink = get_permalink($ID); 

    if(isset($_REQUEST['send_email_notification'])) { 
     date_default_timezone_set('Europe/London'); 
     wp_schedule_single_event(time() + 10, 'send_email_notification_function_execute'); 
    } 
} 
add_action('publish_Test_notifications', 'send_email_notifications', 10, 2); 

function send_email_notification_function() { 
    global $notificationTitle; 
    global $notificationPermalink; 

    echo $notificationTitle; 
    echo $notificationPermalink; 

    $notificationEmail = '[email protected]'; 

    wp_mail($notificationEmail, $notificationSubject, $notificationContent); 
} 
add_action('send_email_notification_function_execute', 'send_email_notification_function'); 

答えて

0

wordpressを使用しているようです。あなたの質問は、https://wordpress.stackexchange.com/にもっとよく答えるかもしれません。

add_actionの呼び出し可能パラメータには、関数ではなくオブジェクトを使用する必要があります。オブジェクトにグローバル変数を含めることができます。

たとえば、EmailNotificationという名前のPHPクラスを作成し、send_email_notification_functionとsend_email_notificationsの2つの関数を持つことができます。このクラスは$ notificationTitleと$ notificationPermalinkの2つのプロパティを持つことができます。

このクラスのインスタンスを作成し、add_actionの2番目の引数として使用できます。

関連する問題