Undefined ‘post_type’ error on Add new page

save_post is called when post is inserted or updated. When you access add new post a post is created and inserted into database. That is actually draft. At that time $_POST is blank therefore, you are seeing warnings. Solution: The best option is exit the function as soon as you don’t find the nonce. And … Read more

Call add_action() in function wordpress

You’re thinking about this entirely wrong. A meta tag isn’t something you add when a post gets saved, it’s something that gets added to the output when a post is viewed. So instead of trying to hook the action inside save_post, you hook it on every page load, and inside the hook you check if … Read more

Faking the “onSave” event

Have you seen wpshell? It’s a command line tool for wordpress. Basically, it’s a WordPress environment that lets you run arbitrary php – so you could set up a WP_Query that pulls all posts, loop through them, and fire that command on each one. Sort of what I had in mind, untested. Using wp_update_post() as … Read more

Is it possible to create a post using a metabox?

Update The answer is so simple, I couldn’t see it at first. 🙂 Just remove the action during the first function call. This way, your work within the API, and your function is really called just once. No need for static or even global variables or constants. function my_metabox_save() { remove_action( ‘save_post’, ‘my_metabox_save’ ); // … Read more

Why save_post_$(custom_post_type) is fired even if I am not already saving a post?

When you choose “Your CPT > Add New”, WP calls get_default_post_to_edit(), which actually creates an “empty” post (with ‘post_status’ => ‘auto-draft’) and then calls wp_insert_post(). This is what is causing your save_datasheet_meta() function to be called before you think it should. Hence, generally you should add some additional sanity checks to the beginning of any … Read more

Placement of Code in Plugin for hooking `save_post`

I’m not sure if if( !current_user_can( ‘edit_post’, $post_id ) ) return; really works. Maybe the current user isn’t set up there and you’ll have to work around it by passing the user ID in a hidden field, then retrieving it using $user = get_user_by( ‘id’, esc_attr( $_POST[‘user_id’] ) ); wp_set_current_user( $user->user_id ); if ( ! … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)