Skip to content

Instantly share code, notes, and snippets.

@pippinsplugins
Created May 7, 2014 20:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pippinsplugins/92e53cc53649a922a10e to your computer and use it in GitHub Desktop.
Save pippinsplugins/92e53cc53649a922a10e to your computer and use it in GitHub Desktop.
Sample custom field for EDD FES
<?php
// Begin FES integration
// add button to the post button listing
function fes_shipping_field_button( $title ) {
if ( version_compare( fes_plugin_version, '2.2', '>=' ) ) {
echo '<button class="fes-button button" data-name="edd_simple_shipping" data-type="action" title="' . $title . '">'. __( 'Shipping', 'edd-simple-shipping' ) . '</button>';
}
}
add_action('fes_custom_post_button', 'fes_shipping_field_button');
function fes_admin_field_save( $field_id, $label = "", $values = array() ) {
if( ! isset( $values['label'] ) ) {
$values['label'] = __( 'Shipping', 'edd-simple-shipping' );
}
$values['no_css'] = true;
$values['is_meta'] = true;
$values['name'] = 'edd_simple_shipping';
?>
<li class="edd_simple_shipping">
<?php FES_Formbuilder_Templates::legend( $values['label'] ); ?>
<?php FES_Formbuilder_Templates::hidden_field( "[$field_id][input_type]", 'edd_simple_shipping' ); ?>
<?php FES_Formbuilder_Templates::hidden_field( "[$field_id][template]", 'edd_simple_shipping' ); ?>
<div class="fes-form-holder">
<?php FES_Formbuilder_Templates::common( $field_id, 'edd_simple_shipping', false, $values ); ?>
</div> <!-- .fes-form-holder -->
</li>
<?php
}
add_action( 'fes_admin_field_edd_simple_shipping', 'fes_admin_field_save', 10, 3);
function edd_simple_shipping_formbuilder_is_custom_field( $bool, $template_field ) {
if ( $bool ) {
return $bool;
} else if ( isset( $template_field['template'] ) && $template_field['template'] == 'edd_simple_shipping' ) {
return true;
} else {
return $bool;
}
}
add_filter('fes_formbuilder_custom_field', 'edd_simple_shipping_formbuilder_is_custom_field', 10, 2);
function edd_simple_shipping_save_custom_fields( $post_id ) {
if ( isset( $_POST ['edd_simple_shipping'] ) ) {
$domestic = ! empty( $_POST ['edd_simple_shipping']['domestic'] ) ? edd_sanitize_amount( $_POST ['edd_simple_shipping']['domestic'] ) : 0;
$international = ! empty( $_POST ['edd_simple_shipping']['international'] ) ? edd_sanitize_amount( $_POST ['edd_simple_shipping']['international'] ) : 0;
update_post_meta( $post_id, '_edd_enable_shipping', '1' );
update_post_meta( $post_id, '_edd_shipping_domestic', $domestic );
update_post_meta( $post_id, '_edd_shipping_international', $international );
} else {
delete_post_meta( $post_id, '_edd_enable_shipping' );
}
}
add_action( 'fes_submission_form_save_custom_fields', 'edd_simple_shipping_save_custom_fields' );
function edd_simple_shipping_field( $attr, $post_id, $type ) {
if ( isset( $attr['required'] ) && $attr['required'] == 'yes' ) {
$required = apply_filters( 'fes_required_class', ' edd-required-indicator', $attr );
}
$enabled = get_post_meta( $post_id, '_edd_enable_shipping', true );
$domestic = get_post_meta( $post_id, '_edd_shipping_domestic', true );
$international = get_post_meta( $post_id, '_edd_shipping_international', true );
?>
<div class="fes-fields <?php echo sanitize_key( $attr['name']); ?>">
<label for="edd_simple_shipping[enabled]">
<input type="checkbox" name="edd_simple_shipping[enabled]" id="edd_simple_shipping[enabled]" value="1"<?php checked( '1', $enabled ); ?>/>
<?php _e( 'Enable Shipping', 'edd-simple-shipping' ); ?>
</label>
<input class="textfield<?php echo esc_attr( $required ); ?>" id="edd_simple_shipping[domestic]" type="text" data-required="<?php echo $attr['required'] ?>" data-type="text" name="<?php echo esc_attr( $attr['name'] ); ?>[domestic]" placeholder="<?php echo __( 'Enter the domestic shipping charge amount', 'edd-simple-shipping' ); ?>" value="<?php echo esc_attr( $domestic ) ?>" size="10" />
<input class="textfield<?php echo esc_attr( $required ); ?>" id="edd_simple_shipping[international]" type="text" data-required="<?php echo $attr['required'] ?>" data-type="text" name="<?php echo esc_attr( $attr['name'] ); ?>[international]" placeholder="<?php echo __( 'Enter the international shipping charge amount', 'edd-simple-shipping' ); ?>" value="<?php echo esc_attr( $international ) ?>" size="10" />
</div> <!-- .fes-fields -->
<?php
}
add_action('fes_render_field_edd_simple_shipping', 'edd_simple_shipping_field', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment