Games as Applets 3

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

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Tags: , , , ,

  • http://huyz.us/ huyz

    I'm not sure how useful this would be in your case because you may have some specific game-related needs.  I just created a new plugin for WordPress which allows you to embed any Java applet inside a self-hosted WordPress blog using shortcodes.

    http://huyz.us/2011/the-easiest-way-to-embed-java-in-wordpress/

  • arielsan

    As you said, we have some specific game needs, like avoid starting the game when the page loads, between others. We should make a WordPress plug-in instead having some php page templates, or maybe use yours if it matches our needs.

  • http://huyz.us/ huyz

    The plugin also has an option to only start the JVM when the user clicks.

    You can fork the plugin at https://github.com/huyz/wp-java-applet-embed .
    Btw, I forked someone else's plugin too, so I can tell you from experience that the code is easy to hack.