WP Engine Redirect Rules

Since WP Engine deprecated .htaccess support we need to use different methods to redirect old URLs.

They offer a good redirect rule system.

Here’s an solution to redirect the following structure

www.domain.com/wp-content/documents/document-name.pdf

to this new structure

www.domain.com/document/document-name

Source

^/wp-content/documents/(.*?)\.pdf$

Destination

https://www.domain.com/document/$1

 

WordPress: How to get the post count for a term and post type

The WordPress function get_term returns the count for the number of posts in the term, but this is for all post types.  You need a custom function to count the number of posts in a term for a specific post_type.

/**
* Returns the number of posts for a term in a taxonomy, for a post type
* @param string $post_type
* @param string $taxonomy
* @return int count
*/
function get_post_count_for_term_and_post_type( $term_id, $taxonomy, $post_type ) {

  // Build the args
  $args = array(
    'post_type' => $post_type,
    'posts_per_page' => -1,
    'tax_query' => array(
      array(
        'taxonomy' => $taxonomy,
        'field' => 'id',
        'terms' => $term_id,
      )
    )
  );

  // Get the posts
  $posts = get_posts( $args );

  // Return the count
  return count($posts);

}

 

 

 

Send WooCommerce emails using code – useful for testing custom emails

If you need to test custom WooCommerce checkout email templates or functionality you don’t want to constantly be generating orders, or changing order statuses.

Instead use this snippet to send the emails until your code is working perfectly.

Just add it to your theme functions.php.  Be sure to set the $order_id and the $email_class if necessary.

add_action('init', 'send_wc_email');
function send_wc_email() {

// The order ID we're testing the email with
$order_id = 623;

// The email we want to send
$email_class = 'WC_Email_Customer_Processing_Order';

/*
Available emails are:
WC_Email_Customer_Note
WC_Email_Customer_Note
WC_Email_Customer_Completed_Order
WC_Email_Customer_Completed_Order
WC_Email_Customer_New_Account
WC_Email_Customer_New_Account
WC_Email_Customer_Reset_Password
WC_Email_Customer_Reset_Password
WC_Email_Customer_Processing_Order
WC_Email_Customer_Processing_Order
WC_Email_Customer_Invoice
WC_Email_Customer_Invoice
WC_Email_New_Order
*/

// Load the WooCommerce Emails
$wc_emails = new WC_Emails();
$emails = $wc_emails->get_emails();

// Select the email we want & trigger it to send
$new_email = $emails[$email_class];
$new_email->trigger($order_id);

// Show the email content
echo $new_email->get_content();
}

How to Show KGs as grams in WooCommerce

The default metric value in WooCommerce is KGs.

My client has been running his site for years in KGs, and many of his products have the weight entered as KGs.

Now he wants to show the value in grams.

WooCommerce has an option to show the weights as grams:

WooCommerce weights

Whilst this changes the label on the website, WooCommerce was still showing the actual weight value in KGs.

I could run a script to change the weights of the products from KGs to grams, but my client was using a CSV export, and I didn’t want to disrupt this.

The following code will display the KG weights as grams on the WooCommerce templates


// Convert the product weight
function ag_woocommerce_product_get_weight( $weight ) {

// Only convert if we have a weight
if ($weight) {

// The weight is in KGS, and we want grams, to multiple by 1000
$weight = $weight * 1000;
}

return $weight;
};
// add the filter
add_filter( 'woocommerce_product_get_weight', 'ag_woocommerce_product_get_weight', 10, 1 );

wp_foots() Compromised WordPress and Drupal sites

I’m seeing a few WordPress and Drupal sites with template files being compromised, and a call to a PHP method wp_foots being added

<?php wp_foots();?>

This PHP call injects malicious Javascript code that typically writes spam links to the footer.

It is safe to delete the wp_foots() call. However, this call is part of a bigger compromise, and other PHP files within the website have almost certainly been injected as well.

You should look out for calls to eval(base64_decode( and delete/fix those files.

Latest from the blog

View All
Social media & sharing icons powered by UltimatelySocial