How to check for duplicate record before saving a Custom Post Type

See this answer for solution, see comments there about error messages. That should work with Classic editor. It could work with Gutenberg too, but it would need additional effort to get correct error messages. UPDATE: If using classic editor and preventing publishing of invalid records is enough, you can use this code: function my_save_post($post_id) { … Read more

Copy permalink to clipboard automatically when publish/update posts?

Finally, I did it! Use the post_updated_messages hook instead. add_filter( ‘post_updated_messages’, function( $messages ) { // Get the permalink $link = esc_url( get_permalink($post_ID) ); // Copy the permalink automatically $autocopy = ‘<script type=”text/javascript”>navigator.clipboard.writeText(“https://wordpress.stackexchange.com/questions/367921/%s”);</script>’; // `6` is for publishing $messages[‘post’][6] = sprintf( __(‘Post published. <a href=”https://wordpress.stackexchange.com/questions/367921/%s”>View post</a>’. $autocopy), $link, $link); // `1` is for updating $messages[‘post’][1] … Read more

Saving multiple fields (dropdown and text) in custom metabox

It looks like the variable you’re using to set the selected item is not defined in that scope. In your constructor: public function __construct() { if ( is_admin() ) { add_action( ‘load-post.php’, array( $this, ‘init_metabox’ ) ); add_action( ‘load-post-new.php’, array( $this, ‘init_metabox’ ) ); } $this->dropdown_args = [ ‘show_option_none’ => ‘- select a page -‘, … Read more

Execute php after post save/update

I believe you should look into status transitions: https://developer.wordpress.org/reference/hooks/transition_post_status/ There are a couple of examples below the explanation on top of the page. Here’s also some example code of mine: add_action( ‘transition_post_status’, ‘nxt_create_news’, 10, 3 ); // Automatically create a news when a wiki post has been published function nxt_create_news($new_status, $old_status, $nxt_wiki_post) { if(‘publish’ === … Read more