MOP: Fake News

Moment of Pride: Fake News

      If you have ever visited OFAL, you may have noticed a number towards the top right, directly above the search box. This number indicates what percentage of OFAL is ‘Fake News‘. Or not, as the number changes randomly.

      When I started the writings that became OFAL, I knew I wanted to somehow indicate that it wasn’t a serious news site. Adding an indicator for what percentage of the news is ‘fake’ was one way to do this. That it could also be viewed as commentary on the ongoing ‘fake news’ controversy was an amusing bonus.

      Having decided on the goal (indicate percent fake news), that left figuring out how to actually implement it.



Child Theme

      As the site runs on WordPress, the general layout is handled by the theme. While I could have built my own theme from scratch, all I wanted to do was make a minor change. Which is done with a Child Theme.

      Pretty sure you only need two files for a Child Theme, functions.php and style.css, but in my case I also have a third (sidebar.php). Functions and style are about as simple as they could be, basically say ‘go to parent theme for everything’. This part, including searching for how to do it, probably took all of five minutes. Was not hard.

      The sidebar was a little trickier. First I had to figure out exactly where to make the change, then what code was needed for the change. Is long enough ago that I’m not sure how many tries it took, but it wasn’t too hard to find the right spot. Could search for the approximate location by what was already showing on the pages, then add a few random letters to see if they show where I want them. Once that was right, then came the code.



PHP randomness

      The actual code was simple enough. Just needed a random number, between 10 and 90. Wanted to avoid the extreme percentages, as I felt the joke wouldn’t work as well with them.

      Below is the actual code snippet:
<h3 class=”widget-title”><?php echo(rand(10,90));?>% Fake News!</h3><br>

      Quick translation into English: That line says to pick a random number between 10 and 90, display that number as a percent, then add the text ‘Fake News’. In theory, each time a page is loaded, a different number is shown. In practice, it doesn’t change quiet that often (caching), but it changes often enough.

      A more detailed breakdown of the code: the ‘h3’ at the start says to treat it as a heading, the class says to use the ‘widget-title’ formatting. These have the text matching the other text it is near. the ?php starts the actual PHP portion, of which ‘echo‘ has it outputting text, ‘rand‘ has it picking a random number, and ‘10,90’ gives it the range for those random numbers. ‘% Fake News!’ is just text to display. The rest is closing formatting: close the PHP, close the h3, end the line with ‘br’.

      With that I had what I wanted, so mission accomplished.



      While not terrible difficult, I did feel a Moment of Pride for this accomplishment. All so that I can mock both myself and the media industry as a whole for being ‘Fake News’.

Leave a Reply