Hotaru Docs

 [həʊdɒks]

Extra Post Fields in Submit

The Submit plugin for social bookmarking sites include plugin hooks to allow other plugins to add extra fields to step 2 of the post submission process.

To demonstrate how you can add extra post information, we'll use the Media Select plugin as an example. This plugin does the following:

  1. Add a post_media field to the database posts table
  2. Read the post field from the database
  3. Add radio buttons to step 2 of post submission and the edit_post template with choices News, Video, and Image
  4. Check for errors when a form is submitted or otherwise set the default value
  5. Update the database record when adding or updating a post

1. Add a post_media field to the database posts table

Here, we alter the posts table to include a new field.

/**
 * Add a post_media field to posts table if it doesn't already exist
 */
public function install_plugin($h)
{
    // Create a new table column called "post_media" if it doesn't already exist
    $exists = $h->db->column_exists('posts', 'post_media');
    if (!$exists) {
        $h->db->query("ALTER TABLE " . TABLE_POSTS . " ADD post_media VARCHAR(20) NOT NULL DEFAULT 'text' AFTER post_comments");
    } 
}

2. Read the post field from the database

The Hotaru Post class in the libs directory has a $vars property which we can use to store extra information. This saves us from having to edit core files to add new properties. In this case, we'll be adding the post's media type to $h->post->vars['media'].

/**
 * Read post media if post_id exists.
 */
public function post_read_post($h)
{
    if (!isset($h->post->vars['post_row']->post_media)) { return false; }
 
    $h->post->vars['media'] = $h->post->vars['post_row']->post_media;
}

3. Add radio buttons to step 2 of post submission and the edit_post template with choices News, Video, and Image

To help keep track of what a form has submitted, we store all the data in $h->vars['submitted_data']. That's especially useful when there's an error in the form and we need to populate the form fields again with the data the user has just entered. In this function, we first test to see if the form has already been submitted and whether "submit_media" exists in $h->vars['submitted_data'].

/**
 * Add a media field to submit form 2 and edit post page
 */
public function submit_2_fields($h)
{
    if (!isset($h->post->vars['media'])) { 
        if (isset($h->vars['submitted_data']['submit_media'])) { 
            $h->post->vars['media'] = $h->vars['submitted_data']['submit_media'];
        } else {
            $h->post->vars['media'] = 'text';
        }
    }
 
    switch ($h->post->vars['media']) {
        case 'video':
            $video = "checked"; $image = ""; $text = "";
            break;
        case 'image':
            $video = ""; $image = "checked"; $text = "";
            break;
        default:
            $video = ""; $image = ""; $text = "checked";
    }
 
    // radio buttons
 
    echo "<tr>\n";
 
        echo "<td>" . $h->lang["submit_form_media"] . "&nbsp; </td>\n";
        echo "<td colspan=2>\n";
 
        // news
        echo "<input type='radio' name='post_media' value='text' " . $text . " >";
        echo "&nbsp;&nbsp;" . $h->lang['media_select_text'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"; 
 
        // video
        echo "<input type='radio' name='post_media' value='video' " . $video . " >";
        echo "&nbsp;&nbsp;" . $h->lang['media_select_video'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"; 
 
        // image
        echo "<input type='radio' name='post_media' value='image' " . $image . " >";
        echo "&nbsp;&nbsp;" . $h->lang['media_select_image'] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"; 
 
    echo "</tr>\n";
}

4. Check for errors when a form is submitted or otherwise set the default value

This example uses radio buttons, so there's nothing that can go wrong. However, it's good practice to include a default value just in case. Here, we set $h->post->vars['media'] to "text". Notice, too, that this is where we add the user's choice to $h->vars['submitted_data'].

/**
 * Check and update post_submit in Submit step 2 and Post Edit pages
 */
public function submit_functions_process_submitted($h)
{
    if (($h->pageName != 'submit2') && ($h->pageName != 'edit_post')) { return false; }
 
    if ($h->cage->post->keyExists('post_media')) {
        $h->post->vars['media'] = $h->cage->post->getAlpha('post_media');
    } else {
        // use existing setting unless blank, in which case set default:
        if (!$h->post->vars['media']) { $h->post->vars['media'] = 'text'; } // default
    }
 
    $h->vars['submitted_data']['submit_media'] = $h->post->vars['media'];
}

5. Update the database record when adding or updating a post

These function update the post in the database with the latest choice of media. The two functions look the same, except for one key difference. The post_add_post function uses the newly created post ID, $h->post->vars['last_insert_id'], whereas the update function already knows the post ID which is stored as $h->post->id.

/**
 * Add media the posts table
 */
public function post_add_post($h)
{
	if ($h->post->type != 'news') { return false; }
 
    $h->post->vars['media'] = $h->vars['submitted_data']['submit_media'];
 
    $sql = "UPDATE " . TABLE_POSTS . " SET post_media = %s WHERE post_id = %d";
    $h->db->query($h->db->prepare($sql, $h->post->vars['media'] , $h->post->vars['last_insert_id']));
}
 
 
/**
 * Update media the posts table
 */
public function post_update_post($h)
{
	if (!isset($h->vars['submitted_data']['submit_media'])) { return false; }
 
    $h->post->vars['media'] = $h->vars['submitted_data']['submit_media'];
 
    $sql = "UPDATE " . TABLE_POSTS . " SET post_media = %s WHERE post_id = %d";
    $h->db->query($h->db->prepare($sql, urlencode(trim($h->post->vars['media'] )), $h->post->id));
}

And that's it. You've added a new field to the posts table and a new option to the post submission process.

Tip for advanced developers: Rather than add column after column to the posts table, consider using the postmeta table for rarely used post information.

Getting StartedDesign and LayoutPlugin DevelopmentAdvanced TopicsFunction ReferenceTroubleshooting