Skip Navigation

[Resolved] CRED post form doesn't fire the 'save_post' hook

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 7 replies, has 2 voices.

Last updated by Waqar 3 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#2007103

I have some custom code that fires when a post is saved from the dashboard.
The action for the code is

add_action( 'save_post', 'func_update_client_id', 30, 2 );

However when I use the CRED Form plugin to create the post, this custom code apparently doesnt run.

Is this a limitation of CRED?
Any suggestions?

#2007177

Hi,

Thank you for contacting us and I'd be happy to assist.

The "save_post" action is fired when a post is published using Toolset's front-end forms, but the post creation process is slightly different.

As soon as a page with the front-end form is loaded, a draft post is created (with a custom 'auto-draft' post status).

Then when the form is submitted this draft post is updated, and its status is changed to what is set in the form's settings.

To troubleshoot this, it would be a good idea to use the debug log to see whether the custom function "func_update_client_id" is being fired or not, when the form is submitted. For example:


add_action( 'save_post', 'func_update_client_id', 30, 2 );
function func_update_client_id() {
    error_log("function called"); 
...

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2007973

I added the "post_updated" hook but it still doesn't seem to be firing.

add_action( 'save_post', 'func_update_client_id', 30, 2 );
add_action( 'post_updated', 'func_update_client_id', 30, 2 );
function func_update_client_id( $post_id, $post ){
  error_log("function called"); 
....

Where can I find the error log and what should i try next?
A different hook?

#2008473

Thanks for writing back.

When the error logging/debugging is enabled in WordPress, the default log file "debug.log" is created in the "wp-content" folder.

Here are some guides on turning WordPress debugging on:
https://wordpress.org/support/article/debugging-in-wordpress/
hidden link

Once it is turned on, you can use the PHP's "error_log()" function to print some message/text in the log file, to confirm whether hooks are firing or not:
hidden link

In case the issue still persists, you're welcome to share temporary admin login details along with complete code for your function "func_update_client_id" and the details about what you expect from it.

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

#2012801

Any update on this?

#2013689

Thank you for sharing these details.

I was able to make the function work for both cases (i.e. if the "client-payment" is added through the admin area or the front-end Toolset form), with slight modifications:


function func_update_client_id( $post_id, $post ){
	//error_log("function called");   
	if ( 'auction-client' == $post->post_type ) {
		update_post_meta( $post_id, 'wpcf-client-id', $post_id);
	}
	else if ( 'auction-vehicle' == $post->post_type )
	{ 
		$client_id =  do_shortcode("[types field='client-id' output='raw' item='@client-username-vehicle.parent'][/types]");
		if ( !empty($client_id)) {
			update_post_meta( $post_id, 'wpcf-client-id-vehicle', $client_id);
		}
	}
	else if ( 'client-payment' == $post->post_type )
	{
		if(!empty($_POST))
		{
			// if post is being edited in the admin area
			if($_POST['action'] =='editpost') {
				$client_id =  $_POST['wpcf']['client-username-payments'];
			}
			// if the post is being edited using the front-end form
			elseif (!empty($_POST['client-username-payments'])) {
				$client_id =  $_POST['client-username-payments'];
			}
			if ( !empty($client_id)) {
				update_post_meta( $post_id, 'wpcf-client-id-payments', $client_id);
			}
		}
	}
}
add_action( 'save_post', 'func_update_client_id', 30, 2 );

Please note how I've handled both cases seperately, inside the "'client-payment' == $post->post_type" condition.

#2014255

OK. Great. Thank you!

Just so I understand,

$_POST['action'] =='editpost'

Is required EVEN in the case that a record is being created for the first time on the Admin Dashboard?

#2014341

Yes, your understanding is correct.

The $_POST['action'] =='editpost' will cover post creation/publishing for the first time, as well as any subsequent edits/updates, from the admin area.