Archive for the ‘wordpress’ Category

To make a plugin for wordpress using RedFruits is very simple. In the wordpress’ forum was a question about this and we are using that example to show how simple is to make it.

In this example the developer wants to make a plugin that shows a form in a post. Ok, we are going to make a shortcode to show the form.

First of all is to make the plugin folder an file. The plugin’s name will be ‘FormInPost’.
You have to create a folder in the plugins folder called FormInPost and to create a file, inside it,called FormInPost.php. Write the next code in the new file:

<?php
/*
Plugin Name: FormInPost
Plugin URI: http://FormInPost.com
Description: FormInPost example!!
Author: RedFruits team
Version: 1.0
Author URI: https://redfruits.wordpress.com
*/
if (!defined('REDFRUITS_PATH')) define('REDFRUITS_PATH', dirname(dirname(__FILE__)).'/redfruits/');
if (!defined('FORMINPOST_PATH')) define('FORMINPOST_PATH', dirname(__FILE__).'/');
if (!defined('ADMIN_RENDER')) define('ADMIN_RENDER', 'ADWordpressRender');

require_once(REDFRUITS_PATH.'wp-extension/ADWPPlugin.class.php');

class FormInPost extends ADWPPlugin
{
	function __construct()
	{
		parent::__construct();
		$this->addShortCode('FormInPost', FORMINPOST_PATH.'shortcodes/FormInPostSC.class.php', 'FormInPostSC');
	}
}
new FormInPost();
?>

Now, the shortcode will be created in a folder (inside FormInPost folder). The new folder will be named like shortcodes. Inside it, you have to create FormInPostSC.class.php. You have to Write the next code in the new file:

<?php
class FormInPostSC extends ADLayer
{
	function render()
	{
		if (is_single()) //only shows when the post is being displayed, not in the category list
		{
			$this->setClassStyle('FormInPost');
			$form = $this->add(new ADForm('FormInPost_form'));
			$form->add(new ADLabeledField('First name:', new ADTextField('first_name', '', 20, 40)));
			$form->add(new ADLabeledField('Last name:', new ADTextField('last_name', '', 20, 40)));
			$form->add(new ADSubmit('FormInPost_save', 'save'));
			$form->setValues($_REQUEST);
			$form->addRequirement('first_name', 'required', 'the first name field must be completed');
			if (isset($_REQUEST['FormInPost_save']))
				if ($form->runRequirements($_REQUEST))
				{
					//TODO save the data
					global $post;
					echo 'TODO: Save data \'', $_REQUEST['first_name'], '\' and \'', $_REQUEST['last_name'], '\' in post id ',$post->ID, '';
				}
			return parent::render(ADMIN_RENDER);
		}
	}
}
?>

The most important thing is to make a class (RedFruits is an Object Oriented API) that implements ADWPPlugin.

The form in the second file draws in the explorer two Textboxes, first name and last name, and a submit button. the first name is a required field (see the addRequirement function). When the submit is pressed the page is reloaded and the if (isset($_REQUEST[‘FormInPost_save’])) is true. The runRequirements function test for the required field.

Remember to write [FormInPost] (see the first param in the addShortCode function) in the post where you want to show the form.

That’s all.

But now you want to change the look. This must be made with the renders. In next posts we are going to see how to make a render.

See you.