0

My website allows users to publish and manage posts from frontend. I've created a kind of account section where they have all their posts published which they can edit and delete. They can even delete posts through the post page (single.php). So 2 cases:

  • My account page (listing all posts): quick actions -> delete post. It works fine, not being necessary a redirect. The page refresh and post is gone.
  • The internal post page (single.php): actions -> delete. In this case I should redirect my people to their account page or wherever else because they get a not_found page after the action. This is my problem.

I could specify a wp_redirect(get_permalink(my_account_page_id)) during the action but my functions is not in-page and it's called through do_action by my functions.php. So I should check every time if they are into their account or in the internal post page (single.php). I've even tried using $pagenow but without success.

My action in functions.php call a modal and looks like:

add_action('delete_post', 'delete_p_function');
    function delete_p_function($pid) { 
        global $wpdb; $pagenow;
        $delete = 'delete_'.$pid;
        if(isset($_POST[$delete]))
        {
            wp_delete_post($pid);
            if($pagenow == 'single.php'):
                wp_redirect(get_permalink(my_account_page_id));
            endif;
        } ?>
        <div id="delete-<?php echo $pid?>" class="modal fade" role="dialog">
            <div class="modal-dialog">
                <form method="post" class="top-15 text-center">
                        <input type="submit" name="delete_<?php echo $pid?>" class="submit_red" value="<?php _e("Delete",'KleeiaDev'); ?>" style="width:100%; text-align:center"/>
                </form>
           </div>
        </div>

    <?php }

Of course if is not single.php there is no action in <form> so the page will be refreshed. So it's fine for the listing page. But how can i check if is in page single.php?

PS. I've even tried is_page_template() without success.

  • Somebody can give me a direction? Thanks – huraji Jun 6 '16 at 11:29
1

I think that this native function will help you:

 if ( is_single() ) {
     //do stuff
 }

You will find more information in the WP developers section. It works for any post type, except attachments and pages.

  • I've tried and debug says 'Warning: Cannot modify header information - headers already sent by...'. How can I manage this problem? Basically the redirect does not take place. – huraji Jun 6 '16 at 14:43
  • Can you copy here all the warning message? – Capiedge Jun 6 '16 at 15:45
  • I've actually solved and thank you for your support! – huraji Jun 7 '16 at 8:38

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.