Last active
January 29, 2019 00:05
-
-
Save ridinghoodmedia/40f01f89eb674fa638d31efd7f597847 to your computer and use it in GitHub Desktop.
Custom redirect after pods creates new post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Allows custom redirect using custom fields belonging to newly created post | |
* after a pods form submission | |
*/ | |
class PodsRedirect { | |
public function __construct() | |
{ | |
// WP uses this to determine query vars | |
add_filter('query_vars', [$this, 'addQueryVars']); | |
// Standard hook for redirects | |
add_action('template_redirect', [$this, 'redirectNewPosts']); | |
} | |
/** | |
* Create query var for new posts to access url param. | |
* | |
* This allows us to check url queries at runtime to determine if a new | |
* Participant post is being created. | |
* | |
* @param $vars | |
* @return array | |
*/ | |
public function addQueryVars($vars) | |
{ | |
$vars[] = 'new_post'; | |
return $vars; | |
} | |
public function redirectNewPosts() | |
{ | |
if( is_singular('post_type_slug') && get_query_var('new_post') ) { | |
// Get pod of new post | |
global $post; | |
$pod = pods('post_type_slug', $post->ID); | |
// Build redirect url using custom fields | |
$field_val = $pod->field('field_name'); | |
$redirect_url = "/new_redirect_slug/?field_name={$field_val}"; | |
wp_safe_redirect($redirect_url); | |
exit; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment