Using PHP arrays to Create and Edit Posts

To simplify creating or editing a post comprising a large amount of short snippets, use a PHP array. This is useful when you have a post with the same structural content repeated over and over; for example when containing multiple flex items all using the same CSS class.

It’s very easy to add to the content, and to experiment with the CSS class being applied should you want to, even if you’re unfamiliar with PHP.

To start with, you need to enable the inclusion and processing of PHP code within a post or page. I do this using the free PHP Code For Posts plugin. In writing this piece I’ve discovered that this plugin, which once had over two hundred thousand active installations, is no longer available from the WP repository. The above link, though, will tell you how you can get it, along with why it’s not on WordPress anymore. I like this plugin because it’s so very simple to use. You add it, activate it, and that’s it … you’re good to go. You can immediately start using PHP code directly in a post.

If you’re hesitant about using a plugin that isn’t from the repository, PHP Everywhere looks like a good alternative, from a brief scan of what it does at least. This plugin seems to enable the use of PHP in widgets, as well as in posts and pages, which I seem to recall the one I’m currently using doesn’t. I’ve added trying out this new plugin to my ‘To Do’ list when the opportunity or a need arises.

Once PHP is enabled, it’s a piece of cake to create the array. Let’s say I want to present three separate items within a flex-box division, with the same CSS applied to each:

<div class=”flex-grid”>

<?php

$randomname = array(

“Home is where you hang your @”,
“Windows will never cease”,
“What boots up must come down” );

foreach ($randomname as $randomname)

{ echo ‘<div class=”flex-item-3 proverbs”>’ . $randomname . ‘</div>’; }

?>

</div>

When executed, this code results in:

Home is where you hang your @
Windows will never cease
What boots up must come down

Once the basic code has been created, it’s very easy to add additional items to it, and to rearrange the order of presentation.

<div class=”flex-grid”>

<?php $randomname = array(

“Home is where you hang your @”,
“Virtual reality is its own reward”,
“Windows will never cease”,
“What boots up must come down”,
“The email of the species is deadlier than the mail”,
“A journey of 1000 sites begins with a single click”,
“Great groups from little icons grow” );

foreach($randomname as $randomname) { echo ‘<div class=”flex-item-3 proverbs”>’ . $randomname . ‘</div>’; } ?>

</div>

Home is where you hang your @
Virtual reality is its own reward
Windows will never cease
What boots up must come down
The email of the species is deadlier than the mail
A journey of 1000 sites begins with a single click
You can't teach an old mouse new clicks
Great groups from little icons grow

The biggest reward comes if you want to test out different CSS classes, to see how they’ll look. Instead of trawling through a long page of <div>s to change each occurrence, you need only change it once.

<div class=”flex-grid”>

<?php $randomname = array(

“Home is where you hang your @”,
“Virtual reality is its own reward”,
“Windows will never cease”,
“What boots up must come down”,
“The email of the species is deadlier than the mail”,
“A journey of 1000 sites begins with a single click”,
“You can't teach an old mouse new clicks”,
“Great groups from little icons grow”,
“Added to pad out the grid!” );

foreach($randomname as $randomname) { echo ‘<div class=”flex-item-s excuses paper-effect-blue“>’ . $randomname . ‘</div>’; } ?>

</div>

Home is where you hang your @
Virtual reality is its own reward
Windows will never cease
What boots up must come down
The email of the species is deadlier than the mail
A journey of 1000 sites begins with a single click
You can't teach an old mouse new clicks
Great groups from little icons grow
Added to pad out the grid!

Of course, there’s no huge benefit when you’re only dealing with three or four items, but what if it’s twenty-three, or twenty-four? Compare the two items below. On the left, a normal post entry, on the right, using this method. Both result in exactly the same output on the page.

<div class=”flex-grid”>

<div class=”flex-item-2-bl”>Item 01 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 01 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 02 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 03 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 04 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 05 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 06 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 07 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 08 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 09 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 10 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 11 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 12 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 13 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 14 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 15 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 16 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 17 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 18 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 19 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 20 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 21 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 22 with quite a lot of text, as it would probably be in real life</div>

<div class=”flex-item-2-bl”>Item 23 with quite a lot of text, as it would probably be in real life</div>

</div>

<div class=”flex-grid”>

<?php $randomname = array(

“Item 01 with quite a lot of text, as it would probably be in real life”,
“Item 01 with quite a lot of text, as it would probably be in real life”,
“Item 02 with quite a lot of text, as it would probably be in real life”,
“Item 03 with quite a lot of text, as it would probably be in real life”,
“Item 04 with quite a lot of text, as it would probably be in real life”,
“Item 05 with quite a lot of text, as it would probably be in real life”,
“Item 06 with quite a lot of text, as it would probably be in real life”,
“Item 07 with quite a lot of text, as it would probably be in real life”,
“Item 08 with quite a lot of text, as it would probably be in real life”,
“Item 09 with quite a lot of text, as it would probably be in real life”,
“Item 10 with quite a lot of text, as it would probably be in real life”,
“Item 11 with quite a lot of text, as it would probably be in real life”,
“Item 12 with quite a lot of text, as it would probably be in real life”,
“Item 13 with quite a lot of text, as it would probably be in real life”,
“Item 14 with quite a lot of text, as it would probably be in real life”,
“Item 15 with quite a lot of text, as it would probably be in real life”,
“Item 16 with quite a lot of text, as it would probably be in real life”,
“Item 17 with quite a lot of text, as it would probably be in real life”,
“Item 18 with quite a lot of text, as it would probably be in real life”,
“Item 19 with quite a lot of text, as it would probably be in real life”,
“Item 20 with quite a lot of text, as it would probably be in real life”,
“Item 21 with quite a lot of text, as it would probably be in real life”,
“Item 22 with quite a lot of text, as it would probably be in real life”,
“Item 23 with quite a lot of text, as it would probably be in real life” );

foreach($randomname as $randomname) {
echo ‘<div class=”flex-item-2-bl”>’ . $randomname . ‘</div>’; }
?>

</div>

Adding to the bottom of either entry is pretty straight-forward. To reorder it though, or add another item within the already existing text, the task becomes more onerous. Harder still when, in real life, the text of each item would likely be quite different. And what about trying a change of class? It needs to be changed twenty-three times in the traditional example, but only once in PHP. You could, of course, split the grid up and try a sample change on just a few lines of code, but that doesn’t truly show you the full end effect.

I’m sure that in time I’ll work out how to use this basic method in more complicated scenarios but for now, if you’re completely new to PHP, this simple example is probably enough to be going on with.

How annoying that I didn’t think of this till editing the very last entry I needed to!

Cautionary Note: In putting this page together, I discovered that the “$randomname” seems to need to be one long word. When I tried hyphenating it – “$random-name” – it caused a processing error. Thought I should mention this so you don’t make the same mistake and get scared off at the first hurdle! Precise syntax in PHP is very important. A bracket missing or in the wrong place, for example, will cause the script to fail and throw up an error.

0 0 vote
Rate this item
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments