Classifieds PLugin: On Frontend Submit Classified (draft) hook

Hi,
I would like to know what is the right wp hook to capture the submission of a draft classified through the front-end form?
Both 'new_to_draft' and 'wp_insert_post' don't seem to work.
Thanks in advance

  • monolith

    Wow, thanks for the quick response. I 'd like to remove a credit from the classified author the first time the classified is created (set as 'draft' in classifieds settings)

    function reduce_credits( $post_id ) {
    		$slug = 'classifieds';
    		if ( $slug != $_POST['post_type'] ) {
    			return;
    		}
    		$author_id = $post_tmp->post_author;
    		$user_credits = get_user_meta( $author_id, 'classifieds_credits', true ) - 1;
    		update_user_meta($author_id, 'classifieds_credits', $user_credits);
    	}
    add_action('wp_insert_post', 'reduce_credits', 10, 1);

    This works only when I publish the classified in backend

    EDIT: I think I can see my error in my function's arguments. It's not just the $post_id (as in new_to_draft) but the whole post object. I will try to fix it and come back with the results, Thanks

  • monolith

    It seems that my syntax was ok.
    Could you give me an example that a 'wp_insert_post' Hook would capture the right ID of the newly created draft classified (always referring to the front end form).
    In some of my tests Iv'e captured the id but minus 1.
    Should this code work?

    function reduce_credits( $post_id ) {
        $slug = 'classifieds';
        if ( $slug != $_POST['post_type'] ) {
            return;
        }
        $post_tmp = get_post($post_id);
        $author_id = $post_tmp->post_author;
        $user_credits = get_user_meta( $author_id, 'classifieds_credits', true ) - 1;
        update_user_meta($author_id, 'classifieds_credits', $user_credits);
    }
    add_action('wp_insert_post', 'reduce_credits');

    I've hardcoded the following code in page-update-classified.php plugin's file and it works fine

    $post_tmp = get_post($post_id);
        $author_id = $post_tmp->post_author;
        $user_credits = get_user_meta( $author_id, 'classifieds_credits', true ) - 1;
        update_user_meta($author_id, 'classifieds_credits', $user_credits);
  • monolith

    I found out in your code that just by entering the add new classified page , you call wp_insert_post to create a blank auto draft.
    So in order to catch the real new classified after hitting save changes I've used the auto-draft_to_draft hook.
    The following code really worked form me

    function reduce_credits( $post ) {
        $slug = 'classifieds';
        if ( $slug != $post->post_type) {
            return;
        }	
    
        $author_id = $post->post_author;
        $user_credits = get_user_meta( $author_id, 'classifieds_credits', true );
        if( ($user_credits - 1) < 0)
            $user_credits = 0;
        else
            $user_credits -= 1;
        update_user_meta($author_id, 'classifieds_credits', $user_credits); 
    
    }
    add_action(  'auto-draft_to_draft',  'reduce_credits', 10, 1 );

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.