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?
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
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?
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.
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.
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?
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.