WordPress.org

Ready to get started?Download WordPress

Forums

redirect post after submission to a thank you screen (8 posts)

  1. granville
    Member
    Posted 3 years ago #

    Dear all

    I am still a noob when it comes to WP and PHP in general.

    I would like to hack WP so that when a user submits a new Post for review, instead of showing the usual page, where the post is still there and they can click on the "see post here" link, they are instead redirected to a "Thank you" page.

    I really don't want to hack the core files as I know that when WP updates all will be lost; but I can't understand how to amend it via functions.php

    I have found a filter in this link http://wordpress.org/support/topic/redirect-to-new-post-after-publish that redirects the user to the actual post on submission; however, I cannot figure out how to amend it for my purposes (to the thank you page).

    Please can anyone assist?

    Many thanks

    G

  2. s_ha_dum
    Member
    Posted 3 years ago #

    In that function you found, you will need to set $pl to the 'redirect' location. I don't think you'll need the preg_match stuff. Just set the $pl value. Of course, you will need a valid place to redirect to. Have you set up a page/post/<backend something> for that yet?

  3. granville
    Member
    Posted 3 years ago #

    Hi apljdi

    Thanks for the response.

    I just realised that I'd forgotten a semi-colon - d'oh!
    So this is the code that I came up with:

    add_filter('redirect_post_location', 'redirect_to_thank_you');
    function redirect_to_thank_you()
    {
    if (isset($_POST['save']) || isset($_POST['publish'])) {
    wp_redirect('http://www.MYURL.com/thank-you');
    }
    }

    However, I now need to work on it as this also applies to new pages created too, and I really need it just for Posts. I'll have a play with it to see if I can do it, but if you have any ideas too that would be great!

    Cheers

    G

  4. granville
    Member
    Posted 3 years ago #

    Okay, so I decided that the best way would be to only have this function apply if it is not the admin logged in. (I only have the admin and the contributor as roles)

    (The logic being that the admin should be able to do everything, but the contributor will only be uploading posts.)

    So I amended the code as follows but it results in a white screen of death :(

    Can anyone help me understand why?

    add_filter('redirect_post_location', 'redirect_to_thank_you');
    function redirect_to_thank_you()
    {
    if ((!current_user_can('level_10')) && (isset($_POST['save']) || isset($_POST['publish']))) {
    wp_redirect('http://www.MYURL.com/thank-you');
    }

    Thanks
    G

  5. s_ha_dum
    Member
    Posted 3 years ago #

    If that is all of your code, you are missing a closing curly brace.

    add_filter('redirect_post_location', 'redirect_to_thank_you');
    function redirect_to_thank_you() {
      if (!current_user_can('level_10') && (isset($_POST['save']) || isset($_POST['publish'])))  {
        wp_redirect('http://www.MYURL.com/thank-you');
      }
    }
  6. granville
    Member
    Posted 3 years ago #

    Thanks for spotting that - yes it was that simple and I was that stupid...no more hangover web development for me.
    Final comment for what it's worth; I have ultimately opted for the if statement to start if(!current_user_can('edit_pages')
    so that it only targets the contributors and not any editors that I may create in the future.
    Thanks for your help apljdi.
    Kind regards
    G

  7. granville
    Member
    Posted 3 years ago #

    Okay, back again.

    So slight change in design. Instead of redirecting to a thank you page, the concept is now to redirect back to a fresh "new post" page.

    I already am redirecting any user who is not an admin from the dashboard to the new post page, so I simply decided to redirect them to the "wp-admin" page after submission.

    Code is here:

    <?php
    add_filter('redirect_post_location', 'redirect_to_new_post');
    function redirect_to_new_post()
    {
    		if (!current_user_can('edit_pages') && (isset($_POST['save']) || isset($_POST['publish']))) {
                wp_redirect('../wp-admin');
        }
    }
    ?>

    So I thought I had it all working smoothly. If the user is not the admin then it works fine.

    However, if the user is the admin, then after editing a page it redirects to a blank screen. Viewing source shows me that it's trying to show a page relating to a forgotten password. If I click back, then the update has taken effect.

    Can you help me understand why the admin publish page is being affected, and what I can do to correct it?

    Thanks

    G

  8. s_ha_dum
    Member
    Posted 3 years ago #

    Try redirecting all the way to the new posts form: /wp-admin/post-new.php

Topic Closed

This topic has been closed to new replies.

About this Topic