$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Check if post is added manually or through wp_insert_post()|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Check if post is added manually or through wp_insert_post()

matteradmin8PV0评论

I might not be thinking this right but here is my situation:

  • I have a custom post type called events.
  • Some of my events are imported from an API and inserted through wp_insert_post() (in a plugin), some are added manually in the admin edit screen.
  • On post saving I need to add some informations to the post_meta but only if the post is added manually (through the admin edit screen).

So I am hooking into save_post_events and performing some security checks but I have no idea how to check if the request is only coming from the admin edit page because right now the save_post action triggers also if the post is added with the wp_insert_post function.

Any ideas?

Thanks

I might not be thinking this right but here is my situation:

  • I have a custom post type called events.
  • Some of my events are imported from an API and inserted through wp_insert_post() (in a plugin), some are added manually in the admin edit screen.
  • On post saving I need to add some informations to the post_meta but only if the post is added manually (through the admin edit screen).

So I am hooking into save_post_events and performing some security checks but I have no idea how to check if the request is only coming from the admin edit page because right now the save_post action triggers also if the post is added with the wp_insert_post function.

Any ideas?

Thanks

Share Improve this question asked Jan 29, 2019 at 16:52 PipoPipo 6092 gold badges11 silver badges27 bronze badges 1
  • You can add a small metabox with checkbox file like Is it Adding manually? when you will add a event from Dashboard, you will check this checkbox. Next you will use get_post_meta function and get this checkbox value. if it is on then you get the post ID, otherwise you will leave it. – Chinmoy Kumar Paul Commented Jan 29, 2019 at 17:02
Add a comment  | 

1 Answer 1

Reset to default 0

I have found a solution without adding any other metaboxes. I haven't thought about it but the import function in my plugin uses a nonce so I just check if my nonce is set or not.

Here is my save_post_events function:

add_action( 'save_post_events', 'wse_327066_example', 10, 3);
function wse_327066_example( $post_id, $post, $update ) {
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || $post->post_status == 'trash' || !current_user_can( 'edit_posts' ) || isset( $_POST['_import_nonce'] ) ) 
    return;

  # Now I can do whatever I want with posts saved from the admin edit screen... 
}
Post a comment

comment list (0)

  1. No comments so far