• Is it possible to set automatically set the time to 8am on a new post in the Block editor using PHP and a action hook? I write batches of scheduled posts and want each to have the publication time of 8am. That way, all I need to do is set the day and save the draft.

    I tried jQuery in the Block editor https://www.ads-software.com/support/topic/using-jquery-with-the-block-editor/ But there are problems interacting with React, detecting when the new post has post-new.php in the URL, and setting a cookie to prevent the time changer from running more than once.

    I suspect a function would use https://developer.www.ads-software.com/reference/hooks/wp_insert_post/ to set the time and AM/PM, and to fire only when a new post is created.

    Is there an action hook for the time and the AM/PM selector in the Block Editor?

    I’d appreciate any ideas.

    Or would I need to update the database column to set the publication time?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t know about doing so within the block editor, but you can alter post data after submission but before it’s added to the DB via the “wp_insert_post_data” filter using PHP. Your filter callback is passed an array of all relevant post data. You can set the date fields to anything you like before returning the modified array. There are 4 interrelated date/time fields: post_date, post_modified, and their GMT variants. Date/time values need to conform to this format: 2024-11-05 14:21:35 Times are 24 hour style, not am/pm. The modified times will appear after the editor page reloads.

    Thread Starter bluedogranch

    (@bluedogranch)

    Thanks, wp_insert_post_data looks like what I should use. The notes say that it also runs on post update; I wonder if there’s way to only have it run once?

    And “The modified times will appear after the editor page reloads.” But the page doesn’t reload in a traditional sense, as the Block editor uses Ajax, and also a popup for the date and time scheduler.

    Moderator bcworkz

    (@bcworkz)

    Compare post_date and and post_modified fields. If they are the same, it’s newly published, otherwise it’s an update.

    But the page doesn’t reload in a traditional sense

    Indeed, which is why I mentioned it. If it’s important that the date display correctly in the current screen, you’ll need a JS based solution. Integrating with Gutenberg could prove to be difficult, but it may not be necessary. You could install a listener on the publish button. Its label only says “Publish” for new posts, otherwise it says “Save”. Your script could alter the date/time field before it is sent to the server. Untested concept though.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.