WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Searching here I've found two questions related and at the time, this wasn't possible.

how to create a conditional content_width for a wordpress theme?

And an alternative way via CSS described by Philip Arthur Moore (the code is also on Codex): What is the role and history of the $content_width global variable?

Since the questions mentioned are a little old, my question is if the versions released after mid 2013 have improved the function so we could use it with conditionals.

share|improve this question

As of now there is no direct way to conditionally set content_width.But we can do this using template_redirect hook and changing the global content_width.This is done in Twenty Fourteen Theme.Following is the code.

 * Adjust content_width value for image attachment template.
 * @since Twenty Fourteen 1.0
function twentyfourteen_content_width() {
    if ( is_attachment() && wp_attachment_is_image() ) {
        $GLOBALS['content_width'] = 810;
add_action( 'template_redirect', 'twentyfourteen_content_width' );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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