1

I'm building a custom theme in Wordpress and I need to add a CSS attribute to the body tag when the URL ends in ?checklist-view=1

What is wrong with my code below, since no fullscreen is being added?

I understand that I need to use the superglobal $_GET, however I'm completely new to PHP.

Is there an easier way to do this with the body_class template tag (like im trying below) or is this something that would require more complicated PHP

EDIT: Changed $class to equal a string and not have it set to an array.

<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0">
    <title>Process Street <?php wp_title(); ?></title>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>" type="text/css" media="screen" />
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    <!--[if lt IE 9]>
    <script src="<?php echo get_stylesheet_uri(); ?>bower_components/html5shiv/dist/html5shiv.js"></script>
    <![endif]-->
    <?php wp_head(); ?>
</head>
<?php
//?checklist-view=1
$is_checklist = $_GET['checklist-view'];
$class= 'fullscreen';
if($is_checklist == '1') {
    echo $class;
}

?>
<body <?php body_class($class); ?>>

1 Answer 1

1

You shouldn't echo the class but, rather, you should just assign it to the variable to be passed to body_class() later. I'd also check whether or not checklist-view is set, to avoid errors:

<?php

$is_checklist = $_GET['checklist-view'];
if ( !empty( $is_checklist ) && $is_checklist === '1') {
    $class= 'fullscreen';
} else {
    $class = '';
}

?>
<body <?php body_class($class); ?>>

You can also achieve the same thing with the ternary operator:

$checklist = $_GET['checklist-view'];
$class = ( !empty( $checklist ) && $checklist === '1') ? 'fullscreen' : '';
?>

<body <?php body_class($class); ?>>
5
  • Hey there. I tried both solutions and neither added the fullscreen class to the body tag. Commented Oct 22, 2015 at 21:30
  • That shouldn't be possible...Can you var_dump( $_GET['checklist-view'] )?
    – rnevius
    Commented Oct 22, 2015 at 21:32
  • I put the var_dump right under the last curly brace, and loaded my page and did a search for it in 'inspect element' and cant seem to find anything. What do you think I should do? Thank you so much for helping. You're being a massive help. Commented Oct 22, 2015 at 21:44
  • You shouldn't need to inspect it anywhere...It should just print the contents of $_GET['checklist-view'] to the page. Are you sure you're doing this in the right template?
    – rnevius
    Commented Oct 22, 2015 at 21:45
  • 1
    It would appear that I am massive dingus. I was working in the wrong project folder in phpstorm. You sir, are a gentleman and a scholar. Commented Oct 22, 2015 at 21:53

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