18

I just stumbled over a global variable called $content_width. It was new to me, so far I could see that it's used in themes, both the standard twenty ten one and third party ones.

It looks like that it contains the width of the content area in pixels. Since global variables are not documented in the codex, I had trouble finding information about it.

When was the $content_width global variable introduced and for what reason? Is there a ticket related to it?

5
  • 2
    really good question. i hope you update the codex once you got a definitive solution.
    – kaiser
    Mar 11, 2011 at 18:27
  • If you submit a theme to the WordPress directory they'll insist it's set before the theme is OK'd. I'm not sure what it's for: you could use CSS/HTML in all of Philip's scenarios.
    – user3876
    Mar 11, 2011 at 20:35
  • @kaiser: I have not clue if it's possible to document global variables in codex at all, but I'll do at least a write-up here or there if I come to some conclusion, sure.
    – hakre
    Mar 13, 2011 at 15:27
  • 1
    I have created a documentation in the codex: codex.wordpress.org/Content_Width
    – Rami
    Feb 6, 2013 at 12:07
  • Weird this post just popped up in the stream as I was writing something about it here (on why it sucks)wycks.wordpress.com/2013/02/14/…
    – Wyck
    Feb 14, 2013 at 0:15

3 Answers 3

5

I believe it was added back on WordPress 2.6 (maybe 2.5). It's there to allow you to set a maximum width on images being displayed as content in your theme. E.g. If your main content area is only 475px wide, you could set $content_width = 450 to make sure your image doesn't break out of your main content area.

5
  • The only reference I could find so far specific to a theme shipping with wordpress itself ("core") is [core.trac.wordpress.org/ticket/10682 Ticket #10682: Define $content_width for Kubrick]. It was introduced by ViperBond but he did not share any information about his intentions. It's written in other tickets that it is a feature but it remains undefined. Looks like everybody but wordpress core themes were using it prior WP 2.9-rare. Thats some years since 2.6/2.5
    – hakre
    Mar 11, 2011 at 15:01
  • 1
    The first mention I remember seeing of it was via this blog post, mainstreetopen.com/wordpress-26-and-full-width-images I did some more searching and I think this is the core.trac.wordpress.org/ticket/5777 is when it was added.
    – Manzabar
    Mar 11, 2011 at 15:49
  • 2
    Searching google like so would of made narrowing down when it was introduced much easier.. site:http://svn.automattic.com/wordpress/tags/2.5 content_width (no results for 2.4) ...
    – t31os
    Mar 12, 2011 at 9:19
  • @t31os - thanks for that google search hint.
    – hakre
    Mar 18, 2011 at 12:59
  • Use it all the time for searching Trac(works better than the Trac search). :)
    – t31os
    Mar 18, 2011 at 13:11
5

Somewhat related: I don't know if it's reasonable to use $content_width if you plan on developing a theme that allows flexible design options. For example, let's say that your #content will be set to 512px if a user has sidebar widgets active and 768px if a user has no sidebar widgets active. I see no reason why CSS isn't enough to handle image overflows in main content areas:

.size-auto, 
.size-full,
.size-large,
.size-medium,
.size-thumbnail {
    height: auto;
    max-width: 100%; }

This CSS works perfectly fine to not only a) protect against image overflows into adjacent DIVs but also b) properly vertically resize images when they are shrunken down to fit into #content. Maybe I'm missing something but I don't quite get the need for $content_width.

3
  • 2
    The $content_width is not merely presentational. It also affects stuff in admin area and size for non-image elements in oembeds.
    – Rarst
    Mar 11, 2011 at 20:42
  • 3
    Thanks Rarst. I figured, but it still doesn't make me feel that happy with it when it comes to building themes with flexible content areas. Also, I wonder if this is critical given that max. dimensions can also be set on the Media Settings page. In any event thanks for the response. Mar 13, 2011 at 6:11
  • I really need to check if the Media Settings page is modifying that value. That could explain why twenty-ten is if-checking $content_width prior set.
    – hakre
    Mar 18, 2011 at 13:01
4

$content_width is used for limit the image width in post content. It will overwrite setting in Media section for large size. If you define this variable in functions.php file, then the width of large size of uploaded image will be $content_width.

Here is a post about this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.