How to fix: WordPress updates and plugin installs require FTP login but shouldn’t.

WordPress is generally very good about working under any server situation you put it in. It even has fall back code if the preferred server settings are not available.

The plugin and update installation code is one example of this. If WordPress doesn’t have the rights to directly update files, it will ask you for FTP login information.

However, there are times when you KNOW the permissions are correct to allow Apache to update files and WordPress doesn’t detect it and insists on FTP. This should be extremely rare. However if you ever do come across this circumstance you can force WordPress to override this behavior and use direct updates.

Just add these lines to wp-config.php:

define( ‘FS_METHOD’, ‘direct’ );
define( ‘FS_CHMOD_DIR’, 0777 );
define( ‘FS_CHMOD_FILE’, 0777 );

You can read more about these constants on the WordPress Codex.

How to: Split path and filename in MS Excel, Google Docs or OpenOffice Calc

I did a code review along with Brad Williams yesterday and today we combined our results.  The problem was, our spreadsheet columns didn’t match.  He’d combined path and filename into one column and I’d split them out into two columns. We decided the split path and filename was useful.  So I had to come up with  a quick set of formula to create the two extra columns, splitting path from filename.

For a  short time, I thought it wouldn’t be possible, and that my formula foo had  finally failed me.  The difficulty is of course that the find()  doesn’t have an option for returning the position of anything but the first occurrence of the search text.  So, you don’t have anyway to know where to split the string.  Ahhh, but lo and behold the substitute function can be used to replace the nth occurrence of a character or string.  If you replace the right duplicated character with something unique, you can come use find() to determine the right place do do the split.

The method I used was based upon formulas found on Excel Ticker in an article written by Mourad Louha.  It took me several reads through to grok the concept of his method. (Actually I came back to the article hours later to figure out how the formula worked – long after hacking the formulas to do what I wanted). So, I thought I would boil it down for you here.  Also his formula was based upon an unusual layout in his spreadsheet.  What I’ve listed here simply assume a 3 column spreadsheet (Folder, File, Full Path) with a row for the title.  If your third column (c2) has the concatenated/full path, just paste the right formula into the first and second columns (a2 and b2) in the second row and you are done.

Here is  Mourad’s method boiled down into simple steps:

1. Determine how many slashes there are.  Subtract the length of the string, with slashes removed, from the original length of the string.

2. Replace the LAST occurrence of the slash with unique text we can find (we substitute $$$ but anything unique could be used)

3.  We now use find()  to get the position of the unique text and use that as the location to split the string.

 

Open Office Calc/Google Spreadsheet Filename Split Formulas

Linux Formula for splitting the path off a filepath:
= LEFT($C2;FIND(“$$$”;SUBSTITUTE($C2;”/”;”$$$”;LEN($C2) – LEN(SUBSTITUTE($C2;”/”;””))))-1)

Linux Formula for splitting a file name off a filepath:
=RIGHT($C2;LEN($C2)-FIND(“$$$”;SUBSTITUTE($C2;”/”;”$$$”;LEN($C2)-LEN(SUBSTITUTE($C2;”/”;””)))))

Windows Formula for splitting the path off a filepath:
= LEFT($C2;FIND(“$$$”;SUBSTITUTE($C2;”\”;”$$$”;LEN($C2) – LEN(SUBSTITUTE($C2;”\”;””))))-1)

Windows Formula for splitting a file name off a filepath:
=RIGHT($C2;LEN($C2)-FIND(“$$$”;SUBSTITUTE($C2;”\”;”$$$”;LEN($C2)-LEN(SUBSTITUTE($C2;”\”;””)))))

 

Microsoft Excel Filename Split Formulas

Microsoft Exceluses commas instead of semicolons.

Linux Formula for splitting the path off a filepath:
= LEFT($C2,FIND(“$$$”,SUBSTITUTE($C2,”/”,”$$$”,LEN($C2) – LEN(SUBSTITUTE($C2,”/”,””))))-1)

Linux Formula for splitting a file name off a filepath:
=RIGHT($C2,LEN($C2)-FIND(“$$$”,SUBSTITUTE($C2,”/”,”$$$”,LEN($C2)-LEN(SUBSTITUTE($C2,”/”,””)))))

Windows Formula for splitting the path off a filepath:
= LEFT($C2,FIND(“$$$”,SUBSTITUTE($C2,”\”,”$$$”,LEN($C2) – LEN(SUBSTITUTE($C2,”\”,””))))-1)

Windows Formula for splitting a file name off a filepath:
=RIGHT($C2,LEN($C2)-FIND(“$$$”,SUBSTITUTE($C2,”\”,”$$$”,LEN($C2)-LEN(SUBSTITUTE($C2,”\”,””)))))

How to: Install MemCached on CentOS / Redhat using yum

Installing memcached on a server for use with W3Total cache can seem daunting if you haven’t done it before.  Once you’ve done it enough to work out a method and know the speed bumps you can come across,  you can

 

 

Use this command to determine the CentOS/RedHat version. You need this to know if you are working with version 4 5 or 6.

cat /etc/redhat-release

Use this command to determine if the OS is 64bit or 32bit (look for x64. If it is not there, 99% of the time you’re on 32 bit.)

uname -a

Retrieve the IUS Community repository installation files to allow an easy install of the memcache module from the IUS repo.

Browse the repo at http://dl.iuscommunity.org/pub/ius/stable/Redhat/ to make certain you have the right files for your server:

This example is for 64 bit Red Hat Enterprise Linux (RHEL) 5 and is current as of 2014-03-04:

cd /usr/src/
mkdir ius
cd ius
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/ius-release-1.0-11.ius.el5.noarch.rpm
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/epel-release-5-4.noarch.rpm

This example is for 32 bit Red Hat Enterprise Linux (RHEL) 5 and is current as of 2014-03-04:

cd /usr/src/
mkdir ius
cd ius
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/i386/ius-release-1.0-11.ius.el5.noarch.rpm
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/i386/epel-release-5-4.noarch.rpm

This example is for 64 bit Red Hat Enterprise Linux (RHEL) 6 and is current as of 2014-03-04:

cd /usr/src/
mkdir ius
cd ius
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/ius-release-1.0-11.ius.el6.noarch.rpm
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/epel-release-6-5.noarch.rpm

This example is for 32 bit Red Hat Enterprise Linux (RHEL) 6 and is current as of 2014-03-04:

cd /usr/src/
mkdir ius
cd ius
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-11.ius.el6.noarch.rpm
wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm

CENTOS 7 64 bit

cd /usr/src/
mkdir ius
cd ius

wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/7/x86_64/ius-release-1.0-13.ius.el7.noarch.rpm

wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/7/x86_64/yum-plugin-replace-0.2.7-1.ius.el7.noarch.rpm

wget http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm

 

Install both of these files:

rpm -ivh *.rpm

Perform the install:

yum -y install memcached
service memcached start
chkconfig memcached on
pecl install memcache

Note: If you get a “Can’t compile c code” error, try making your tmp folder executible before running the pecl command:

mount -o,remount,rw,exec /var/tmp

Make sure you remount the tmp directory securely with noexec when you are done:

mount -o,remount,rw,noexec /var/tmp

 

Make sure this line has correctly been added to the php.ini file for your site:

extension=”memcache.so”

Sometimes the php.ini file has been overridden for a specific directory. Look for that, if phpinfo() tells you memcache is not active.  This picture shows a site with the default php.ini overridden by a local copy. It required the extension line to be manually added to the overriding ini file.

phpinfo() output

How to: Refresh your WordPress page unless a comment is being typed

A customer wanted a piece of code that allowed a page to be refreshed once the client has remained on the page after a certain amount of time. They’d used a refresh command previously, but the problem was that this interrupted anyone who was in the middle of typing a comment.

This is my solution to refresh the page every 5 minutes:

<script language="JavaScript">
    var sURL = unescape(window.location.pathname);
    var intValue = 0;
    function doLoad()
    {
        intValue=setTimeout( "refresh()", 300*1000 );
    }

    function refresh()
    {
        window.location.href = sURL;
    }

    function noRefresh(e)
    {
        switch (e.keyCode) {
            case 40:
            case 39:
            case 38:
            case 37:
            case 34:
            case 33:
                break;
            default:
                clearTimeout(intValue);
        }

    }

    if ($.browser.mozilla) {
        $(document).keypress(noRefresh);
    } else {
        $(document).keydown(noRefresh);
    }

    $(document).ready(doLoad());
</script>
<noscript>
    <meta http-equiv="refresh" content="300">
</noscript>

Fixing mysqldump: Got error: 1016: Can’t open file & mysqldump: Got error: 23

When doing exports of large databases using mysqldump, it is common to get errors that are along the lines of:

mysqldump: Got error: 1016: Can’t open file: ‘./databasename/tablename.frm’ (errno: 24) when using LOCK TABLES

or maybe

mysqldump: Got error: 23: Out of resources when opening file ‘./databasename/tablename.MYD’ (Errcode: 24) when using LOCK TABLES

Both come from the same cause and don’t worry – your database is not currupt..

The first action that mysqldump takes is to lock all of the tables so that the database cannot go out of sync from the beginning to the end of the export.  Of course, that means that your users can’t make changes while  doing the dump. And that’s just one more reason you need to be careful using mysqldump.

The solution to this problem is simple add –skip-lock-tables to your command line.

At that point the tables won’t be locked, and the export will run much faster. There is a slight downside in that people can use the table as it is being exported and you could potentially get an update written as you are exporting that table. But it is unlikely that you’ll hit any real world problems when doing this, especially with things like blog exports.