Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPress Cronジョブの表示とコントロール方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

最近、ある読者からWordPressのcron jobシステムをダッシュボードで表示、コントロールできないかとの質問がありました。

CronはWebサーバー上でスケジュールされたタスクを実行する技術です。WordPressにはビルトインcronが搭載されており、更新のチェックやスケジュールされた投稿の公開など、スケジュールされたタスクを実行することができます。

この投稿では、WordPressのcronジョブを表示し、コントロールする方法を紹介します。

WordPress Cronとは?どのように動作しますか?

Cron(クーロン)とは、スケジュールされた時間や定期的な間隔で実行されるコマンドに使われる専門キーワードです。ほとんどのウェブサーバーでは、サーバーのメンテナンスとスケジュールされたタスクの実行に使用されます。

WordPressには独自のcronシステムがあり、スケジュールされたタスクを実行することができます。例えば、更新のチェック、ゴミ箱内の古いコメントの削除、スケジュールされた投稿の公開などです。

WordPressのプラグインは、あなたが指定したタスクを実行するためにこれを使用することもできます。例えば、WordPressバックアッププラグインはWordPress cronを使用して、指定されたスケジュールで自動的にバックアップを作成することができます。

しかし、プラグインによるWordPress cronの無責任な使用は、サイトの速度を低下させる可能性があります。特に、共有ホスティングサービスを使用している場合。

プラグインが頻繁にリソースを消費するタスクを実行する場合は、問題を特定して修正する必要があります。

コードを書かずにWordPressのcronシステムを表示しコントロールする方法を見てみましょう。

WordPress Cronシステムの表示とコントロール

最初に必要なことは、WP Crontrolプラグインをインストールして有効化することです。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

有効化した後、Tools ” Cron Eventsページでクーロン設定をコントロールする必要があります。

WordPress Cron events

WordPressのcronシステムを使用してサイトで実行するようにスケジュールされたすべてのcronイベントのリストが表示されます。

最初のカラムには、cronを実行するフックの名前が表示されます。

フック名は通常、この特定のイベントが何をするのかのヒントになる。

WordPressの初期設定のフックのほとんどは、wp_update_pluginsやwp_update_themesなどのように、wp_プレフィックスで始まります。

WordPressのプラグインは、フックに独自の接頭辞を使う場合と使わない場合があります。例えば、All In One SEOは aioseo_接頭辞を使っています。

また、cronが次にいつ実行されるか、そして次に実行されるまでの時間間隔も表示されます。

リストの最後のカラムでは、クーロンイベントの編集、削除、実行ができます。

重要:クーロンイベントの使用には十分注意し、WordPressの初期設定クーロンイベントは決して削除しないでください。

ここで、WordPressプラグインによって作成されたcronイベントがかなりリソースを消費するとします。

まず、プラグインの設定をチェックして、そこからコントロールするオプションがあるかどうか確認してください。もしなければ、cronイベントの横にある’Edit’リンクをクリックして変更することができます。

Editing a cron in WordPress

編集ボタンをクリックすると、下にある’クーロンイベントの変更’タブが開きます。

ここでは、イベントを実行する頻度を変更できます。

Modifying cron settings

設定が完了したら、’Modify Cron Event’ボタンをクリックして設定を保存してください。

WordPressに独自のCronイベントを追加する

WP Crontrolプラグインを使用すると、WordPressに独自のクーロンジョブを簡単に追加することができます。Tools ” Cron Eventsページにアクセスし、’Add Cron Event’ タブにスクロールダウンするだけです。

Add custom cron event in WordPress

最初に、cronイベントのフック名をプロバイダーする必要があります。フック名にはスペースや特殊文字は使用できません。

実行したい関数が引数を必須としている場合は、その引数を指定することができる。

次に、WordPressに次にいつcronを実行するかを指示する必要がある。now’を入力すれば即座にcronが起動し、’tomorrow’、’+2 days’、または’25-02-2020 12:34:00’を入力することができます。

最後に、スケジュールを選択する必要があります。1時間ごと、1日2回、毎日、週1回を選択できる。繰り返さないイベントにすることもできます。

完了したら、’Add Cron Event’ボタンをクリックして変更を保存してください。

イベントリストにあなたのcronイベントが表示されます。

しかし、このイベントがトリガーされたときに何をすべきかをWordPressに指示していないため、現在は何もしません。

フックとcronフックがトリガーされた時に実行される関数を追加する必要があります。

add_action( 'wpb_custom_cron', 'wpb_custom_cron_func' );

function wpb_custom_cron_func() {
  wp_mail( 'you@example.com', 'Automatic email', 'Automatic scheduled email from WordPress to test cron');
}

自分のメールアドレスを使うことをお忘れなく。

この機能はクーロンが実行された時にテストメールを送信します。ページをスクロールし、cronイベントの横にある’Run Now’リンクをクリックしてテストすることができます。

注意:cronを使用するには、中級レベルのプログラミングとWordPress開発者のスキルが必須です。これらのスキルをお持ちでない場合は、WordPress開発者に依頼することをお勧めします。

以上、この投稿がWordPressのcronジョブの表示とコントロール方法を学ぶのに役立てば幸いです。また、WordPressを高速化し、パフォーマンスを向上させるための究極のガイド、またはWordPressセキュリティプラグインのエキスパートピックもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

20件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Paul Barrett says

    We run cron at server level because wp-cron overloads teh server. There’s is nothing on our site that is so time-critical the jobs need to running frequently. If we set the frequency to say daily, would WordPress have a problem?

    • WPBeginner Support says

      It would heavily depend on what is being done on the site and when. We do not have a specific recommendation for what can be delayed to that extent.

      管理者

  3. Matt K says

    When I go to change the the recurrence on the Hooks, they just get changed back. Something isn’t right. Do I need to disable WP crons in wp-config.php first?

    • WPBeginner Support says

      It would depend on the specific chron job, if you reach out to the plugin’s support they should be able to assist

      管理者

  4. Heinz says

    Can I deactivate or even delete WP Control plugin in after having set a cron job, so I don’t have too many plugins running and slow down the website?

  5. Andre Hesdahl says

    I am a newbee with WordPress and don’t know where to put (or how to execute) the code:
    add_action( ‘wpb_custom_cron’, ‘wpb_custom_cron_func’ );

    function wpb_custom_cron_func() {
    wp_mail( ‘you@example.com’, ‘Automatic email’, ‘Automatic scheduled email from WordPress to test cron’);
    }

  6. Jeff says

    Thanks for the article. Interesting to see the number of plugins that have cron jobs running often. Great peak under the hood plugin…

  7. hazel beaver says

    When I check the cron schedule the dates shown in the ‘next run’ column are all in the past. Does that mean they are not running?

    In the past I think I added ‘true’ in the wp config file to stop automatic wp updates from breaking my site…. could this be the cause?

  8. Oleg says

    Great article, but no need for

    if ( ! wp_next_scheduled( ‘wpb_custom_cron’ ) ) {
    wp_schedule_event( time(), ‘hourly’, ‘my_task_hook’ );
    }

    this part is done in the plugin,

  9. Ahmad Sayeed says

    I tested you given example. It works like a superman. But please give us an example to send parameter. I have added the function while creating the even. But I don’t know how to use them on PHP function.

    Please give us an example

  10. Sunny says

    My website is coming up with a long wp cron string after my URL and I cant get rid of it. Any thoughts?

    ?doing_wp_cron=1499261576.8675799369812011718750 comes up after my url and its making me look really spammy. Your insight would be appreciated.

  11. Aniket Ashtikar says

    Is that ‘my_task_hook’ cron hook rightly mentioned there at line 2 or should that rather be changed to ‘wpb_custom_cron’?

  12. Matt says

    Do you know if its possible to visit a link in the wp-admin area? I have a plugin that adds admin features to my wordpress site but I really want this scheduled to happen every 24 hours. I have the link but obviously am required to be logged in.

  13. Pam M says

    Do you need that first if statement shown in the sample code? It looks like it would override the ability to schedule and unschedule in the WP Crontrol plugin.
    Your example worked nicely when I scheduled the job in the plugin and tested it with the Run Now option. I’ll see if I get an email at the scheduled time too.
    Thanks for the helpful background info.

  14. Nigam says

    Hi

    When I am going to refresh the page of WP-Cron Events from wordpress admin then only run the event.

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。