【问题标题】:Execute if status is changed wordpress如果状态改变,则执行 wordpress
【发布时间】:2021-08-05 08:03:09
【问题描述】:

我正在使用motors 主题。我有一个 viber 机器人,如果所选汽车的状态更改为 is-exist,我想发送一条消息。

状态变量:

$status = get_post_meta( get_the_ID(), 'status', true );
$status = urldecode($status);

如果状态更改为 is-exist 应执行的代码:

$client->sendMessage(
    ( new \Viber\Api\Message\Text() )
        ->setSender( $botSender )
        ->setReceiver( $receiverId )
        ->setText( 'New Renault Megane with status "Is exist") );

【问题讨论】:

    标签: php wordpress api bots chatbot


    【解决方案1】:

    您可以使用update_postmeta 挂钩来检查后元是否发生更改。这是钩子的样子(取自meta.php):

    /**
     * Fires immediately before updating a post's metadata.
     *
     * @since 2.9.0
     *
     * @param int    $meta_id    ID of metadata entry to update.
     * @param int    $object_id  Post ID.
     * @param string $meta_key   Metadata key.
     * @param mixed  $meta_value Metadata value. This will be a PHP-serialized string representation of the value
     *                           if the value is an array, an object, or itself a PHP-serialized string.
     */
    do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
    

    所以是这样的:

    add_action('update_postmeta', function($meta_id, $object_id, $meta_key, $meta_value) {
      if( $meta_key === 'status' && $meta_value === 'is-exist' {
        //... send notification
      }
    }, 10, 4);
    

    【讨论】:

    • 您好,感谢您的回答。对不起,我的延迟回复。我应该为雷诺品牌做这个。品牌在 DB 中是 make。你为什么不使用这样的状态:`$status = get_post_meta( get_the_ID(), 'status', true ); $status = urldecode($status); if ($status==='on-road')???
    猜你喜欢
    • 1970-01-01
    • 2015-05-30
    • 2019-06-11
    • 2019-03-08
    • 1970-01-01
    • 2018-11-22
    • 2014-07-07
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多