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 / data_feed.php
Last active January 18, 2019 19:47
#proxyscript
<?php
/*
################### READ ME #################################
You must create a proxy file that retrieves your import file from FTP and then returns it.
You will then call that file with the 'Download from URL' option in WP All Import.
This is an example of such a proxy file. This is provided in the hopes it is useful, but without any support.
###############################################################
Instructions:
* Copy the code below to a new php file in the root of your WP site
* Update the FTP server details
@trey8611
trey8611 / gist:3dcca4d76d681c05342b131d65de8e1d
Created July 26, 2017 21:02
Get all image src values from HTML
// Get all image src from HTML content
function my_get_images_from_content( $html ) {
$doc = new DOMDocument();
@$doc->loadHTML( $html );
$imageTags = $doc->getElementsByTagName( 'img' );
$all_images = array();
foreach( $imageTags as $tag ) {
$all_images[] = $tag->getAttribute( 'src' );
}
@trey8611
trey8611 / imagediff.txt
Last active August 9, 2017 14:52
Check if images are different
function are_images_diff( $img1, $img2 ) {
$md5image1 = md5( file_get_contents( $img1 ) );
$md5image2 = md5( file_get_contents( $img2 ) );
if ( $md5image1 == $md5image2 ) {
return "They are the same.";
} else {
return "They are different.";
}
}
@trey8611
trey8611 / export-from-custom-db-tables.md
Last active June 14, 2023 09:12
WP All Export - export data from custom database tables

Sometimes you might need to export extra data from a custom database table. This is possible by using a custom PHP function in the export along with the WPDB class.

For example, let’s say that we are exporting Users from a BuddyPress installation and we need to get the value of the “Name” field from the BuddyPress “Extended Profile” section: https://d.pr/6kZfA8. This information is stored inside the ‘bp_xprofile_data’ table: https://d.pr/CTdjb2. So, to export this, we’ll want to:

  • Click “Add Field” in our export, keep “ID” in the top drop-down
  • Name the field, for example “BuddyPress Name”
  • Click “Export the value returned by a PHP function"
  • Type in our function name, add the code to the function editor, and click “Done”.

Example video: https://drive.google.com/file/d/0BxSOi52PDCsAZ1JXazB0RzFFTnc/view?usp=sharing&resourcekey=0-OOnGF0Zes40Qqn6iEvFtIQ

WordPress doesn't allow multiple users to share the same e-mail address, but there are certain plugins that change that behavior. This is an example workaround using the Allow Multiple Accounts plugin.

  1. Install and activate this plugin: https://wordpress.org/plugins/allow-multiple-accounts/

  2. Use a custom PHP function in the import to generate a fake e-mail for the imported user(s): [fake_email()]. Screenshot: https://d.pr/FREE/Yr0MOL.

  3. Store the real e-mail in a Custom Field named '_email_temp'. Screenshot: https://d.pr/ERw4F9

  4. Use our API to change the users e-mail to the real e-mail after the user is saved.

<?php
function my_search_media_library( $image ) {
global $wpdb;
$all_images = array();
$query = "SELECT * FROM " . $wpdb->posts . " WHERE guid LIKE '%" . $image . "%'";
$results = $wpdb->get_results( $query );
if ( $results ) {
foreach ( $results as $result ) {
@trey8611
trey8611 / query_cyrillic_attribute_values.md
Last active April 17, 2024 17:23
WP All Import - use XPath Query based on Cyrillic attribute value

XPath doesn't allow you to make queries with Cyrillic symbols unless you disable XML pre-processing by adding this code in your child themes functions.php file (or in a plugin like Code Snippets: https://wordpress.org/plugins/code-snippets/):

function wpai_is_xml_preprocess_enabled( $is_enabled ) {
	return false;
}
add_filter( 'is_xml_preprocess_enabled', 'wpai_is_xml_preprocess_enabled', 10, 1 );

Once that code is in place, upload your file to an import and queries like this will be possible:

<?php
add_action( 'pmxi_saved_post', 'my_update_stock', 10, 3 );
function my_update_stock( $order_id, $xml_data, $is_update ) {
$import_id = ( isset( $_GET['id'] ) ) ? $_GET['id'] : $_GET['import_id'];
if ( $import_id == "7" && $is_update == false ) { // change "7" to your import ID
if ( $order = wc_get_order( $order_id ) ) {
$items = $order->get_items();
@trey8611
trey8611 / adjust-wooco-variation-threshold.md
Last active April 9, 2024 11:53
Adjust WooCommerce Variation Threshold

By default, WooCommerce only loads 30 variations on the front-end for variable products. If your product has more variations than that, WooCommerce will not fetch them until they're selected in the drop-downs - which means that unavailable variations will show up in the drop-downs.

If you don't want WooCommerce to display unavailable options in the drop-downs, you'll have to adjust the WooCommerce AJAX Variations Threshold to be more than the amount of variations your product has.

Here's an example snippet that tells WooCommerce to load 100 variations:

function soflyy_change_threshold( $amount, $product ) {
 return 100; // change this to the amount of variations you want to load
<?php
/*
################### READ ME #################################
You'll pass the URL to your feed/file to this function inside the "Download from URL" option when creating an import.
Image examples: https://d.pr/hCfNek and https://d.pr/MnerNb.
1. [custom_file_download("ftp://username:password@hostname.com/full/path/to/file.csv","csv")]
2. [custom_file_download("http://example.com/full/path/to/file.csv","csv")]