Skip to content
This repository has been archived by the owner on Oct 13, 2022. It is now read-only.

Commit

Permalink
Improved structure and added logic to avoid duplicate function/class …
Browse files Browse the repository at this point in the history
…failure

#28
  • Loading branch information
elliotcondon committed Feb 16, 2016
1 parent e3e4b4f commit a28f496
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 27 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ http://www.advancedcustomfields.com/resources/tutorials/creating-a-new-field-typ

### Structure

* `/css`: folder for .css files.
* `/images`: folder for image files
* `/js`: folder for .js files
* `/assets`: folder for all asset files.
* `/assets/css`: folder for .css files.
* `/assets/images`: folder for image files
* `/assets/js`: folder for .js files
* `/fields`: folder for all field class files.
* `/fields/FIELD_NAME-v5.php`: Field class compatible with ACF version 5
* `/fields/FIELD_NAME-v4.php`: Field class compatible with ACF version 4
* `/lang`: folder for .pot, .po and .mo files
* `acf-FIELD_NAME.php`: Main plugin file that includes the correct field file based on the ACF version
* `FIELD_NAME-v5.php`: Field class compatible with ACF version 5
* `FIELD_NAME-v4.php`: Field class compatible with ACF version 4
* `readme.txt`: WordPress readme file to be used by the WordPress repository

### step 1.
Expand Down
75 changes: 53 additions & 22 deletions acf-FIELD_NAME.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,70 @@
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/

// exit if accessed directly
if( ! defined( 'ABSPATH' ) ) exit;


// check if class already exists
if( !class_exists('acf_plugin_FIELD_NAME') ) :

// 1. set text domain
// Reference: https://codex.wordpress.org/Function_Reference/load_plugin_textdomain
load_plugin_textdomain( 'acf-FIELD_NAME', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );




// 2. Include field type for ACF5
// $version = 5 and can be ignored until ACF6 exists
function include_field_types_FIELD_NAME( $version ) {
class acf_plugin_FIELD_NAME {

include_once('acf-FIELD_NAME-v5.php');
/*
* __construct
*
* This function will setup the class functionality
*
* @type function
* @date 17/02/2016
* @since 1.0.0
*
* @param n/a
* @return n/a
*/

}

add_action('acf/include_field_types', 'include_field_types_FIELD_NAME');




// 3. Include field type for ACF4
function register_fields_FIELD_NAME() {
function __construct() {

// set text domain
// https://codex.wordpress.org/Function_Reference/load_plugin_textdomain
load_plugin_textdomain( 'acf-FIELD_NAME', false, plugin_basename( dirname( __FILE__ ) ) . '/lang' );


// include field
add_action('acf/include_field_types', array($this, 'include_field_types')); // v5
add_action('acf/register_fields', array($this, 'include_field_types')); // v4

}


/*
* include_field_types
*
* This function will include the field type class
*
* @type function
* @date 17/02/2016
* @since 1.0.0
*
* @param $version (int) major ACF version. Defaults to 4
* @return n/a
*/

include_once('acf-FIELD_NAME-v4.php');
function include_field_types( $version = 4 ) {

// include
include_once('fields/acf-FIELD_NAME-v' . $version . '.php');

}

}

add_action('acf/register_fields', 'register_fields_FIELD_NAME');

// initialize
new acf_plugin_FIELD_NAME();


// class_exists check
endif;

?>
5 changes: 5 additions & 0 deletions assets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# assets directory

Use this directory to store asset files such as CSS, JS and images.

This directory can be removed if not used.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a28f496

Please sign in to comment.