Your Cart

Call us toll free: +1 789 2000

Free worldwide shipping on all orders over $50.00

如何将Slack提醒通知与WordPress集成

函数get_daycount_of_last_blog_posted () {$ args =数组 (         ‘ post_type ‘ = > ‘ ‘,         ‘ post_status ‘ = > ‘发布 ‘,         ‘ posts_per_page ‘ = > 1 ,            );
美元datediff = 0;$blog_posts = new WP_Query($args);If ($blog_posts->have_posts()):虽然(blog_posts美元——> have_posts ()): $ blog_posts – > the_post ();$datediff = time() – strtotime(get_the_date());endwhile ;         wp_reset_postdata ();endif;
if ($datediff)返回($datediff / (60 * 60 * 24));
返回datediff美元;}这个代码和方法将从WordPress数据库中获取最后添加的文章,并计算从您请求它到最后一篇文章发布之间的天数。WordPress提供了一个函数wp_remote_post()来执行HTTP请求并接收响应。使用此方法,我们将向webhook发送自定义消息。在WordPress项目的根目录下创建一个文件notification.php,并将下面的代码添加到其中。

5) {
$msg = ‘嘿,团队,我们的应用程序发现我们没有添加新的文章在UpdraftPlus超过5天。请调查一下。”
/ /准备提交数据/载荷松弛$ data =数组 (         ‘ 有效载荷’ = > json_encode(数组 (                 ” 文本”= > $味精 ,             )         )     );
/ / Post数据通过松弛webhook端点使用wp_remote_post wp_remote_post (slack_webhook_url美元,数组 (             ‘ ‘ = > ‘ POST方法 ‘,             ‘ 身体’ = >元数据 ,         )     );}

这个代码将张贴一个消息到Webhook URL如果一个新的文章没有添加超过5天在您的博客。结果,你会在选定的Slack频道上收到通知。在您的服务器上设置cron我们的目标是创建一个自动化系统,它将在特定的Slack频道上通知团队这个任务。因此,我们在WordPress项目的根文件夹中创建了一个PHP文件。通过在服务器上使用该文件设置cron,您可以设置Slack应用程序自动运行。

在服务器上设置cron的方法可能因主机提供程序的不同而有所不同。在本教程中,我们将举例说明如何在Media Temple web托管的CPanel上设置cron。

登录您的CPanel。在“高级”部分下选择“Cron Jobs”。

在下一页中,从” Common Settings “下拉菜单中选择” Once Per Day(0 0 * * *) “选项。这意味着cron每天执行一次脚本。

最后,在Command字段中通过调整notification.php文件路径添加以下语句。

前/ usr /地方/ bin / php /home/xyz/public_html / notification.php 之前

就是这样!通过学习本教程,您现在可以为您和您的团队创建自己的Slack应用程序。这当然会帮助每个人跟上博客的进度,并确保你不会错过任何重要的约会。

Free Worldwide shipping

On all orders above $50

Easy 30 days returns

30 days money back guarantee

International Warranty

Offered in the country of usage

100% Secure Checkout

PayPal / MasterCard / Visa