• Resolved Aurovrata Venet

    (@aurovrata)


    Hi Scott

    I am working on an extension to link your crm plugin to a document management plugin. Today I came a across a strange error which I manage to narrow down to this function wpCRMSystemCustomFields:saveContactTitle in your main file, wp-crm-system.php.

    This function is hooked to save_post, and executes if the saved post is type ‘wpcrm-contact’.

    I am hooking on the the same post update/publish. When a new contact is created/updates I am creating another cpt using the function wp_insert_post. This fires your function and is able to execute it because your are checking for the post_type based on the $_POST value. Since my newly created cpt is executed from the result of POSTed wpcrm-contact post, your function is unable to distinguish the two.

    Currently I am overcoming this problem by updating the the post title after inserting the post.

    However, could I suggest the following change to your code?

        You could either hook the save_post with 2 parameters,

        add_action( 'save_post', array( &$this, 'saveContactTitle' ), 2, 2 );
        function saveContactTitle( $post_id, $post ) {
          if('wpcrm-contact' != $post->post_type){
            return;
          }
          ...
        }
        or you could use the post_id to verify the post before updating it,

        function saveContactTitle( $post_id) {
          $post = get_post($post_id);
          if('wpcrm-contact' != $post->post_type){
            return;
          }
          ...
        }

    your thoughts?

Viewing 7 replies - 1 through 7 (of 7 total)
  • If I modified the existing saveContactTitle function to what’s below, would your function work?

    function saveContactTitle( $post_id ) {
    	global $post;
    	if ( empty( $post ) ) {
    		$post = get_post($post_id);
    	}
    	if ( $post_id == null || empty($_POST) ){
    		return;
    	}
    	if ( !isset( $_POST['post_type'] ) || 'wpcrm-contact' != $_POST['post_type'] || 'wpcrm-contact' != $post->post_type ) { //check for 'wpcrm-contact' != $post->post_type here
    		return;
    	}
    ...
    }
    • This reply was modified 9 years, 8 months ago by Scott DeLuzio. Reason: missing code tags
    Thread Starter Aurovrata Venet

    (@aurovrata)

    HI Scott

    thank for the quick reply. Unfortunately this does not work, somehow the global $post is still the initial wpcrm-contact post onto which I have hooked my cpt wp_insert_post.

    However, this works,

    
    function saveContactTitle( $save_post_id ) {
      global $post_id;
    
      if($save_post_id != $post_id){
         return;
      }
      ...
    }

    Any chance I could take a look at the function you’re using? I’d like to test my code changes live so I can see the issues you’re having and also make sure the core plugin functions correctly. If you don’t want the code public, you can send it using our contact form.

    Thread Starter Aurovrata Venet

    (@aurovrata)

    Dear Scott

    apologies for the late reply, here is my code (line 150), it hooks on ‘save_post_wpcrm-contact’, in which I validate some fields before creating a cpt for another plugin (line 232). It is this cpt (which has the same title at the hooked wpcrm-contact attached organisation) which has its title changed to the title of the wpcrm-contact by your function. I hope that makes sense.

    Thread Starter Aurovrata Venet

    (@aurovrata)

    I just updated to v2.0.6, but the issue still persists, so I figure the simplest change to your code would be

    
    function saveContactTitle( $post_id ) {
      global $post;
      if ( empty( $post ) || $post->ID != $post_id ) {
        $post = get_post($post_id);
      }
    ...
    
    Thread Starter Aurovrata Venet

    (@aurovrata)

    please confirm a fix for this, else I will need to implement some extra coding in my plugin to cover this.

    Check out version 2.0.7. This should fix the issue you were having.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Error in Function wpCRMSystemCustomFields::saveContactTitle modifying wrong cpt’ is closed to new replies.