Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active March 6, 2017 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tommcfarlin/842b94790b2f8a8fcb9cf8364ef4b2f2 to your computer and use it in GitHub Desktop.
Save tommcfarlin/842b94790b2f8a8fcb9cf8364ef4b2f2 to your computer and use it in GitHub Desktop.
[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