Support » Plugin: Pods - Custom Content Types and Fields » pods_api_post_save_pod_item or save_post not working on frontend

  • Resolved onmountain


    Hi everyone,
    I am pulling my hair out on this one. Almost at the point of changing from PODS, so please help if you can…

    I have been trying to get any action hook to fire when I insert and/or update a pod on the frontend form. I have tried pods_api_post_save_pod_item and pods_api_post_save_pod_item_my_pod_name but these do not seem to fire. I even tried save_post but that is not either. I am trying to do some tasks when the submit is clicked on the pods created form.

    I have deactivated all other plugins, tried a different theme. I have run these in snippets and also in a custom plugin. I know the save_post works when I update a page or post.

    Here is an example that is not triggered:

    add_action('pods_api_post_save_pod_item', 'my_test_function', 10, 3); 
    function my_test_function($pieces, $is_new_item, $id) { 
    	$myfile = fopen("podsubmitlog.txt", "a") or die("Unable to open file!"); 
    	$date = new DateTime();
    	$date = $date->format("y:m:d h:i:s");
    	$txt = "Test function ---- ".$date."\n";
    	fwrite($myfile, $txt);

    I also set the pods settings so that the api is enabled for the pod, as well as individual fields. I also tried changing the hook priority, etc.

    The weird thing is that I stumbled upon this action hook, that does not appear in the PODS documentation. But it actually does fire. Can I use this???

    function action_pods_form_after_submit( $pod, $fields, $params ) {
    	$thetitle = $pod->field('post_title');
    	$thetype = $pod->field('post_type');
    	$myfile = fopen("podsubmitlog.txt", "a") or die("Unable to open file!"); // ../wp-content/plugins/jamie/
    	$date = new DateTime();
    	$date = $date->format("y:m:d h:i:s");
    	$txt = $date."--".$thetitle."--".$thetype."\n";
    	fwrite($myfile, $txt);
    // add the action
    add_action( 'pods_form_after_submit', 'action_pods_form_after_submit', 10, 3 );

    My form:
    Just to be complete, my form is on a page, using this short code:

    add_shortcode( 'edit_sitter', function () {
    	$out = ''; // init
    	$theurlparamname = 'sittertitle';
    	$theparam = get_request_parameter($theurlparamname);
    		$mypod = pods( 'sitter', $theparam );
    		$fields = array( 'post_title' => array( 'type' => 'text', 'label' => 'Name of Sitter)', 'required' => true ), 'email', 'cell', 'notes' => array( 'label' => 'My Notes',  'type' => 'paragraph' ), 'availability', 'sitteruserid' => array( 'hidden' => true, 'readonly' => true ), 'memberuserid' => array( 'hidden' => true, 'readonly' => true ) );	 // note can't use defaults on edit
    		$thebuttontxt = 'Save Changes';
    		$sURL    = site_url(); // WordPress function to get site url
    		//$thereturnlink = $sURL . '/my-sitters';	
    		$thereturnlink = $sURL . '/view-sitter/?sittertitle='.$theparam;	
    		$out .=  $mypod->form( $fields, $thebuttontxt, $thereturnlink );	
    	return $out;
    } );

    Again, the form functions correctly…

    I am at a lose on what is happening. All pods features and all my 5 pod ityems save and work correctly. Any help is appreciated,

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jory Hogeveen


    Hi @onmountain

    In your specific case you can use the form hook, but if it should also fire when someone in the admin changes the same object then it won’t trigger.

    Where (how) did you register the pods_api_post_save_pod_item in your installation?

    Do you see any errors in the logs? Even though it might save there could be error preventing the code from further execution.

    Is the pods_api_pre_save_pod_item hook also not firing?

    Let me know!

    Cheers, Jory

    Thread Starter onmountain


    Thanks Jory,
    Turns out everything was firing, but I wasn’t able to get the function to write into my own log file. My bad πŸ˜‰ I think that was a permissions thing. What tricked me was that save_post was working (and logging into my log file) when I clicked update on a regular post or page, but not a CPT. I isolated it being only a PODS issue by coding a simple CPT in a test plugin and that also did not appear to work, so I figured out it must have been something of my doing…

    I simply set up the wp_debug log and a write_log function, and using that could see it was actually working for any CPT, including a pods CPT πŸ™‚ My confidence in using PODS and debugging a complex app is high – thanks to you and your team for building a great system.

    In case this helps any future programmer, here is soem code to help debug:

    // This is a logging function for any debugging task 
    // NOTES:  Youy must have the following lines in the wp-config.php file in the root folder, which
    //      puts a debug.log text file under the wp-content folder under root
    // define( 'WP_DEBUG', true );
    // define( 'WP_DEBUG_DISPLAY', false );
    // define( 'WP_DEBUG_LOG', true );
    // NOTES: install Error Log Viewer Plugin by bestwebsoft to view log from admin menu
    // for error logging
    if (!function_exists('write_log')) {
        function write_log ( $log ) {
            if ( true === WP_DEBUG ) {
                if ( is_array( $log ) || is_object( $log )) {
                    error_log( print_r( $log, true ));
                } else {
                    error_log( $log );

    You can then use the log to test what is happening “behind the scenes”:

    add_action( 'save_post', 'psc_anyposttype_save', 10, 3 );
    function psc_anyposttype_save($post_id, $post, $update) {
    write_log('I am in the save_post hook for post_id = '.$post_id.' -- post post id = '.$post->ID.' -- type='.$post->post_type);

    Again, encountering bugs and problems can be very frustrating for coders, BUT it sure helps you understand the framework better, so it was a constructive (even if very frustrating) few days. Good luck everyone and as they say on Gallaxy Quest … never surrender πŸ™‚

    Plugin Author Jory Hogeveen


    Hi @onmountain

    No problem! Thanks for letting me know πŸ™‚

    Cheers, Jory

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘pods_api_post_save_pod_item or save_post not working on frontend’ is closed to new replies.