What is the role and history of the $content_width global variable?

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.

Read More

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

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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. $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.