Skip to content

Instantly share code, notes, and snippets.

View trey8611's full-sized avatar
💭
listening to n*sync

Trey trey8611

💭
listening to n*sync
View GitHub Profile
@trey8611
trey8611 / import-woodmart-additional-variation-images.md
Last active May 3, 2024 17:45
[WoodMart Additional Variation Images]
function wpai_woodmart_gallery_image_imported( $post_id, $att_id, $filepath, $is_keep_existing_images = '' ) {
	$product = wc_get_product( $post_id );
	if ( ! $product ) return;

	$variationID_first = get_post_meta( $product->get_id(), wpai_woocommerce_add_on\XmlImportWooCommerceService::FIRST_VARIATION, TRUE );
	if ( $product->is_type( 'variation' ) || ! empty( $variationID_first ) ) {
		if ( empty( $variationID_first ) ) {
			$variationID = $product->get_id();
			$parent      = $product->get_parent_id();
@trey8611
trey8611 / phpexcel-object-add-row-to-excel-file.md
Last active November 6, 2021 14:18
[PHPExcel Object - Add row to XLSX file] #wpallimport

This is an example showing how to add a row to an Excel file when it's uploaded to WP All Import. Note that this code won't work in WP All Import's Function Editor, it must be in your child theme's functions.php file or in a Code Snippets plugin.

add_filter( 'wp_all_import_phpexcel_object', 'example_add_row_to_excel_file', 10, 2 );
function example_add_row_to_excel_file( $PHPExcel, $xlsFilePath ) {
	
	// Target the first sheet (because WPAI only references the first sheet; all other sheets are ignored)
	$PHPExcel->setActiveSheetIndex(0);
	
	// Append a new row after the last row in the sheet
@trey8611
trey8611 / php-check-if-category-is-in-mapping-rules.md
Created November 6, 2021 13:55
[Set Post Status based on Category Mapping Rules] #wpallimport

Pass a category to this function to check if it exists in the taxonomy mapping rules. If it does, set post status to 'publish', else set it to 'draft'.

function my_check_cats( $cat ) {
	$import_id = wp_all_import_get_import_id();
	$import    = new PMXI_Import_Record();
	$import->getById( $import_id );
	if ( $import->isEmpty() ) return 'publish';
	
	$mapping_rules = $import->options['tax_mapping']['product_cat'];
@trey8611
trey8611 / custom-column-direct-link-export.md
Created October 29, 2021 21:00
[Custom Columns on Manage Exports Page] Adds a "Direct Link" column with a direct link to the export file.
@trey8611
trey8611 / dynamic-attachment-export-elements.md
Created October 25, 2021 17:31
[Dynamic Attachment Export Elements] Output separate attachment elements per-attachment.

This snippet finds the post with the most attachments in the database, then outputs and populates those in the export columns.

The snippet will need to be adjusted if you want it to only output attachments that are images.

function wpae_output_attachment_headers( $headers, $export_id ) { 
    global $wpdb;
    $sql = "SELECT `ID`, `post_parent`, COUNT(*)
    FROM `wp_posts`
 WHERE `post_type` = 'attachment'
@trey8611
trey8611 / import-svi-variations-gallery-images.md
Created October 19, 2021 20:48
[WP All Import SVI Variations Gallery]
@trey8611
trey8611 / programmatically-update-unique-key.md
Last active April 19, 2023 03:28
[Programmatically update unique identifier] #wpallimport

Before using this snippet, it is really important to back-up your database so that you can roll back if needed. Also:

  • Note the $import_id checks in each function - you need to change '5' to the import ID that you want to target.
  • Note 'id' and {id[1]} in the functions. Change these to the element you want to become the new Unique Identifier.
add_action( 'pmxi_saved_post', 'my_update_uid', 10, 3 );
add_action( 'pmxi_after_xml_import', 'my_update_import_uid', 10, 2 );

function my_update_uid( $post_id, $xml_data, $is_update ) {
@trey8611
trey8611 / find-woocommerce-product-by-attribute.md
Last active October 25, 2022 15:04
Find WooCommerce Product by Attribute Name and Value

Some code inspiration borrowed from here: https://stackoverflow.com/a/40862030.

function my_find_product_by_attribute( $attribute_name, $attribute_value ) {    
	global $wpdb;
    // Find variation with CUSTOM attribute value
    $args_find_variation = array(
        'post_type'      => 'product_variation',
        'meta_query'     => array( array (
            'key'        => 'attribute_' . $attribute_name,
@trey8611
trey8611 / http-api-curl-options-custom.md
Last active March 29, 2025 21:10
[Change cURL Header and other options] Change timeout settings, headers, pass tokens, etc. #curl #download-from-url

You can use the http_api_curl hook to change timeout settings, headers, etc, when downloading a feed via URL with WP All Import.

There are example snippets below that you can modify for your use case. This code needs to be saved in your child theme's functions.php file, or in a plugin like Code Snippets.

Examples

Basic Authentication (base64_encode)

add_action( 'http_api_curl', 'example_set_curl_auth', 10, 3 );
function example_set_curl_auth( $handle, $r, $url ) {
@trey8611
trey8611 / wpml-export-multiple-translations.md
Last active August 16, 2021 19:36
[WPML - Export data from other translations] #wp-all-export #wpml #wpae

The following is an example showing how to export the titles in every WPML language when exporting your posts. It can be modified to get other data, like the content. The steps are:

  • Create an export of your posts from a single language.
  • Use custom PHP functions in the Custom XML Feed template to add the translation data to each exported property: https://d.pr/i/TD4D5j.

For example, if you were exporting the "en" language code, you could get the rest of the post titles by calling the function in the XML template like so:

<Titles>
	[my_get_titles({ID})]
</Titles>