【问题标题】:Passing variables from a function to another in WordPress在 WordPress 中将变量从函数传递到另一个函数
【发布时间】:2016-10-27 14:19:06
【问题描述】:

我在将一些变量从一个函数传递到另一个函数时遇到困难。

我试图让它们全球化,但收效甚微。

我要传递的变量是从send_email_notificationssend_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');

【问题讨论】:

    标签: php variables scope global-variables


    【解决方案1】:

    看来您正在使用 wordpress。 https://wordpress.stackexchange.com/ 可能会更好地回答您的问题。

    您应该使用对象而不是函数作为 add_action 的可调用参数。您的对象可以包含您的全局变量。

    例如,您可以创建一个名为 EmailNotification 的 php 类,它可以有两个函数 send_email_notification_function 和 send_email_notifications。这个类可以有两个属性,称为 $notificationTitle 和 $notificationPermalink。

    然后您可以创建此类的一个实例并将其用作 add_action 的第二个参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      相关资源
      最近更新 更多