-
-
Save tommcfarlin/842b94790b2f8a8fcb9cf8364ef4b2f2 to your computer and use it in GitHub Desktop.
[WordPress] Creating Custom WooCommerce Input Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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