Exchange Code Snippets

(Difference between revisions)
Jump to: navigation, search
(Created page with "Here, you'll find various snippets of code from iThemes Exchange.")
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Here, you'll find various snippets of code from iThemes Exchange.
+
Here you'll find various snippets of code from iThemes Exchange.
 +
 
 +
== Adding a field to the registration form from an add-on ==
 +
 
 +
'''Requires Exchange 1.1.0'''
 +
 
 +
<br />
 +
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
 +
 
 +
<br />
 +
 
 +
==== 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:
 +
<pre class="brush: php gutter:false">
 +
/**
 +
* 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) Splice 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' );
 +
</pre>
 +
 
 +
<br />
 +
 
 +
====  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
 +
 
 +
<br />
 +
 
 +
==== Step Three: Register your template folder with the Exchange template part API ====
 +
<pre class="brush: php gutter:false">
 +
/**
 +
* 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 );
 +
</pre>
 +
 
 +
 
 +
<br />
 +
[[:Exchange|← Back to Exchange Codex Home]]

Revision as of 13:40, July 31, 2013

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

Contents

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

Requires Exchange 1.1.0


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) Splice 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 );



← Back to Exchange Codex Home

Personal tools
Namespaces
Variants
Actions
iThemes Codex
Codex Navigation
Toolbox