Skip to content

Instantly share code, notes, and snippets.

@PeteSchuster
Created February 8, 2013 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeteSchuster/4739183 to your computer and use it in GitHub Desktop.
Save PeteSchuster/4739183 to your computer and use it in GitHub Desktop.
Title separator add on for WordPress Plugin Advanced Custom Fields
<?php
/*
* Advanced Custom Fields - New field template
*
* Create your field's functionality below and use the function:
* register_field($class_name, $file_path) to include the field
* in the acf plugin.
*
* Documentation:
*
*/
class ps_separator extends acf_Field {
/*--------------------------------------------------------------------------------------
*
* Constructor
* - This function is called when the field class is initalized on each page.
* - Here you can add filters / actions and setup any other functionality for your field
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function __construct($parent) {
// do not delete!
parent::__construct($parent);
// set name / title
$this->name = 'separator'; // variable name (no spaces / special characters / etc)
$this->title = __("Separator",'acf'); // field label (Displayed in edit screens)
} //end __construct
/*--------------------------------------------------------------------------------------
*
* create_options
* - this function is called from core/field_meta_box.php to create extra options
* for your field
*
* @params
* - $key (int) - the $_POST obejct key required to save the options to the field
* - $field (array) - the field object
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function create_options($key, $field) {
// vars
$defaults = array(
'textvalue' => '',
'subjvalue' => '',
'htag' => 'h1'
);
$field = array_merge($defaults, $field);
$field['textvalue'] = isset($field['textvalue']) ? $field['textvalue'] : '';
$field['subjvalue'] = isset($field['subjvalue']) ? $field['subjvalue'] : '';
$field['htag'] = isset($field['htag']) ? $field['htag'] : '';
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Separator Title",'acf'); ?></label>
<p class="description"><?php _e('Title to go in separater. eg. "This is Title".','acf'); ?></a></p>
</td>
<td>
<?php
$this->parent->create_field(array(
'type' => 'text',
'name' => 'fields['.$key.'][textvalue]',
'value' => $field['textvalue'],
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Separator Subject",'acf'); ?></label>
<p class="description"><?php _e(' Subject to go below title. eg. "Subject Title".','acf'); ?></a></p>
</td>
<td>
<?php
$this->parent->create_field(array(
'type' => 'text',
'name' => 'fields['.$key.'][subjvalue]',
'value' => $field['subjvalue'],
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Title Size",'acf'); ?></label>
<p class="description"><?php _e("Define how to render Header tags. If Default ACF is selected, title color, custom css, and background color will NOT work.",'acf'); ?></p>
</td>
<td>
<?php
$this->parent->create_field(array(
'type' => 'select',
'name' => 'fields['.$key.'][htag]',
'value' => $field['htag'],
'choices' => array(
'h1' => __("H1",'acf'),
'h2' => __("H2",'acf'),
'h3' => __("H3",'acf'),
'h4' => __("H4",'acf'),
'h5' => __("H5",'acf')
)
));
?>
</td>
</tr>
<?php
} // end create_options
/*--------------------------------------------------------------------------------------
*
* pre_save_field
* - this function is called when saving your acf object. Here you can manipulate the
* field object and it's options before it gets saved to the database.
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function pre_save_field($field) {
// do stuff with field (mostly format options data)
return parent::pre_save_field($field);
} //end pre_save_field
/*--------------------------------------------------------------------------------------
*
* create_field
* - this function is called on edit screens to produce the html for this field
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function create_field($field) {
$htag = $field['htag'];
$htagstyle = '';
if ($field['subjvalue'] != ""){
$subjvalue = '<div><p style="margin: 0" class="label">'.$field['subjvalue'].'</p></div>';
} //end if
if ($htag == "h3"){
$htag = "h6";
$htagstyle = ' style="font-size:15px !important;" ';
} //end if
//Show all renders
echo '<'.$htag.' class="hndle"'.$htagstyle.'"><span>'.$field['textvalue'].'</span></'.$htag.'>';
echo $subjvalue;
} // end create_field
/*--------------------------------------------------------------------------------------
*
* admin_head
* - this function is called in the admin_head of the edit screen where your field
* is created. Use this function to create css and javascript to assist your
* create_field() function.
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function admin_head() {
//Hide Title
?>
<style type="text/css">
.field-separator > p.label { display: none !important; }
</style>
<?php
} // end admin_head
/*--------------------------------------------------------------------------------------
*
* admin_print_scripts / admin_print_styles
* - this function is called in the admin_print_scripts / admin_print_styles where
* your field is created. Use this function to register css and javascript to assist
* your create_field() function.
*
* @author Elliot Condon
* @since 3.0.0
*
*-------------------------------------------------------------------------------------*/
function admin_print_scripts() {
} // end admin_print_scripts
function admin_print_styles() {
} //end admin_print_styles
/*--------------------------------------------------------------------------------------
*
* update_value
* - this function is called when saving a post object that your field is assigned to.
* the function will pass through the 3 parameters for you to use.
*
* @params
* - $post_id (int) - usefull if you need to save extra data or manipulate the current
* post object
* - $field (array) - usefull if you need to manipulate the $value based on a field option
* - $value (mixed) - the new value of your field.
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function update_value($post_id, $field, $value) {
// do stuff with value
// save value
parent::update_value($post_id, $field, $value);
} // end update_value
/*--------------------------------------------------------------------------------------
*
* get_value
* - called from the edit page to get the value of your field. This function is useful
* if your field needs to collect extra data for your create_field() function.
*
* @params
* - $post_id (int) - the post ID which your value is attached to
* - $field (array) - the field object.
*
* @author Elliot Condon
* @since 2.2.0
*
*-------------------------------------------------------------------------------------*/
function get_value($post_id, $field) {
// get value
////$value = parent::get_value($post_id, $field);
// format value
// return value
////return $value;
$value = parent::get_value($post_id, $field);
$value = htmlspecialchars($value, ENT_QUOTES);
return $value;
} //end get_value
/*--------------------------------------------------------------------------------------
*
* get_value_for_api
* - called from your template file when using the API functions (get_field, etc).
* This function is useful if your field needs to format the returned value
*
* @params
* - $post_id (int) - the post ID which your value is attached to
* - $field (array) - the field object.
*
* @author Elliot Condon
* @since 3.0.0
*
*-------------------------------------------------------------------------------------*/
function get_value_for_api($post_id, $field) {
// get value
////$value = $this->get_value($post_id, $field);
// format value
// return value
////return $value;
} // end get_value_for_api
} //end class ps_separator
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment