Exchange Code Snippets

From IThemes Codex
Revision as of 12:14, July 15, 2013 by Glennansley (Talk | contribs)
Jump to: navigation, search

Here, you'll find various snippets of code from iThemes Exchange.

Contents

Adding a field to the registration form from an add-on

This process takes 3 steps:

  • Adding the template part to the registration form
  • Creating a template file in your add-on
  • Registering that template directory with Exchange

Step One: Add the field to the Registration Form’s field array

The registration form is generated by looping through an array of fields and pulling in a template part for each field. Add a filter to your add-on that pulls in the existing fields and adds your custom field to the array:

/**
 * This function adds our registration field to the list of fields included in the content-registration template part
 *
 * @since x.x.x
 *
 * @param array $fields existing fields
 * @return array
*/
function my_addon_add_field_to_content_registration_template_part( $fields ) { 

    /** 
     * We want to add our field right before the save button
     * 1) Find the save button
     * 2) Spice our value in right before the save button
     * 3) In the event that the save button wasn't found, just tack onto the end
    */

    $save_key = array_search( 'save', $fields );
    if ( false === $save_key )
        $fields[] = 'my-addon-field';
    else
        array_splice( $fields, $save_key, 0, array( 'my-addon-field' ) );

    return $fields;
}
add_filter( 'it_exchange_get_content_registration_field_details', 'my_addon_add_field_to_content_registration_template_part' );

Step Two: Create the template file in you’re add-on.

You’ll want to duplicate the directory structure found within core Exchange for the content-registration template-part: /your-addon/templates/content-registration/details/my-addon-field.php

Step Three: Register your template folder with the Exchange template part API

/**
 * This function tells Exchange to look in a directory in your add_on for template parts
 *
 * @since x.x.x
 *
 * @param array $template_paths existing template paths. Exchange core paths will be added after this filter.
 * @param array $template_names the template part names we're looking for right now.
 * @return array
*/
function my_addon_add_template_directory( $template_paths, $template_names ) { 

    /** 
     * Use the template_names array to target a specific template part you want to add
     * In this example, we're adding the following template part: content-registration/details/my-addon-field.php
     * So we're going to only add our templates directory if Exchange is looking for that part.
    */
    if ( ! in_array( 'content-registration/details/my-addon-field.php', $template_names ) ) 
        return $template_paths;

    /** 
     * If we are looking for the my-addon-field template part, go ahead and add our add_ons directory to the list
     * No trailing slash
    */
    $template_paths[] = dirname( __FILE__ ) . '/templates';

    return $template_paths;
}
add_filter( 'it_exchange_possible_template_paths', 'my_addon_add_template_directory', 10, 2 );
Personal tools
Namespaces
Variants
Actions
iThemes Codex
Codex Navigation
Toolbox