Get WordPress images from a different site

Developers usually work on their local installs before pushing any changes to the live or production server and that's how a developer's workflow should be.

In order to setup a local or dev install on which a developer can work or test, they need to pull down the codebase and database dump to exactly mirror the site but they also need to download the media folder to make sure all images show up fine, which can have a bit of its own issues.

Codebase is usually under version control, so that's easy to pull and push changes. Database dump is a single file to download. But media folder can be very large sometimes and not possible to download every time you want to sync up with the files on live site. A super easy solution, that I use, is to setup a redirection rule in your local webserver to redirect all media requests to the live site and then you don't need to sync your media files at all.

Here is a single line, that can be added in your WordPress .htaccess file (if you are using apache, for nginx or any other webserver, idea stays the same, syntax will vary ofcourse):

RedirectMatch 302 ^/wp-content/uploads/(.*)$ http://mylivesite.com/wp-content/uploads/$1

Voila! Now you have all images showing up from live site directly :-)

P.S. - This doesn't work if you are offline for obvious reasons.

Use WooCommerce product image as its category image

WooCommerce is capable of showing category as well as products on its shop page and this is configurable from settings available in admin side but I can totally image running into the situation that categories don't have their images uploaded and they will end up showing placeholder / default image. A quick solution would be to pull a product image from that category and use that image in place of thumbnail, highly dependent on what sort of products you are selling on your store, but in case you fit the bill, here's how to do so:

Install this as a plugin, you can download it. Activate the plugin and any product category will now pull image from a product under that category and show it. In case an image is uploaded for the product category or one already exists, that takes a higher priority and is used instead of trying to get one out of a product.

How to use WooCommerce email design in custom emails

Let's say you have already styled your WooCommerce email and want to use the same email design for a custom email, how will you go about that? Turns out its pretty easy. For the sake of simplicity , I am assuming you have the needed data in variables, change them accordingly as per your code.

For instance, right now, your email function looks something like this:

1 2 3 4 5
$email = 'ashfame@example.com';
$subject = 'Custom stuff email';
$email_content = ''; // whatever it is
 
wp_mail( $email, $subject, $email_content );
view raw before.php hosted with ❤ by GitHub

Now, do the following and it will pull the email design in use with WooCommerce emails even if you haven't styled it.

1 2 3 4 5 6 7 8 9 10 11 12 13 14
$email = 'ashfame@example.com';
$subject = 'Custom stuff email';
$email_heading = 'Custom Heading';
$email_content = ''; // whatever it is
 
ob_start();
 
do_action( 'woocommerce_email_header', $email_heading );
 
echo $email_content; // or simply have HTML markup outside PHP tags here
 
$email_content = ob_get_clean();
 
wp_mail( $email, $subject, $email_content );
view raw after.php hosted with ❤ by GitHub

The magic happens by calling in header and footer markup of WooCommerce emails along with the actual content, buffer the output it and pass that on to mail function.

SSH password less key authentication 101

Check whether you already have a key pair generated. Inside .ssh directory of your home, if you see files name id_rsa and id_rsa.pub then you can use them.

If you don't have them, generate a new one.

How to generate a SSH key pair?

ssh-keygen -t rsa

To be able to use password less authentication, you need to put the public key on server.

Do that either by adding the contents of public key file in ~/.ssh/authorized_keys on server or use ssh-copy-id user@serverhost which will do that for you.

Now just attempt to login by user@serverhost.com and you will be able to do so.

Its totally safe to use public key because without your private key that's no good to anyone.

How it works?

Server sends you a challenge string which you encrypt using your private key and send the encrypted challenge string back. Now only your public key can decrypt it to what it originally was, thus confirming its actually you and let you login. Simple enough.

Setting up your VPS

Its very easy to setup a VPS considering the tools now we have on our exposure. This post is more of a self note to myself, so pardon me for not explaining things.

To quickly setup latest NginX, MySQL, PHP (with APC and Suhosin) & exim, just use this script https://github.com/vladgh/VladGh.com-LEMP

These instructions will get things up and running a secure server.

Buy a VPS which will get you root access to your server.

Create a new user with sudo powers which will be actually used to do everything on a regular basis since using root is not recommended.

adduser ashfame
usermod -a -G sudo ashfame

Setup iptables (firewall)

From your new account, setup your key based login, disable root login & password login so that only key based logins are allowed.

sudo nano /etc/ssh/sshd_config
PasswordAuthentication no
PermitRootLogin no
UsePAM no

Links which explain the above things in detail:

https://library.linode.com/securing-your-server

http://stackful-dev.com/linux-101-hardening-ssh.html

Send & Receive mails to & from localhost

Mails don't work when locally but sometimes it might prove to be very useful in development if one can send & receive mails to & from localhost. It can be setup pretty easily too. Basic idea is to configure postfix for localhost & then use mutt to read local mails. Here is how it is done (should work fine for any UNIX based OS):

Install postfix

sudo apt-get install postfix

Install mutt

sudo apt-get install mutt

To configure postfix for localhost, specify the following in its config file at /etc/postfix/main.cf

myorigin = localhost
mydestination = $myhostname, localhost.$mydomain, localhost
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
relayhost =

Restart postfix

sudo /etc/init.d/postfix restart

Now you can read localhost mails using mutt

mutt

It will read emails from /var/mail/USERNAME

Ubuntu controlling network adapters through terminal

We all know that Ubuntu's network management is quite good but not there yet. One big issue with it is that, on some machines, disabling one type of network adapter, disables the other one too.

Like I use wifi to connect to the internet but my bluetooth is switched on by default all the time. And if I try to disable bluetooth it disables wifi too.

To control them via terminal or command line, you can do this (Atleast from Ubuntu 10.10 onwards):

ashfame@ashfame-xps:~$ rfkill list all
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
4: hci0: Bluetooth
Soft blocked: yes
Hard blocked: no

You see how it says soft blocked & hard blocked. Soft block is a switch in OS & hard block is a hardware switch in your machine.

You can disable the ones you don't want by:

rfkill block bluetooth

and enable the ones you want by:

rfkill unblock wifi

Disabling the ones you don't want will save you some battery if you are on a laptop/notebook/ultrabook.