/    Sign up×
Community /Pin to ProfileBookmark

I need some help with php and wordpress please :D

Hey all!
I NEED YOUR HELP PLEASE!
I have a client that wants to change the background color of all h1 h2 h3’s and certain sections’ background’s color according to the season that they are in, but for the life of me I just can’t and PHP is my weakest point. is there anyone here that would be able to give me advice on this and how I would implement it on the WordPress website.

much love to all.
cheers

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 17.2022 — I haven't touched WordPress in years now, so don't know exactly where the changes would be made, but an approach I might take would be to add a final CSS file to be loaded in the <head> section, and have it be a .php file. Then I might do something like this (with logic copied from [this stackoverflow post](https://stackoverflow.com/questions/40893960/php-how-to-check-the-season-of-the-year-and-set-a-class-accordingly))
[code=php]
<-?php // remove the hyphen!

$today = date('z');
if($today < 80 or $today > 356) {
$color = '#6699FF'; // winter
}
elseif($today < 173) {
$color = '#66FF99'; // spring
}
elseif($today < 266) {
$color = '#66FF33'; // summer
}
else {
$color = '#FFCC33'; // autumn
}
echo "
h1, h2, h3 {
background-color: $color;
}
";
[/code]
Copy linkTweet thisAlerts:
@ginerjmAug 17.2022 — Very clever and so fast!
Copy linkTweet thisAlerts:
@ruttgerauthorAug 17.2022 — Thank you so much for answering so quickly! i just have a few questions though....

If I were to use this logic as php snippet and make it header / sitewide accessible. How would i use the $color value on the website's standard css file?
Copy linkTweet thisAlerts:
@NogDogAug 17.2022 — @ruttger#1646008

I'm not really WordPress savvy these days. I might look at this first search result I found -- https://mrvirk.com/how-to-add-code-to-head-tag-in-wordpress.html -- and just add a line to the to use the above file, something like:
[code=html]
<head>
<!-- make sure it's created/saved as a PHP file -->
<link rel="stylesheet" href="/path/to/season.php">
</head>
[/code]
Copy linkTweet thisAlerts:
@NogDogAug 17.2022 — PS: Might want to add the following to the PHP/CSS file (after the opening php tag and before you echo anything), so that the browser doesn't cache it for too long. (In this example I'm using 43,200 seconds = 12 hours, adjust as you see fit.).
[code=php]
header("Cache-Control: public, max-age=43200");
[/code]
Copy linkTweet thisAlerts:
@ruttgerauthorAug 22.2022 — thanks for the help i managed to a develop a solution hope this helps someone in the future.

function load_season_stylesheet() {<br/>
// Locate the css for the season<br/>
$seasons = array(<br/>
"spring" =&gt; "assets/css/spring.css",<br/>
"summer" =&gt; "assets/css/summer.css",<br/>
"autumn" =&gt; "assets/css/autumn.css",<br/>
"winter" =&gt; "assets/css/winter.css"<br/>
);<br/>
// What is today's date - number<br/>
$day = date("z");<br/>
// Days of spring<br/>
$spring_starts = date("z", strtotime("March 21"));<br/>
$spring_ends = date("z", strtotime("June 20"));<br/>
// Days of summer<br/>
$summer_starts = date("z", strtotime("June 21"));<br/>
$summer_ends = date("z", strtotime("September 22"));<br/>
// Days of autumn<br/>
$autumn_starts = date("z", strtotime("September 23"));<br/>
$autumn_ends = date("z", strtotime("December 20"));<br/>
// If $day is between the days of spring, summer, autumn, and winter<br/>
if ($day &gt;= $spring_starts &amp;&amp; $day &lt;= $spring_ends) : $season = "spring";<br/>
elseif ($day &gt;= $summer_starts &amp;&amp; $day &lt;= $summer_ends) : $season = "summer";<br/>
elseif ($day &gt;= $autumn_starts &amp;&amp; $day &lt;= $autumn_ends) : $season = "autumn";<br/>
else : $season = "winter";<br/>
endif;<br/>
$season_path = $seasons[$season];<br/>
// $season_data = get_season_stylesheet();<br/>
$stylesheet_handle = $season;<br/>
$stylesheet_src = get_theme_file_uri($season_path);<br/>
$deps = false;<br/>
$ver = null;<br/>
$media = 'screen';<br/>
wp_enqueue_style($stylesheet_handle, $stylesheet_src, $deps, $ver, $media);<br/>
}<br/>
add_action('init', 'load_season_stylesheet');
×

Success!

Help @ruttger spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...