Functions.php (WordPress)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

Functies die ik soms onderbreng in functions.php

dvb_add_attribute_taxonomy

function dvb_add_attribute_taxonomy($attribute)
{
    # Add a WooCommerce Attribute-taxonomy
    #######################################################################################################
    #
    # * Somehow, the usual function register_taxonomy doesn't work for WooCommerce Attributes, hence the
    #   lower-level approach in this function
    # * Based on the example https://wordpress.stackexchange.com/questions/244335/creating-custom-woocommerce-attribute-taxonomies-from-a-plugin
    # * Jeroen Strompf, De Vliegende Brigade - April 2019
    # * To be included in functions.php where appropriate


    #######################################################################################################
    # Get acces to $wpdb class
    #######################################################################################################
    #
    global $wpdb;

    #######################################################################################################
    # Verify input
    #######################################################################################################
    #
    if (empty($attribute['attribute_type'])) { $attribute['attribute_type'] = 'text';}
    if (empty($attribute['attribute_orderby'])) { $attribute['attribute_orderby'] = 'menu_order';}
    if (empty($attribute['attribute_public'])) { $attribute['attribute_public'] = 0;}
    if (empty( $attribute['attribute_name'] ) || empty( $attribute['attribute_label'] ) ) 
    {
            return new WP_Error( 'error', __( 'Attribute name and/and slug is missing.', 'woocommerce' ) );
    } 
    elseif ( ( $valid_attribute_name = dvb_check_attribute_name( $attribute['attribute_name'] ) ) && is_wp_error( $valid_attribute_name ) ) 
    {
            return $valid_attribute_name;
    } 
    elseif ( taxonomy_exists( wc_attribute_taxonomy_name( $attribute['attribute_name'] ) ) ) 
    {
            return new WP_Error( 'error', sprintf( __( 'Slug "%s" already in use', 'woocommerce' ), sanitize_title( $attribute['attribute_name'] ) ) );
    }


    #######################################################################################################
    # Insert into WordPress
    #######################################################################################################
    #
    # The array field names must correspond with the table column names in the tabel
    #
    $wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );

    do_action( 'woocommerce_attribute_added', $wpdb->insert_id, $attribute );

    flush_rewrite_rules();
    delete_transient( 'wc_attribute_taxonomies' );

    return true;
};

dvb_check_attribute_name

function dvb_check_attribute_name( $attribute_name )
{
	# Check if a given attribute name is valid
    #######################################################################################################
    #
    # * Based on the example https://wordpress.stackexchange.com/questions/244335/creating-custom-woocommerce-attribute-taxonomies-from-a-plugin
    # * Jeroen Strompf, De Vliegende Brigade - April 2019
    # * To be included in functions.php where appropriate

    if ( strlen( $attribute_name ) >= 28 ) 
    {
		return new WP_Error( 'error', sprintf( __( 'Slug "%s" is longer than 28 characters)', 'woocommerce' ), sanitize_title( $attribute_name ) ) );
    } 
    elseif ( wc_check_if_attribute_name_is_reserved( $attribute_name ) ) 
    {
		return new WP_Error( 'error', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term.', 'woocommerce' ), sanitize_title( $attribute_name ) ) );
    }
    return true;
}

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