Unpublish pages?

  • Has anyone added functionality to allow a page to be unpublished? We have a site that is ready to go live, except for one page that doesn't have approval yet. I know we could hide it from the navigation, but is there anyway to unpublish it so it won't show up unless you're in the back end?

  • Yes, you can do this with page attributes and a little bit of PHP: https://grabapipit.com/blog/drafting-at-the-page-level


    If you prefer to write less PHP you can install the Pipit app and use the pipit_perch_user_logged_in() function:


    PHP
    1. // get the page status
    2. $page_status = perch_page_attribute('status', [], true);
    3. if($page_status == 'draft' && !pipit_perch_user_logged_in()) {
    4. // page is draft and user is not logged into Perch control panel
    5. }


    If you are using Runway, you can use your 404 page like so:


    PHP
    1. if($page_status == 'draft' && !pipit_perch_user_logged_in()) {
    2. PerchSystem::use_error_page(404);
    3. exit;
    4. }


    I don't think you can use PerchSystem::use_error_page() on standard Perch. You can redirect to the 404 if you're ok with that. This doesn't respond with a 404 code though.


    PHP
    1. if($page_status == 'draft' && !pipit_perch_user_logged_in()) {
    2. PerchSystem::redirect('/404.php');
    3. }



    Or you can use PHP's http_response_code() to set the HTTP response code and then output you not found message:

    PHP
    1. if($page_status == 'draft' && !pipit_perch_user_logged_in()) {
    2. http_response_code(404);
    3. // display your not found message from a Perch Layout
    4. perch_layout('404');
    5. exit;
    6. }
  • This is exactly what I needed. Thank you!


    One little change, I had to add 'suppress' to this otherwise it was showing on every page.

    Code
    1. <perch:pages id="status" type="select" label="Status" options="Draft|draft,Published|published">