Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin

tommcfarlin/00-init.php Secret

Last active Mar 6, 2017
Embed
What would you like to do?
[WordPress] Creating Custom WooCommerce Input Field
<?php
public function init() {
add_action(
'woocommerce_product_options_grouping',
array( $this, 'product_options_grouping' )
);
add_action(
'woocommerce_process_product_meta',
array( $this, 'add_custom_linked_field_save' )
);
}
<?php
public function product_options_grouping() {
// Generic, I know.
$description = sanitize_text_field(
'Enter information relevant to the customer of this product.'
);
$args = array(
'id' => 'acme_text_id',
'label' => sanitize_text_field( 'Product IDs' ),
'placeholder' => '',
'desc_tip' => true,
'description' => $description,
);
woocommerce_wp_text_input( $args );
}
<?php
public function add_custom_linked_field_save( $post_id ) {
if ( ! ( isset( $_POST['woocommerce_meta_nonce'], $_POST[ 'acme_text_id' ] ) || wp_verify_nonce( sanitize_key( $_POST['woocommerce_meta_nonce'] ), 'woocommerce_save_data' ) ) ) { // Input var okay.
return false;
}
// This is where you can take it a step further to add your own validation.
$ids = sanitize_text_field(
wp_unslash( $_POST['acme_text_id'] ) // Input var okay.
);
update_post_meta(
$post_id,
'acme_text_id',
esc_attr( $ids )
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment