• Resolved markwill

    (@markwill)


    I am somewhat new to WordPress theme development. I’m a little unclear on the role of wp_head() and the degree to which it (indirectly) adjusts page layout.

    I have a site that is currently using the Twenty Fifteen theme. One of the pages I want to create is unique to the site – by design it looks quite different any of the pages in the theme. I do want to render as a WordPress page, though, so that I can later add widgets, edit the content, etc.

    So, I created (let’s say) a mypage.php file the page and associated header-mypage.php and footer-mypage.php files. Initially these are virtually empty.

    Here’s the part where I am a little confused. If I leave in wp_head() then the page has the left and right parts of the Twenty Fifteen theme, even though none of the aforementioned .php files implement this. If I take out the reference to wp_head() then my page renders just fine.

    So, obviously I can just leave out wp_head() but I’ve read a number of times that I shouldn’t be doing that in a WordPress page – I need to give the chance for plugins to leverage hooks in referenced through wp_page().

    I haven’t yet located WHERE the code is that is called from wp_head() that actually changes the layout of the page – all I know is that there’s some theme-level page layout that is affecting even the most empty of pages I create.

    Should I just leave wp_head() after all?

    Thanks.

    Mark

Viewing 6 replies - 1 through 6 (of 6 total)
  • Here’s the part where I am a little confused. If I leave in wp_head() then the page has the left and right parts of the Twenty Fifteen theme, even though none of the aforementioned .php files implement this. If I take out the reference to wp_head() then my page renders just fine.

    In which file are you removing wp_head(): header.php or header-mypage.php? Are you explicitly calling header-mypage.php in mypage.php by using get_header( 'mypage' )?

    Thread Starter markwill

    (@markwill)

    Thank you for the response, Stephen. The basic flow is..

    I have basically mirrored the single.php (and the header and footer files) to create my own three files.

    The mypage.php file has a call to get_header(‘mypage’);

    In header-mypage.php, I originally had the call to wp_head() just before the closing </head> tag (which is where I believe it is supposed to be). If I leave that in the page gets messed up with remnants of Twenty Fifteen somehow. If I remove wp_head() at that point the page renders perfectly.

    Note that mypage.php, header-mypage.php and footer-mypage.php are stripped down to the minimum (basically an almost empty page) as I test this. With the wp_head left in, though, I get some HTML added for Twenty Fifteen, which messes up the page.

    So, as I say, I guess I have a fix – except that I keep reading to leave this call to wp_head() in so plugins, etc can register for hooks.

    Make sense?

    Mark

    With the wp_head left in, though, I get some HTML added for Twenty Fifteen, which messes up the page.

    Can you clarify which HTML gets added with and without wp_head()?

    Can you post the contents of mypage.php and header-mypage.php to Pastebin and post the link here?

    Thread Starter markwill

    (@markwill)

    Sure.

    Here’s mypage.php: http://pastebin.com/0Kc0yCkK

    And here’s header-mypage.php: http://pastebin.com/sqyhZhQ4

    The version posted includes wp_head() and results in this HTML: http://pastebin.com/tCW8uNM3. On the page, that shows as the page being split – 1/3 on the left in white and the 2/3 on the right in gray (which comes from the Twenty Fifteen theme, I guess). Note that this is despite extremely brief mypage.php and header-mypage.php files (and certainly nothing to do with that page split).

    If I remove the wp_head() call, as the only change, the page is much more streamlined: http://pastebin.com/fN8CAJgw.

    It is interesting to me that the underlying hooks related to wp_head() have such an impact on page layout – I would have thought that would be in the various .php files.

    So, at this stage I want to leave wp_head() in, so that the various plugins are integrated, but not have it directly impact the page layout.

    I am hardly the expert here but I am curious to know whether a “good” theme design should leave wp_head() having such a direct impact on page layout.

    Thanks.

    Mark

    Okay, I see what’s going on. Twenty Fifteen sets the background color for the sidebar/menu area and the main content area by setting a width and background color for body::before { ... } and a background color on body { ... }, respectively.[*] By leaving out wp_head(), WordPress doesn’t load any stylesheets on that page and so it looks normal.

    You could fix this by writing some custom CSS targeting that specific page template:

    body.page-template-mypage-php::before {
    width: 0%;
    }
    
    body.page-template-mypage-php {
    background-color: #fff;
    }
    
    body.page-template-mypage-php .site { ... }
    body.page-template-mypage-php .site-footer { ... }

    and so on and so forth. Another option would be to use is_page_template() to load a custom stylesheet for that particular page template.

    [*] See lines 3893 and 92 of style.css. Also, Twenty Fifteen writes inline styles to <head> if you select alternative color schemes in Dashboard > Appearance > Customize > Colors.

    Thread Starter markwill

    (@markwill)

    Awesome – thank you, Stephen. Really appreciate the responses.

    My challenge now is to better understand the difference between what I CAN do (technically) and what I SHOULD do 🙂 I am not motivated to tweak things too much. I like to follow best practices and keep things simple as much as I can. I guess my overall goal here is relatively common – use a theme but have a small number of pages that have a different design (for whatever reason). I have learned, from this thread, that the theme can impact the page layout indirectly through wp_head(), by sucking in stylesheets, rather than leaving that just to .php files.

    Thanks again, Stephen. I will mark this as resolved – and then chew on my options 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Leaving out wp_head()’ is closed to new replies.