Functions.php (WordPress): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
Regel 1: Regel 1:
Paar functies die ik soms onderbreng in <code>functions.php</code>
+
Functies die ik soms onderbreng in <code>functions.php</code>
 +
 
 +
== dvb_delete_all_products ==
  
 
<pre>
 
<pre>
Regel 8: Regel 10:
 
# * Handy for debugging
 
# * Handy for debugging
 
# * Source: https://www.wpini.com/delete-all-woocommerce-products/
 
# * Source: https://www.wpini.com/delete-all-woocommerce-products/
 +
# * Strompf, May 2019
 
#
 
#
 
function dvb_delete_all_products()
 
function dvb_delete_all_products()
Regel 24: Regel 27:
 
    ON taxes.term_id=terms.term_id
 
    ON taxes.term_id=terms.term_id
 
  WHERE object_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type='product');";
 
  WHERE object_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type='product');";
 
+
Paar fPaar f
 
try{
 
try{
 
     
 
     
Regel 34: Regel 37:
 
}catch(Exception $e){}
 
}catch(Exception $e){}
 
}
 
}
 +
</pre>
 +
 +
== dvb_delete_all_thumbnails ==
 +
 +
<pre>
 +
#
 +
#######################################################################################
 +
# Remove all product-related images from the database
 +
#######################################################################################
 +
#
 +
# * Currently, I don't use galleries. Only removing thumbnails, is sufficient
 +
# * Only remove thumbnail-images, no other images (e.g., the site logo image - When an
 +
#  image is used for both, it will be deleted - Don't use them for multiple functions!)
 +
# * Images won't be removed from the upload-directory - Just to keep it simple
 +
# * Strompf, May 2019
 +
#
 +
function dvb_delete_all_thumbnails()
 +
{
 +
# Access
 +
#######################################################################################
 +
#
 +
# $pad = "/home/strompf/www/rt.dvb/";
 +
# require_once($pad . "wp-load.php");
 +
#
 +
global $wpdb;
 +
 +
 +
# Assemble queries
 +
#######################################################################################
 +
#
 +
$sql_01 = "drop table if exists ".$wpdb->prefix."image_tmp;";
 +
$sql_02 = "create temporary table ".$wpdb->prefix."image_tmp select ".$wpdb->prefix."postmeta.meta_value as image_id from ".$wpdb->prefix."postmeta
 +
join ".$wpdb->prefix."posts on ".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."posts.ID where ".$wpdb->prefix."posts.post_type like 'product' and ".$wpdb->prefix."postmeta.meta_key like '_thumbnail_id';";
 +
$sql_03 = "delete ".$wpdb->prefix."posts from ".$wpdb->prefix."posts join ".$wpdb->prefix."image_tmp on ".$wpdb->prefix."posts.ID = ".$wpdb->prefix."image_tmp.image_id;";
 +
$sql_04 = "delete ".$wpdb->prefix."postmeta from ".$wpdb->prefix."postmeta join ".$wpdb->prefix."image_tmp on ".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."image_tmp.image_id;";
 +
 +
 +
# Ready to go?
 +
#######################################################################################
 +
#
 +
// echo "\nsql_01: ".$sql_01;
 +
// echo "\nsql_02: ".$sql_02;
 +
// echo "\nsql_03: ".$sql_03;
 +
// echo "\nsql_04: ".$sql_04."\n";
 +
 +
 +
# Go!
 +
#######################################################################################
 +
#
 +
try
 +
{
 +
$wpdb->query($sql_01);
 +
    $wpdb->query($sql_02);
 +
    $wpdb->query($sql_03);
 +
    $wpdb->query($sql_04);
 +
   
 +
}
 +
catch(Exception $e){}
 +
}
 
</pre>
 
</pre>
  

Versie van 7 mei 2019 16:15

Functies die ik soms onderbreng in functions.php

dvb_delete_all_products

###############################################################
# dvb_delete_all_products
###############################################################
#
# * Handy for debugging
# * Source: https://www.wpini.com/delete-all-woocommerce-products/
# * Strompf, May 2019
#
function dvb_delete_all_products()
{
	global $wpdb;
	 
	$sql_1 = "DELETE FROM ".$wpdb->prefix."term_relationships WHERE object_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'product');";
	$sql_2 = "DELETE FROM ".$wpdb->prefix."postmeta WHERE post_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'product');";
	$sql_3 = "DELETE FROM ".$wpdb->prefix."posts WHERE post_type = 'product';";
	 
	$sql_4 = "DELETE relations.*, taxes.*, terms.*
	  FROM ".$wpdb->prefix."term_relationships AS relations
	  INNER JOIN ".$wpdb->prefix."term_taxonomy AS taxes
	    ON relations.term_taxonomy_id=taxes.term_taxonomy_id
	  INNER JOIN ".$wpdb->prefix."terms AS terms
	    ON taxes.term_id=terms.term_id
	  WHERE object_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type='product');";
	 Paar fPaar f
	try{
	    
	    $wpdb->query($sql_1);
	    $wpdb->query($sql_2);
	    $wpdb->query($sql_3);
	    $wpdb->query($sql_4);
	    
	}catch(Exception $e){}
}

dvb_delete_all_thumbnails

#
#######################################################################################
# Remove all product-related images from the database
#######################################################################################
#
# * Currently, I don't use galleries. Only removing thumbnails, is sufficient
# * Only remove thumbnail-images, no other images (e.g., the site logo image - When an 
#   image is used for both, it will be deleted - Don't use them for multiple functions!)
# * Images won't be removed from the upload-directory - Just to keep it simple
# * Strompf, May 2019
#
function dvb_delete_all_thumbnails()
{
	# Access
	#######################################################################################
	#
	# $pad = "/home/strompf/www/rt.dvb/";
	# require_once($pad . "wp-load.php");
	#
	global $wpdb;


	# Assemble queries
	#######################################################################################
	#
	$sql_01 = "drop table if exists ".$wpdb->prefix."image_tmp;";
	$sql_02 = "create temporary table ".$wpdb->prefix."image_tmp select ".$wpdb->prefix."postmeta.meta_value as image_id from ".$wpdb->prefix."postmeta
	join ".$wpdb->prefix."posts on ".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."posts.ID where ".$wpdb->prefix."posts.post_type like 'product' and ".$wpdb->prefix."postmeta.meta_key like '_thumbnail_id';";
	$sql_03 = "delete ".$wpdb->prefix."posts from ".$wpdb->prefix."posts join ".$wpdb->prefix."image_tmp on ".$wpdb->prefix."posts.ID = ".$wpdb->prefix."image_tmp.image_id;";
	$sql_04 = "delete ".$wpdb->prefix."postmeta from ".$wpdb->prefix."postmeta join ".$wpdb->prefix."image_tmp on ".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."image_tmp.image_id;";


	# Ready to go?
	#######################################################################################
	#
	// echo "\nsql_01: ".$sql_01;
	// echo "\nsql_02: ".$sql_02;
	// echo "\nsql_03: ".$sql_03;
	// echo "\nsql_04: ".$sql_04."\n";


	# Go!
	#######################################################################################
	#
	try
	{
		$wpdb->query($sql_01);
	    $wpdb->query($sql_02);
	    $wpdb->query($sql_03);
	    $wpdb->query($sql_04);
	    
	}
	catch(Exception $e){}
}	

Bronnen