I'm really struggling to figure out how to make a form for a child post redirect to its parent after submission. There are a few solutions in the forum but I just can't get them to work properly.
Here's my code:
<?php
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
add_filter('cred_success_redirect', 'redirect_to_parent', 10, 3);
function redirect_to_parent($url, $post_id, $form_data) {
if ($form_data['id']==2873 || $form_data['id']==2896) {
$parent_id = toolset_get_related_post($post_id, 'work-recording');
$url = get_permalink( $parent_id );
if ($form_data['id']==3157 || $form_data['id']==3162) {
$parent_id = toolset_get_related_post($post_id, 'work-resource');
$url = get_permalink( $parent_id );
}
}
return $url;
}
My Work post type is the parent, and then I have Recording, Resource and Lyric and child post types. I want to redirect to the Work when I add or edit a child post type.
Hi, in the Form editor screen for these 4 Forms, the option "After visitors submit this form" must be set to "Go to a specific post" or "Go to a page". It doesn't matter which post or page is selected here, but some redirect must be set. Otherwise the redirect hook will not be triggered.
If that doesn't solve the problem, we should add some server log statements and monitor the logs to see what is happening. If you're not familiar with server logs, I can show you how to activate them temporarily. Go in your wp-config.php file and look for
define('WP_DEBUG', false);
Change it to:
define('WP_DEBUG', true);
Then add these lines, just after the WP_DEBUG line:
define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
Submit the Form, and a new file should be created in the root directory of your site called "error_log.txt". Open the log in any text editor, copy the contents, and paste them in your next reply. When we are done testing you can revert the changes in wp-config.php and delete the log.