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 / media-export-import.md
Created October 30, 2024 16:52
Media Export / Import - Add "Media" to the WP All Export and WP All Import Drop-Downs

This code will add "Media" to WP All Export and WP All Import's drop-down options when choosing a post type to export/import. This allows you to directly edit specific fields for media library items, but it does not allow you to import new media library items.

The code needs to be saved in your child theme's functions.php file, or in a Code Snippets plugin.

 // Add "Media" to export drop-down
 
 add_filter( 'wpallexport_custom_types', 'my_add_custom_type', 10, 1 );
 
@trey8611
trey8611 / import-into-yith-swatches.md
Created July 17, 2024 18:40
Import swatches image into YITH WooCommerce Color, Image & Label Variation Swatches Premium

This snippet sets the swatch image for the "pa_shade" attribute assigned to each variation. Read the comments for tips on customizing it for other attributes and restricting it to a certain import ID.

# ************************************************************************************************************
# * Import swatches image for the "YITH WooCommerce Color, Image & Label Variation Swatches Premium" plugin. *
# ************************************************************************************************************

add_action( 'wp_all_import_variable_product_imported', 'wpai_wp_all_import_variable_product_imported', 10, 1 );
function wpai_wp_all_import_variable_product_imported( $post_parent ) {
@trey8611
trey8611 / export-jetengine-relations.md
Created March 20, 2024 20:14 — forked from juanlistab/export-jetengine-relations.md
Export JetEngine Relations with WP All Import

How to Export JetEngine Relations

This function will use the default "jet_rel_default" table in JetEngine to look for the post_id of the parent and return its relations from the "child_object_id" column. If you're using a separate table for your relations, you can change the $table_name value.

Instructions:

  • Add a New Field in WP All Export (see https://d.pr/i/OW9dze).
  • Set a name for the column and use "ID" for the field to export (see https://d.pr/i/v5Mq98).
  • Modify the function as needed, paste it in the Function Editor and paste the name of the function in the "Export the value returned by a PHP function" field (see https://d.pr/i/2c6c07).
  • The function will return a list of IDs separated with pipes by default, but if you want to export the titles instead, you need to change the $mode in "function my_export_je_relation ($post_id, $mode = 'id')" to "title".
@trey8611
trey8611 / import-jetengine-relations.md
Created March 20, 2024 20:13 — forked from juanlistab/import-jetengine-relations.md
Import Data to JetEngine Relations

How to Import JetEngine Relations

This snippet will work for One to One, One to Many and Many to Many relationships:

Instructions:

  • Create a "_temp_child" temporary field to store the values of the relations you want to import (see https://d.pr/i/cIooGf). It will accept multiple values separated with commas, and you can use either the ID or the Title.
  • Edit the snippet using the instructions below and paste it in the Function Editor inside WP All Import.
  • The "jet_rel_default" table is the default table used for relations in JetEngine. If you're using a separate table, you'll need to set that table's name instead.
  • You'll need to specify the relationship ID in 'rel_id’; you can find it here (see https://d.pr/i/eSVZ32).
@trey8611
trey8611 / remove-image-sizes-from-url.md
Last active April 17, 2024 17:24
Remove image sizes from image URLs in content

Remove -000x000.ext from image URLs in content by passing the content to a custom PHP function:

[my_fix_images({content[1]})]

Code (save in the Function Editor at All Import › Settings):

function my_fix_img( $img = '' ) {
@trey8611
trey8611 / remove-duplicats-xpath.md
Created July 15, 2022 20:03
[Remove Duplicates via XPath 1.0]

If you have a variations XPath like this:

{variations/variant[*]}
<variations>
  <variant>
 V1
@trey8611
trey8611 / chain-imports-exports-together.md
Created March 12, 2022 14:59
[Chain Cron Imports / Exports Together] Sequential Cron Imports/Exports

Cron Job Imports: http://www.wpallimport.com/documentation/recurring/cron/.

If you want to run imports or exports sequentially, you can use this workaround:

  • Set up the "trigger" cron job and "processing" cron job for the import/export that should run first.
  • Set up "processing" cron jobs for all of the imports/exports that are going to run.
  • Modify the following code by adding your own URL and import/export IDs then save the code inside the Function Editor:
function my_run_cron_after_import( $import_id ) {
@trey8611
trey8611 / import-woocommerce-rest-api-feed.md
Created February 21, 2022 19:21
[Import WooCommerce Rest API Product Feed] #wpallimport #woocommerce #restapi

This function can be saved in your child theme's functions.php file, or in a code snippets plugin ( https://wordpress.org/plugins/code-snippets/ ), then used in WP All Import to download a product feed via the WooCommerce REST API. Things that need to be/can be changed are:

  • In the function call, change "100" to the amount of product you want per-page request.
  • In the code, change the $max_pages value from 3 to however many pages you want to fetch.
  • In the code, change the $key and $sec (secret value) values to the correct ones from your WooCo API key details.

Usage (in the Download a file › From URL field in WP All Import):

[my_fetch_products("https://example.com/wp-json/wc/v3/products?per_page=100","json")]
@trey8611
trey8611 / import-variation-swatches.md
Created February 18, 2022 21:06
[WooCommerce Variation Swatches and Photos] #wpallimport #woocommerce #swatches

This is an example snippet that imports swatches added by this plugin: https://woocommerce.com/products/variation-swatches-and-photos/. This example only works with the variation attribute "Colour" and it uses the images uploaded to the variations as the swatch images. It is highly likely that you will need to modify it for your specific use case.

add_action( 'wp_all_import_variable_product_imported', 'my_set_swatches', 10, 1 );


function my_set_swatches( $id ) {
	$product = wc_get_product( $id );
	if ( ! $product ) return;
	if ( ! $product->is_type( 'variable' ) ) return;
@trey8611
trey8611 / upload-embedded-media.md
Created January 8, 2022 18:58
[Upload embedded media to WordPress Media Library] #phpexcel

This snippet uploads all images embedded in the import file to the WordPress Media Library. Hat tip to https://stackoverflow.com/a/10910199 for the code inspiration.

add_filter( 'wp_all_import_phpexcel_object', 'wpai_wp_all_import_phpexcel_object', 10, 2 );

function wpai_wp_all_import_phpexcel_object( $objPHPExcel, $xlsFilePath ) {
    $i = 0;
    foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
        if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
            ob_start();