6
if ( ! isset( $content_width ) ) {
    $content_width = 660;
}

The above mentioned code exist in wordpress Default Theme Functions.php on line#33

My Question: $content_width

It looks like a global variable defined somewhere in core files in wordpress? $content_width=660;

what is 660 is it in pixel. Actually which content width is this setting? The current theme has this anatomy: sidebar and content area.

Or it is effecting post and pages types?

1
  • 3
    LOL just can't upvote enough this question Apr 11, 2016 at 12:15

1 Answer 1

6

$content_width is one of the dark magic global variables wordpress uses. The value refers to the width in pixels of the area in which the post content is being displayed in a post page. It is expected to be set by themes.

The reason for the

if ( ! isset( $content_width ) ) {

is to let child themes to override this value by defining it in their functions.php file

Why is it needed? There might be other needs, but one place in which it is needed is where the oEmbed protocol is being used to retrieve some HTML (usually an iframe) from a 3rd party service. For example a youtube video sharing iframe that is retrieved based on a youtube URL.

As part of the oEmbed protocol you can let the 3rd party server know the dimensions of area it may fill, and wordpress core needs to assume something that will make some sense just based on the youtube url, and it will use the single post content width - the $content_width variable.

Looking now at the 4.4 code I see that there are more uses, the native video shortcode, getting images in the correct size for the editor and more.

BTW the default is 500

5
  • Thanks! You mean if we go here - wp-themes.com/twentyfifteen Then in the Right hand side column the width is set by $content_width variable? But that raises a doubt how does sidebar attains the remaining area, isn't it that the $content_width should be in %age then. Thats the confusion I had because of which I posted the question.
    – WordCent
    Apr 11, 2016 at 14:24
  • Regarding your answer "As part of the oEmbed protocol...." that means it has subtle uses Oembed provides so many embedding features and as per your explanation $content_width will be sending subtle signals to the third party things that this is the width you can take, Right?
    – WordCent
    Apr 11, 2016 at 14:57
  • @Champion comment 1., your interpretation is not exactly correct, this value is usually in addition to everything done via CSS. PHP just have no way to know what is the width set in CSS, and setting up this variable is a way to provide this information. comment 2. exactly. If you use the oembed shortcode you can actually specify height and width, but when just doing the automagic conversion from a URL to an iframe this value is a subtle hint, basically used instead of the width value you might have passed in the shortcode. Apr 11, 2016 at 16:22
  • Thanks Sir. Can you help me little bit more about my comment #1.
    – WordCent
    Apr 11, 2016 at 16:59
  • 1
    @Champion, if you ask if it is the right hand side area, then yes, but actually more applicable to wp-themes.com/?p=19 Apr 11, 2016 at 17:11

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.