Following our Games as Applets series, we want to talk about the Wordrpess Page Template we are using in order to deploy our games as Java Applets inside the Blog.

WordPress Custom Fields

When creating a new blog post or page, the creator can add metadata using WordPress Custom Fields, to be processed later by the PHP templates associated. In our case, we are using the following custom fields:

  • applet_height - height of the applet.
  • applet_width - width of the applet.
  • applet_screenshot - URL to a screenshot of the game
  • jnlp_url - URL to the JNLP for launching the game.

WordPress Page Templates

After adding metadata to our posts or pages, we need to make a WordPress Page Template in order to process it. In our case, we created a PHP template for our games deployed as Java Applets using the structure explained in our last post about Games as Applets. Our games template looks like this file.

Note: I didn’t want to put all the PHP file code directly in the post because it is too large.

This is how we get the post metadata:

<?php 
	$jnlp_href = get_post_meta($post->ID, "jnlp_url", true);
	$applet_width = get_post_meta($post->ID, "applet_width", true); 
	$applet_height = get_post_meta($post->ID, "applet_height", true);  
	$applet_screenshot = get_post_meta($post->ID, "applet_screenshot", true);  
?>

And this is how we pass the values to the javascript:

<?php echo '

' ?>

Once the page template is created, if we want to add a new page for a game, we only have to select it from the templates list and voilá.

Conclusion

Using WordPress Page Templates and Custom Fields, we can reduce the information of the game’s page to game’s related information only, and move all common logic and information between game pages to the page template.

This is how it looks to edit a game page:
Game Page being Edited

Note: there is no applet tag or javascript in the page.

Also, if we modify the page template, we have all game pages updated, that could be really useful if you need to fix a bug or make an improvement to the page but could be a problem in case we introduce a new bug.

One problem not resolved yet is if you want to change the WordPress Theme, you will have to migrate the page template. I don’t know exactly if you can create a page template theme independent.

References