Jump to content

Redirect admin view links for one page website?


Recommended Posts

I'm building a template for a one page website and have run into a small problem with the admin view page links.  

Because my site is built with content blocks rather than pages there is no header and footer for each block, only the homepage needs to pull in the header and footer for the site.  As a result clicking to view a page in the admin shows the page content but with zero styling.  This is obviously not ideal for clients so I'd like to find a better solution.

Here's a rough idea of the site structure:

    Home  (header & footer)

        Page 1

            Content Block 1

            Content Block 2

        Page 2

            Content Block 3 etc etc

My page's use an anchor # for the url eg #contact so Ideally when the user clicks on the page they'd be redirected to that relevant anchor.  I know this might be complicated to achieve so all I really need to do is redirect all pages to the homepage (parent).  

Any idea how I would go about this? 

Link to post
Share on other sites

If you use a different template on the pages that shouldn't be view-able (I assume everything but home), and that template does not have an associated file (example "basic-page" template has "basic-page.php") then the view button itself will not be shown. 

Another option is maybe to just use a repeater for the content blocks and then genuinely only have one page in the admin (home) and remove the other.

Hope that helps.

  • Like 1
Link to post
Share on other sites

just tell them not to view page in the admin, or make those pages redirect to the anchor on the homepage, using some php in the template file (?)

Link to post
Share on other sites

Thanks guys.  @adamspruijt I'm not quite sure what you mean by 

If you use a different template on the pages that shouldn't be view-able (I assume everything but home), and that template does not have an associated file (example "basic-page" template has "basic-page.php") then the view button itself will not be shown. 

Do you mean creating templates via admin without creating a physical template file?  All my blocks contain markup so they're built as separate eg block.php files.

I'm not familiar with the repeater field technique for content blocks.  Can you explain a bit more?

One workaround I thought of would be to add this to my admin template css which solves the problem up to a point.  

.content .PageListActionView {
  display: none !important;
}

 I was hoping there would be a nice simple way to handle it in Processwire. I know there's the redirect module but that's not ideal is it involves specifying each url and all I really need is a global rule that says - if child redirect to parent, or something like that.  

@Macrura can you give me an example of how I would add a redirect to a template?

Link to post
Share on other sites
<?php 
$homepage = $pages->get('/'); 
if($page != $homepage) { // if we're not on the homepage do this
	$redurl = $homepage->url . "#" . $page->name; // this is where the page will redirect to
	$session->redirect($redurl);
}
?>

don't do it this way - it won't work right on the homepage - look at diogo's post below.

Edited by Macrura
Link to post
Share on other sites

Thanks guys.  @adamspruijt I'm not quite sure what you mean by 

Do you mean creating templates via admin without creating a physical template file?  All my blocks contain markup so they're built as separate eg block.php files.

I'm not familiar with the repeater field technique for content blocks.  Can you explain a bit more?

Yes, I am talking about having a template without a file, you could probably move those templates that are not supposed to be view-able  like "block.php" into an "includes" folder and then use simple code to grab the right file based on the template name like this (assuming you've gotten the pages you need in the variable "$blocks"):

<?php 
foreach ($blocks as $block) {
	include("./includes/".$block->template.".php");
}
?>

 For the repeater method this would work only assuming the content in the "blocks" was very simple and could be handle with basic "Textarea/TinyMCE" fields, I didn't realize you mentioned #contact. Assuming the contact area requires a form or more advanced fields, what I mentioned first above would work best.

Link to post
Share on other sites

Macrura, your solution won't work because inside the render() the $page object is the one from the rendered template, so ($page != $homepage) will always be true.

But the same can be achieved by testing agains the url:

if($page->url == $_SERVER[REQUEST_URI]) $session->redirect($pages->get(1)->url."#".$page->name);

and in the homepage, something like:

foreach($page->children as $p) {
    echo "<div id='{$p->name}'>";
    echo $p->render();
    echo "</div>";
}

  • Like 1
Link to post
Share on other sites

@diogo - i thought that in this case the link from the admin was actually rendering the page at it's correct URL, like:

/page1/content-block-1

which is why i thought that would work... unless i'm missing something...

Link to post
Share on other sites

What happens with render is that the $page object is momentarily replaced by the rendered page, so if you render a page inside the homepage, and in that page template you compare $page != $homepage, this will always be true. By doing like I did, you are comparing the real url in the browser with the rendered page url. This means that if the page it's called directly, the real url and the url from the $page object will match, but if the page is rendered from another page they won't.

  • Like 1
Link to post
Share on other sites

Ok so I think what you are saying that when the homepage renders the content blocks, which are being done with the render method, the check I wrote won't work because the $page object is still not really 'home'; so i get that now, and will edit the code in my post..

Link to post
Share on other sites

Thanks guys.  @adamspruijt, I never thought of adding blocks like that.  Definitely fast to deploy a site but one advantage of building with blocks as children is the blocks can be changed without touching the source.  I'm sure this could be set up via an of options page though.

@diogo Looks like a fairly straightforward solution, look forward to testing it out.  Much appreciated.

Link to post
Share on other sites

Thanks guys.  @adamspruijt, I never thought of adding blocks like that.  Definitely fast to deploy a site but one advantage of building with blocks as children is the blocks can be changed without touching the source.  I'm sure this could be set up via an of options page though.

Why would you need to change the source? The structure of the block would be defined in the includes and ordered based on their order in the admin, unless your adding a new "block type", otherwise you should be able to add new content indefinitely. I don't think I know enough about what you're doing, you may not require the flexibility I'm picturing.

Regardless the redirect works, and allows you to have the view buttons intact on the back end, which could be nice.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jploch
      Hey folks,
      currently Iam working on a website for one of my clients and I need some advice on how to approach this in PW.
      The website is for a company, that offers holiday houses in two locations. 

      The client wants the homepage to show the first location. Normally I just have a home template for the first page, but here the URL should reflect that you are in Location 1. So when you visit the URL casamani.com it should redirect to casamani.com/location-1. Not sure if this makes sense at all.

      Whould it be bad for SEO and performance reasons to redirect home to the Location-1 page?
      Another approach would be to render the Location-1 template on the home template or do an include like discussed here.
      Here is how the tree looks:
      – Home
      – Location 1 (Homepage)
           – Creation
           – Adventure
           – Sustainability
      – Location 2 
           – Creation
           – Adventure
           – Sustainability

      Thanks for looking into this!
    • By rjgamer
      Hi,
      is there a hook after the current (active) page got created? Or which method got called in the Page class after the Constructor of the current page got initialized?
      Thanks.
       
    • By milo695
      Hello,
      I've added a class in ProcessPageEditLink but when I call it from Editor (attributes > class) nothing happens.
      Also, I've created the class in my custom.css, is there any other css file this class needs to be in?
      thanks
    • By milo695
      Hi all,
      I need help creating link in CK editor that would target my translated page, so it depends on what language user has selected.
      something like:
      <a href="?id=1450">view all</a> would target:
      <a href="en/onion/">Onion</a> <a href="de/zwiebel"></a> depending on user defined language.
      Thank you
    • By rjgamer
      Hi guys,
      the field "redirect_last" of type DateTime got not updated. The update on the field "redirect_counter" works and got saved.
      Does anybody know what I did wrong in my code?
      if ($input->urlSegment(1) === 'redirect') { $page->of(false); $page->redirect_last = time(); $page->redirect_counter += 1; if ($page->save('redirect_counter')) { $session->redirect($page->website_url, 302); } } Thanks.
×
×
  • Create New...