简体   繁体   中英

Passing variables from a function to another in WordPress

I'm having difficulty passing some variables from one function to another.

I've tried to make them global with little success.

The variables I would like to pass are the ones from send_email_notifications to 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 = 'test@test.com';

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

It seems you are using wordpress. Your question might be better answered on https://wordpress.stackexchange.com/ .

You should use an object instead of a function for the callable parameter of add_action. Your object can contain your global variables.

For example you can create a php class called EmailNotification and it can have two functions send_email_notification_function and send_email_notifications. This class can have two properties called $notificationTitle and $notificationPermalink.

You can then create an instance of this class and use it as the second argument to add_action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM