当新图片上传到WordPress时运行图片编辑算法的钩子

bsxbgnwa  于 2023-05-28  发布在  WordPress
关注(0)|答案(1)|浏览(161)

我正试图实现一个功能,在WordPress的图像编辑算法自动执行每次新的图像上传。我想调用另一个服务器来处理编辑,但我不确定使用哪个钩子来触发此操作。
我尝试使用以下代码:

add_action('add_attachment', 'send_image_to_remote_server');

然而,它似乎并没有像预期的那样工作。
有人能建议正确的钩子来调用我的服务器并发送新上传的图像的API地址进行编辑吗?
提前感谢您提供的任何帮助。

pvabu6sv

pvabu6sv1#

如果你想触发一个动作,并将新上传的图片的API地址发送到远程服务器进行编辑,你可以使用WordPress中的add_attachment钩子。但是,add_attachment钩子在附件元数据保存到数据库之前触发,因此此时API地址可能不可用。
为了确保API地址在钩子被触发时可用,可以使用wp_insert_attachment钩子。在将附件元数据保存到数据库后,将触发此挂接。下面是如何修改代码的示例:

add_action('wp_insert_attachment', 'send_image_to_remote_server', 10, 3);

function send_image_to_remote_server($attachment_id, $post_data, $update) {
    // Get the API address of the uploaded image
    $api_address = ''; // Replace with your logic to retrieve the API address

    // Make sure the API address is available
    if (!empty($api_address)) {
        // Call your remote server and send the API address
        // Replace this with your code to send the API address to the remote server
    }
}

send_image_to_remote_server函数中,您可以使用自己的逻辑检索上传图像的API地址。获得API地址后,可以继续将其发送到远程服务器进行编辑。
请确保将占位符''替换为您的逻辑,以检索映像的实际API地址。此外,用您自己的代码替换注解,将API地址发送到远程服务器进行编辑。
通过使用wp_insert_attachment钩子,您可以确保API地址可用,并且在将附件元数据保存到数据库后,可以将其发送到远程服务器。

相关问题