Tag Archives: wordpress

WordPress: How to Replace an Image in the Media Library Tips, Tricks and Tutorials 01 JUL 2014

Every now and then I mistakenly hit upload on the wrong image whilst crafting a post. Annoyingly, I then have to upload the correct version of the image, which automatically gets assigned a new identifier instead of giving me the option of just simply replacing the file I just mistakenly uploaded.

WordPress doesn’t have a native way of doing this, but that of course then opens the door for the rest of us developers, and so Mans Jonasson stepped up and crafted the nifty Enable Media Replace WordPress plugin.

As the description says, “This plugin allows you to replace a file in your media library by uploading a new file in its place. No more deleting, renaming and re-uploading files!”

It has two replace options, namely:


How to Password Protect Your WordPress Site Tips, Tricks and Tutorials 28 MAY 2014

Sometimes it is quite useful to password protect your whole wordpress website. Maybe it contains sensitive information meant only for your team, or perhaps as a designer you don’t want to hand over the site to the client before payment, but you do need to show them how it looks so that you can get paid!

Enter a fantastic plugin by Ben Huson simply entitled Password Protected.

Essentially, once enabled, your entire site gets hidden behind a login page which asks visiting users for a password. Not for individual pages, or certain post types, or a certain section, but the whole site!

It is important to note that this plugin only protects your WordPress content, and as such it does not protect images or uploaded files – so if you enter the exact URL to in image file it will still be accessible. (In other words, this isn’t intended to prevent hot-linking of your files).

Nevertheless, pretty damn useful overall.

Install as per usual via the Admin panel, and activate. You’ll be asked to select a password and well as what types of users should be able to view the content with or without a password. As a bonus, the plugin does give you the option as to whether or not you want to password protect RSS feeds.


password protected wordpress plugin

Related Link: http://wordpress.org/plugins/password-protected/

Moving WordPress: Database Search and Replace Script Software & Sites 28 MAY 2014

If you’ve ever moved a WordPress site to a different domain then you know it is somewhat of a headache. First you copy over all of the files, then you create a database sql dump of your existing database and import it into the new location, make the necessary changes to the configuration file, and then probably curse when you realize that not all your plugins and themes are working correctly any more.

The problem is that obviously one has to do search and replace operations on the database to ensure that all references to the old domain is switched to the new domain, but given the different serialization storage formats used by various third party bits of code, well simply put, it is easier said than done.

Enter this fantastic database search and replace script written in PHP by the guys over at interconnect/it.

You simply download the zipped script, extract the folder to an area in your WordPress location, and then directly access the newly created folder via the browser – the slick AJAX-driven user interface asks you for the search and replace terms, gives you already filled in database connection information (it’s pretty good at picking this up from your WordPress install), and then lets you run off with either a dry run or live execution. Slick, painless and very effective.

Delete the folder when you’re done, though the script even offers to try and do it on your behalf as well! Well written, user friendly, and highly recommended for whenever you find yourself having to change domains for your WordPress site.


Related Link: Database Search and Replacement Script

Why is WordPress asking for My FTP Details? (Ubuntu) Tips, Tricks and Tutorials 27 MAY 2014

I had an instance where every time I tried to add either a theme or plugin to my fresh WordPress install, WordPress would respond with something along the lines of “To perform the requested action, WordPress needs to access your web server”, followed by a form asking me to enter all the server’s FTP details.

It is not an issue that I usually encounter, and some quick Internet delving provided the answer – essentially WordPress was figuring out as to whether or not it had access to the filesystem, and if it didn’t, well then it would try and accomplish what it needs to do via FTP.

With that knowledge now gleaned, it made perfect sense – in this particular case I had only opened up the wp-content folder for writing, meaning that WordPress was technically blocked from writing to the filesystem anywhere other than that specific folder.

To solve this is relatively simple. If you have SSH root access to the Ubuntu server, then it is simply a matter of logging in and changing the ownership of the entire WordPress directory. So for example, if Apache is running as www-data, you could accomplish the ownership change via:

chown -R www-data: /home/www/wordpressdirectory

That should resolve the issue outright. (Note that not all versions of chown are equal. If that command does not work, see your local chown man page for usage information.)

Well, the above certainly solved my issue.

horizontal wordpress logo landscape

Bonus: A handy tip if you aren’t sure what user you instance of Apache is running as, create a test script with the following content:

<?php echo(exec("whoami")); ?>


WordPress: .htaccess Permalinks not Working (Ubuntu) Tips, Tricks and Tutorials 27 MAY 2014

If your freshly installed WordPress site is throwing standard 404 errors (the Apache default look and feel 404 pages mind you) instead of showing your content when accessing via permalinks, then chances are pretty pretty high that either a) WordPress can’t write to your .htaccess file or b) the Apache rewrite (mod_rewrite) module isn’t installed.

To solve this you’ll need to have SSH access into your Ubuntu server. Once you have logged in, head over to your WordPress install directory and see if there is a .htaccess file there:

ls -a .htaccess

If it isn’t there then that means that WordPress was unable to automatically create it, probably because of file rights. To resolve this, create a .htaccess file:

 touch .htaccess

And populate it with the following WordPress default .htaccess content:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Next, give it appropriate file rights:

chmod 755 .htaccess

Finally, let us go make sure that the Apache mod_rewrite module is installed by running the following:

sudo a2enmod rewrite
sudo service apache2 restart

With the mod_rewrite now enabled under Apache, next is to ensure that the AllowOverride directive is set appropriately for your Apache vhost directory declaration – you can use either AllowOverride FileInfo or AllowOverride All to achieve this. For example, for the default website, edit /etc/apache2/sites-available/default to reflect:

<Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            # changed from None to FileInfo
            AllowOverride FileInfo
            Order allow,deny
            allow from all

With both of these now in place, your permalink problem should be solved.

horizontal wordpress logo landscape

How to Recover: WordPress Plugin Update Left Site in Maintenance Mode Tips, Tricks and Tutorials 12 SEP 2013

wordpress-logoI logged into the WordPress Admin panel for this site the other day and noticed the site had four plugins requiring an update. So off to the Updates page I went, where I selected all plugins affected and pressed the Update button.

Sadly, this didn’t run as smoothly as normal.

Instead, mid-process the frame was replaced with a Site in Maintenance Mode message screen, which of course if you tried any other page of the site, both normal and administrative panel, remained there.

Basically one of the plugins had failed during the update process, thus leaving the site stuck in Maintenance mode and most likely inoperable thanks to the broken plugin.

First things first, I fired up my FTP client and browsed to the home directory for this site where I spotted a .maintenance file – which I promptly deleted.

With the maintenance mode now ‘disabled’, I refreshed a page and noted the error message displayed pointed to the JetPack plugin update as having failed – of course I was lucky here, sometimes it is impossible to know which plugin update failed, which makes the next step slightly harder.

Because I knew that the JetPack plugin was the culprit, I headed off to wordpress.org to download a fresh copy of the plugin, which I then unzipped and installed via FTP to the wp-contents/plugins folder, overwriting the contents of the existing jetpack folder. (If you don’t know what plugin crashed, you’ll have to do this process for all the plugins you were updating.)

The FTP upload complete, I refreshed the page and was relieved to see my site back in all of its former glory.


How to add Custom Query Parameters in WordPress Programming 08 JUL 2013

wordpress-logoI’m in the process of modifying my WordPress theme such that when you click on one of the post excerpts shown on the home page, or any category page for that matter, the resulting post page shows up in a fancybox lightbox, thereby allowing you to safely and quickly open up links without worrying about losing your place on the scrolling listing page.

In order to minimize the work for myself, I intend to reuse the single.php file in my theme to display for both a standard accessed single page as well as this new lightbox view, though obviously the two views would have to be tweaked, as for example on the lightbox page, you don’t necessarily want to show the site’s header all over again!

So how does one do this? Well, the obvious answer is that we need either a $_GET or $_POST variable which we can use to distinguish the two views, which brings us to the next question: how does one add a custom query parameter to WordPress such that it’s rewrite engine understands what to do with the new parameter?

Well the answer lies in the useful query_vars filter which WordPress uses in its URL rewriting mechanism.

Locate or create the functions.php file in the root of your theme folder and add the following bit of example code:

add_filter('query_vars', 'fancybox_display_queryvars' );

function fancybox_display_queryvars( $qvars )
  $qvars[] = 'fancybox_display';
  return $qvars;

In this particular example I’m only adding one new URL parameter (fancybox_display) to those that WordPress already understands, meaning that given any URL that is suffixed with ‘?fancybox_display=1’ will result in a $_GET variable I can access and make use of.

For example, to check if it exists, and thus know we need to follow the logic dictating a lightbox layout using our example above, use the following:

if( isset( $wp_query->query_vars['fancybox_display'] )) {
 //do something 

And that’s all there is to it really.

UPDATE: Only noticed now that I’ve obviously stumbled into this problem before… Oh well, a double post shouldn’t hurt me too much I guess.

WordPress: Check the Current Page Number CodeUnit 30 OCT 2012

If you are working on designing your own WordPress theme, then I’m pretty sure that at some point in time you’ve wanted to identify or check what page the user is currently viewing when working with loops.

To do this we need to make use of WordPress’ get_query_var function and look up a variable named ‘paged’, which as you guessed it, is a reference as to which page is currently being shown.

In practice:

$pageNumber = (get_query_var('paged')) ? get_query_var('paged') : 1; 

Obviously the page number can now be found in the $pageNumber variable.


WordPress: Force Contact E-mail before Allowing a File Download CodeUnit 25 SEP 2012

The other day one of the WordPress sites I was asked to help out on (http://www.portsevolution.com) needed a solution whereby they first needed to receive some contact details before allowing a reader to download an associated PDF file.

Digging around the WordPress plugin repository I came across a great solution in the form of a plugin by M&S Consulting, entitled “Email Before Download”, which basically combines the functionality of the excellent WordPress plugin “Contact Form 7” with that of “WordPress Download Monitor” to achieve the required functionality.

Taken from their official description: Email Before Download presents your users with a form where they submit information, like their name and email address, prior to receiving a download. This plugin integrates with the popular Contact Form 7 and WordPress Download Monitor plugins, allowing you to create any form you like and manage/monitor your file downloads. You can also export a list of users that have downloaded files from the plugin’s settings page.

As an option, you can configure Email Before Download to: