PHP array: how to create and extract from WordPress shortcode

By , last updated October 22, 2016

Problem

We have a website that is set up with WordPress and PHP where we are selling texture packs. Each pack has different number of images inside. We need to tell the PHP service what images are included in the pack.

WordPress shortcode

For the sake of our problem we have a number of image IDs that we need to send from the WordPress post to the PHP service. The easiest way is to use WordPress shortcodes. Shortcodes are macro codes that you can use in your content.

We are going to create our own custom shortcode for sending image id’s for each pack to the service.

Create content

First, we create the WordPress post. As a programmer with some experience I prefer descriptive names to everything and make 3 packs with images like this:

[texture_pack id=1 images='1, 2, 3']
[texture_pack id=2 images='4, 5, 6, 7 ,8']
[texture_pack id=3 images='9, 10, 11, 12, 13, 14, 15']

In this code sample “images” is an array of image id’s for each pack and “texture_pack” is a shortcode.

Shortcode function

Shortcode needs to be coded in a PHP file. When you are using WordPress you can easily insert the following code into your theme’s functions.php file (not recommended). It is recommended to make a plugin instead. Remember, PHP is not as object oriented as Java, but it’s still a programming language, thus rules of Clean Code need to be followed!

add_shortcode( 'texture_pack', 'create_texture_pack' );
function create_texture_pack( $attributes )
{
	//some code...
}

The function add_shortcode() links your content shortcode from the post to the function in PHP.

The best way of extending WordPress is to use plugins and / or create your own plugins. It’s not recommended to edit existing theme files and existing plugins. The changes will be overwritten when themes and plugins are updated with new features and security fixes. Your time is more worth than writing the same piece of code for each update or going through pages with differences between files.

PHP arrays

PHP arrays are very easy to use, almost too easy. They are actually maps, but can be easily used as plain arrays without specifying any keys. Other names are hashes and hashmaps.

Example of a simple array without keys:

<?php
$array = array("a", "b", "c", "d");
?>

PHP array as a map with keys and values:

<?php
$array = array(
"aKey" => "aValue",
"bKey" => "bValue");
?>

Read more in detail in the official PHP manual.

Extract attributes from the shortcode

Now, the parameter $attributes is the one containing “id” and an array of images in a form of a string. To get the data from the attributes you can use PHP extract() function:

function create_texture_pack( $attributes )
{
	extract( shortcode_atts( array(
		'id' => '0',
		'images' => array()
	), $attributes ) );
}

At this stage the parameter $images is a plain string that needs to be parsed. If you print the number of images at this stage you will get the following results:

images: 1
images: 1
images: 1

Exploding a string into an array

In order to get the actual PHP array of image id’s from the string we use PHP function explode() and provide what dividing sign we used (in my case ‘,’):

function create_texture_pack( $attributes )
{
	extract( shortcode_atts( array(
		'id' => '0',
		'images' => array()
	), $attributes ) );

	$images_arr = explode(",", $images);

	$numimages = count($images_arr);
	echo "images: $numimages";
}

Now the output will be correct:

images: 3
images: 5
images: 7

Now that the coding is done, the real difficult part is left: to make the design with HTML / CSS and JavaScript and make it look good.

Senior Software Engineer developing all kinds of stuff.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*