Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Created July 29, 2014 17:38
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 BFTrick/31de2d2235b924e853b0 to your computer and use it in GitHub Desktop.
Save BFTrick/31de2d2235b924e853b0 to your computer and use it in GitHub Desktop.
Generate a custom button within the Integration tab.
/**
* Initialize integration settings form fields.
*
* @return void
*/
public function init_form_fields() {
$this->form_fields = array(
// don't forget to put your other settings here
'customize_button' => array(
'title' => __( 'Customize!', 'woocommerce-integration-demo' ),
'type' => 'button',
'custom_attributes' => array(
'onclick' => "location.href='http://www.woothemes.com'",
),
'description' => __( 'Customize your settings by going to the integration site directly.', 'woocommerce-integration-demo' ),
'desc_tip' => true,
)
);
}
/**
* Generate Button HTML.
*
* @access public
* @param mixed $key
* @param mixed $data
* @since 1.0.0
* @return string
*/
public function generate_button_html( $key, $data ) {
$field = $this->plugin_id . $this->id . '_' . $key;
$defaults = array(
'class' => 'button-secondary',
'css' => '',
'custom_attributes' => array(),
'desc_tip' => false,
'description' => '',
'title' => '',
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
<?php echo $this->get_tooltip_html( $data ); ?>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<button class="<?php echo esc_attr( $data['class'] ); ?>" type="button" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php echo $this->get_custom_attribute_html( $data ); ?>><?php echo wp_kses_post( $data['title'] ); ?></button>
<?php echo $this->get_description_html( $data ); ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
@techrah
Copy link

techrah commented Jun 30, 2015

What links the form field to the generate_button_html function? I don't see anything in the form field configuration that says "use the function generate_button_html" to render the button.

@tharmann
Copy link

I was wondering about this too so I looked over the source code and found this in the "generate_settings_html" function:

if ( method_exists( $this, 'generate_' . $v['type'] . '_html' ) ) {
                $html .= $this->{'generate_' . $v['type'] . '_html'}( $k, $v );
            } else {
                $html .= $this->{'generate_text_html'}( $k, $v );
}

So it looks like the function matches your field "type" with a function of "generate_yourtype_html".

We just need to make sure we have a function that matches our custom type then.

I either missed that in the tutorial or our friends forgot to explain that..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment