-1

I have a Custom template. In it i want to check that if the current logged in user has custom posts named (student_form) or not if posts equals true i want to redirect it to some page other wise i want to show him/her a form which i created using wp fronted user.

Following code i wrote but its showing me blank page.

     <?php 
     /*
     * Template Name: Form Page
     */
     get_header(); 


     global $current_user=null;
     get_currentuserinfo();

     $args=array(
     'author' => $current_user->ID,
     'post_type' => 'student_form',
     'caller_get_posts'=> 1
     );
     $my_query = null;
     $my_query = new WP_Query($args);
     if( $my_query->have_posts() ) 
     {

     exit( wp_redirect('http://burjhuraira.com/orgibs/dashboard/') );
     }

     else
     {
     echo do_shortcode('[wpuf_form id="414"]');

     }

     wp_reset_query();

     get_footer(); 
     ?>
  • 1
    Please do not use flags against your question in case there's no progress. – kaiser May 11 '15 at 12:46
2

Notice that your redirection must happen before the HTTP headers are sent out.

You can try the following, where we place our logic inside the template_redirect hook callback (PHP 5.4+):

/**
 * Redirect logged-in users to the admin dashboard 
 * if the have written 'student_form' posts
 * else display [wpuf_form id="414"]
 */

add_action( 'template_redirect', function()
{
    if( ! is_user_logged_in() )
        return;

    if( wpse_current_user_has_posts( 'student_form' ) )
    {
        // Redirect to the admin dashboard:
        wp_safe_redirect( admin_url() );  // Edit to your needs!
        exit();
    }
    else
    {
        // Note that here you might instead do a redirect to a page containing that shortcode.
        get_header();
        echo do_shortcode( '[wpuf_form id="414"]' );
        get_footer();
        exit();
    }

} );

where we use this helper function:

/**
 * Check if the current user has written any posts in a given post type
 * 
 * @param string $post_type
 * @return bool
 */    
function wpse_current_user_has_posts( $post_type = 'post' )
{
    $posts = [];
    if( $uid = get_current_user_id() )
    {
        $posts = get_posts( 
            [ 
                'posts_per_page' => 1, 
                'post_type'      => sanitize_key( $post_type ), 
                'author'         => $uid,
                'fields'         => 'ids'
            ]
        );
    }
    return 0 < count( $posts );
}
  • should i Write both in function.php? – Ammar Ul Hassan May 11 '15 at 11:15
  • that should work or better yet create a plugin @AmmarUlHassan – birgire May 11 '15 at 11:17
  • Actuall iam not that good in WP and my concepts for hooks are also weak so please explain about ur answer – Ammar Ul Hassan May 11 '15 at 11:19
  • You could try your functions.php file first. I haven't tested the code but hopefully there are no syntax errors. Note that this is for PHP 5.4+. The reason why I choose the template_redirect hook is that it happens before the HTTP headers are sent out. @AmmarUlHassan – birgire May 11 '15 at 11:23
  • Its not working :( – Ammar Ul Hassan May 11 '15 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.