How to create a WordPress compatible slug in MySQL

Sometimes it is convenient to import terms or posts into a WordPress database.  When you do, you may need to create a ‘slug’ column. To do this, I’ve modified a user defined function I found on StackOverflow or some similar site (possibly here: http://stackoverflow.com/questions/5409831/mysql-stored-function-to-create-a-slug/5410316 ).  That version had some bugs. So I fixed them.

Here is my final script to create the UDF slugify.

Usage: select slugify(name), name from externaldata;

 

CREATE DEFINER=`root`@`localhost` FUNCTION `slugify`(`dirty_string` VARCHAR(200)) RETURNS varchar(200) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE x, y , z Int;
Declare temp_string, allowed_chars, new_string VarChar(200);
Declare is_allowed Bool;
Declare c, check_char VarChar(1);

set allowed_chars = “abcdefghijklmnopqrstuvwxyz0123456789-“;
set temp_string = lower(dirty_string);

Select temp_string Regexp(‘&’) Into x;
If x = 1 Then
Set temp_string = replace(temp_string, ‘&’, ‘ and ‘);
End If;

Select temp_string Regexp(‘[^a-z0-9]+’) into x;
If x = 1 then
set z = 1;
While z <= Char_length(temp_string) Do
Set c = Substring(temp_string, z, 1);
Set is_allowed = False;
Set y = 1;
Inner_Check: While y <= Char_length(allowed_chars) Do
If (strCmp(ascii(Substring(allowed_chars,y,1)), Ascii(c)) = 0) Then
Set is_allowed = True;
Leave Inner_Check;
End If;
Set y = y + 1;
End While;
If is_allowed = False Then
Set temp_string = Replace(temp_string, c, ‘-‘);
End If;

set z = z + 1;
End While;
End If;

Select temp_string Regexp(“^-|-$|'”) into x;
If x = 1 Then
Set temp_string = Replace(temp_string, “‘”, ”);
Set z = Char_length(temp_string);
Set y = Char_length(temp_string);
Dash_check: While z > 1 Do
If Strcmp(SubString(temp_string, -1, 1), ‘-‘) = 0 Then
Set temp_string = Substring(temp_string,1, y-1);
Set y = y – 1;
Else
Leave Dash_check;
End If;
Set z = z – 1;
End While;
End If;

Repeat
Select temp_string Regexp(“–“) into x;
If x = 1 Then
Set temp_string = Replace(temp_string, “–“, “-“);
End If;
Until x <> 1 End Repeat;

Return temp_string;
END

Help Me with Discussing Banner Sizes for Responsive Sites

I posted sent this to a client asking about what the right banner image size is for a #WordPress site. Where am I off and could have improved it?

[..]
So when creating custom headers for sites that are responsive and will be seen on different sizes, there are really three basic approaches:

1. Resizing/Scaling – The whole image is always shown, but it resizes smaller on smaller screens. The end result of this is that the detail of the the image is lots and titles that are now 1/4 the original size and are unreadable.

2. Centered – Allows for a single image to be used that can be as wide enough for a large screen, but only the middle is shown. This means that the most important info, like the title, has to be in the middle 320 pixels. It will also result in Google yelling at you because you are serving a very wide image on phones with a very limited width screen. A variance of this is left justified with the same results.

3. Multiple images – Based upon the size of the screen, the best image is displayed to the reader. This can be combined with #2 to cover multiple dimensions with less of a penalty from really wide images.

As with everything else, the answer that involves the most work, is the best answer. So #3 is best way to go.

With your last image, I created a three images (attached) that had the spirit of the first image. The first was just resized to fit the 1280 width screen.

The common sizes are: (http://bit.ly/1IAL98z)
320 for iphone 3&4,
768 for ipads,
1080 new tablets,
1280 for desktops & newer

You could go NUTS creating images for all the different dimensions. Take a look at the second article in the links below and you can see how many “common” breakpoints there are. If you create a site with three banners 320, 768, 1280, I think that covers most of the basis, especially if the readable content is mostly centered and can fall off the edges without any loss of message.

Some good articles:
http://www.metaltoad.com/blog/device-chart-for-analytics-driven-responsive-web-design-planning
https://responsivedesign.is/develop/browser-feature-support/media-queries-for-common-device-breakpoints
https://developers.google.com/web/fundamentals/design-and-ui/responsive/fundamentals/how-to-choose-breakpoints#pick-major-breakpoints-by-starting-small-then-working-up
https://responsivedesign.is/articles/why-you-dont-need-device-specific-breakpoints

How do you split a text field/textarea by line breaks?

Sometimes you want to provide a simple way for users to enter multiple values.  Perhaps the simplest way for the user is to understand is allow them to enter new values on each line.  Historically that’s how Internet Explorer allowed you to configure multiple home pages: 1 per line. It’s a tested method that works.

However, how do you then process the information?  There are lots of possiblitities, however I prefer an answer I found was posted back in 2011: Use preg_split().

preg_split( '/\r\n|[\r\n]/', $_POST[ 'yourtextarea' ], PREG_SPLIT_NO_EMPTY )

By using regex, this function will handle entries from various platforms. You don’t need to worry about whether is just a carriage return or just a linefeed or even both at the end of a line.  The split is handled nicely and you get an array returned. It will even, by adding an optional third parameter, ignore any blank lines.  Once you have your value returned, you can write the array out to an option record, or process it however you like.

Once you have the array loaded from your DB, how do you split it into multiple lines again? That’s simple: Use implode().

implode( "\n",  $myarray );

Hope that helps someone!

WordPress and NotePad++

I use NetDrive and NotePad++ to do my WordPress Development, but I’ve customized NPP slightly along the way.
I’ve added the following functions:
Alt-F3 – Search the WordPress codex for the word at the cursor in the editor (usually for filters and actions etc)
Alt-F5 – Look up the word at the cursor as a function on the WordPress Developer wiki (will show the source)
Alt-F6 – Look up the word at the cursor as a function in the WordPress codex (will include examples)

To do this I edited shortcuts.xml

You can edit this file by closing notepad++ and then hitting WindowsKey+R and pasting in: notepad %AppData%\Notepad++\shortcuts.xml
And pressing enter.

 <NotepadPlus>
 <InternalCommands>
 <Shortcut id="41014" Ctrl="no" Alt="no" Shift="yes" Key="116" />
 </InternalCommands>
 <Macros>
 <Macro name="Trim Trailing and save" Ctrl="no" Alt="yes" Shift="yes" Key="83">
 <Action type="2" message="0" wParam="42024" lParam="0" sParam="" />
 <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
 </Macro>
 </Macros>
 <UserDefinedCommands>
 <Command name="Launch in Firefox" Ctrl="yes" Alt="yes" Shift="yes" Key="88">firefox &quot;$(FULL_CURRENT_PATH)&quot;</Command>
 <Command name="Launch in IE" Ctrl="yes" Alt="yes" Shift="yes" Key="73">iexplore &quot;$(FULL_CURRENT_PATH)&quot;</Command>
 <Command name="Launch in Chrome" Ctrl="yes" Alt="yes" Shift="yes" Key="82">chrome &quot;$(FULL_CURRENT_PATH)&quot;</Command>
 <Command name="Launch in Safari" Ctrl="yes" Alt="yes" Shift="yes" Key="70">safari &quot;$(FULL_CURRENT_PATH)&quot;</Command>
 <Command name="Get php help" Ctrl="no" Alt="yes" Shift="no" Key="112">http://www.php.net/$(CURRENT_WORD)</Command>
 <Command name="Google Search" Ctrl="no" Alt="yes" Shift="no" Key="113">http://www.google.com/search?q=$(CURRENT_WORD)</Command>
 <Command name="Codex Search" Ctrl="no" Alt="yes" Shift="no" Key="114">https://wordpress.org/search/$(CURRENT_WORD)</Command>
 <Command name="WP Developer Function" Ctrl="no" Alt="yes" Shift="no" Key="116">https://developer.wordpress.org/reference/functions/$(CURRENT_WORD)/</Command>
 <Command name="Codex Function" Ctrl="no" Alt="yes" Shift="no" Key="117">https://codex.wordpress.org/Function_Reference/$(CURRENT_WORD)</Command>
 <Command name="Open containing folder" Ctrl="no" Alt="no" Shift="no" Key="0">explorer $(CURRENT_DIRECTORY)</Command>
 <Command name="Open current dir cmd" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /K cd /d $(CURRENT_DIRECTORY)</Command>
 <Command name="Open in another instance" Ctrl="no" Alt="yes" Shift="yes" Key="79">$(NPP_DIRECTORY)\notepad++.exe $(CURRENT_WORD) -nosession -multiInst</Command>
 </UserDefinedCommands>
 <PluginCommands />
 <ScintillaKeys />
</NotepadPlus>

 

How to find Joomla WordPress or Drupal version from Linux CLI

I was about to rewrite some of these queries again, and decided to google instead.  I found this page with them already written: http://kb.iweb.com/entries/29801848-Verifying-CMS-versions-on-multiple-websites

So this too gets stored away in my cave.  Enjoy:


WordPress version
:

Linux/cPanel:

find /home/*/public_html/ -type f -iwholename “*/wp-includes/version.php” -exec grep -H “\$wp_version =” {} \;

Linux/Plesk:

find /var/www/vhosts/*/httpdocs/ -type f -iwholename “*/wp-includes/version.php” -exec grep -H “\$wp_version =” {} \;

Windows/IIS (default path) with Powershell:

Get-ChildItem -Path “C:\inetpub\wwwroot\” -Filter “version.php” -Recurse | Select-String -pattern “\`$wp_version =”

 

Joomla! 1/2/3 version and release:

Linux/cPanel:

find /home/*/public_html/ -type f \( -iwholename ‘*/libraries/joomla/version.php’ -o -iwholename ‘*/libraries/cms/version.php’ -o -iwholename ‘*/libraries/cms/version/version.php’ \) -print -exec perl -e ‘while (<>) { $release = $1 if m/ \$RELEASE\s+= .([\d.]+).;/; $dev = $1 if m/ \$DEV_LEVEL\s+= .(\d+).;/; } print qq($release.$dev\n);’ {} \; && echo “-“

Linux/Plesk:

find /var/www/vhosts/*/httpdocs/ -type f \( -iwholename ‘*/libraries/joomla/version.php’ -o -iwholename ‘*/libraries/cms/version.php’ -o -iwholename ‘*/libraries/cms/version/version.php’ \) -print -exec perl -e ‘while (<>) { $release = $1 if m/ \$RELEASE\s+= .([\d.]+).;/; $dev = $1 if m/ \$DEV_LEVEL\s+= .(\d+).;/; } print qq($release.$dev\n);’ {} \; && echo “-“

 

Drupal version:

Linux/cPanel:

find /home/*/public_html/ -type f -iwholename “*/modules/system/system.info” -exec grep -H “version = \”” {} \;

Linux/Plesk:

find /var/www/vhosts/*/httpdocs/ -type f -iwholename “*/modules/system/system.info” -exec grep -H “version = \”” {} \;

 

Latest version information:

 

 

What does “Is this ok [y/d/N]:” mean?

When installing new software using yum, you will occasionally get the the option of “Is this ok [y/d/N]:”  Everyone knows recognizes Yes and No, but the option d may have thrown you for a loop.

“d” simply stands for download only.  If you were to do this from the commandline you’d use this:

yum –download-only

Occasionally this is useful if you are prepping a resource, like a thumb drive, with all of the RPMs that you standardly install.  You might also want to have the RPMs available for switching between versions of software.  I’m sure there are many more reasons too.

Hopefully this has answered your question.

Advanced Google Search Operators

Sometimes a simple Google search won’t do. Today I needed to find some malformed URLs in a site.   I couldn’t remember how to search for specific text in a URL.

I had a hard time finding the Google documentation of search commands this morning.  It I needed the allinurl: function.

I don’t want to have to search that hard again so here is the documentation in my Code Cave:

Search Service Search Operators
Web Search allinanchor:allintext:allintitle:allinurl:cache:define:filetype:id:inanchor:info:intext:intitle:inurl:link:related:site:
Image Search allintitle:allinurl:filetype:inurl:intitle:site:
Groups allintext:allintitle:author:group:insubject:intext:intitle:
Directory allintext:allintitle:allinurl:ext:filetype:intext:intitle:inurl:
News allintext:allintitle:allinurl:intext:intitle:inurl:location:source:
Product Search allintext:allintitle:

Source and additional information:  http://www.googleguide.com/advanced_operators_reference.html

Finding large files on a Linux server

Here are a couple commands you might find useful.

This command finds files that are larger than half a gig:

find / -type f  -size +500M -exec ls -lh {} \;

This command finds files that are larger than 1 gig:

find / -type f  -size +1G -exec ls -lh {} \;

This command finds files that are larger than 1 gig excluding the home directory which might be on a different mount. (notice the removal of the -type f so that the not works on paths):

find / -not \( -path /home -o -path /boot -o -path /tmp \)  -size +1G -exec ls -lh {} \;

 

How to Fix: After upgrading WHMCS: Down for Maintenance An upgrade is currently in progress

I recently came across an issue I’d not seen before. After performing what I thought was a proper upgrade, I found that my support and sales portal was displaying the error:

Down for Maintenance (Err 2)
An upgrade is currently in progress… Please come back soon…

I found several difference references to this error on the web. The one detail that I was able to glean was that this error usually occurs when the files are updated, but the database is not. This was the case in my situation. It turns out that I caused my own problem. The key to the mystery is that the files to perform the database upgrade are actually part of the “install” directory.

The instructions for a full upgrade are  here: http://docs.whmcs.com/Upgrading

Basically  you just:

  1. Extract the files
  2. Rename the admin and cron directories as needed
  3. Copy those files over the existing installation
  4. Visit the site and follow the upgrade instructions, if any.

My problem was that #4  includes an error at the end that tells you the install directory still exists, delete it.  Being a guy who likes to optimize a process as much as possible, I was deleting the install directory before copying  in the files.  This process worked great 99% of the time.  However, eventually they changed the database structure from 5.2.13 to 5.2.14 or 5.2.15.  THEN the result was that the WHMCS software could not upgrade the database. and I got the wonderful WHMCS Down for Maintenance error.

So really the steps should be:

  1. Extract the files
  2. Rename the admin and cron directories as needed
  3. Copy those files over the existing installation
  4. Visit the site and follow the upgrade instructions, if any.
  5. Delete the install directory when prompted.

At that point, everything should work fine.   At least it did for my particular incarnation of the problem.  I hope it solves your issues too.

How to quickly update WHMCS on WiredTree hosting and others.

NOTE: This script has been updated to remove the premature deletion of the install directory.

Over the last month or two, WHMCS has been updated many times. This is a GOOD THING, but it can be annoying to go through the normal update process every week or two.

So, I created a small function that I’ve added to /etc/bashrc on my server.

It does the following:
1. Verifies the script is running from the right server.
2. Changes directories to the location where I have WHMCS installed.
3. Removes any files and folders from a previous update
4. Downloads the latest update from the password protected WiredTree repository
5. Unpacks it
6. Renames the admin directory to a new name for security purposes (you may need to do the cron dir too)
7. Changes the owner and group of the new files to be the one required for this website.
8. Copies in all of the new files, overwriting what is there.

So updates now take a few seconds rather than 20 distracting minutes. Maybe you will find this useful.

Here is the function I added to /etc/bashrc:

 

function upwhmcs(){
  curhostname=$(hostname -s)
  desiredhost='REPLACE_WITH_HOSTNAME'

  if [ "${curhostname}" == "$desiredhost" ]; then
    cd /home/REPLACE_WITH_DIRNAME
    rm -Rf whmcs
    rm -f whmcs_current.zip
    wget http://REPLACE_WITH_USERNAME:REPLACE_WITH_PASSWORD@whmcs.wiredtree.com/whmcs_current.zip
    unzip -o whmcs_current.zip
    cd whmcs
    mv admin REPLACE_WITH_ADMIN_DIR_NAME
    chown -R REPLACE_WITH_DIROWNER:REPLACE_WITH_DIROWNER .
    cp -Rf * ../www
    cd ../www
    pwd
    return
  fi
  echo "Wrong Server. Update skipped"
}

 

To use that listing you need to replace the following text
REPLACE_WITH_HOSTNAME – Replace this with what you get when you run ‘hostname -s’ from the command line.
REPLACE_WITH_DIRNAME – Replace this text with the directory of your whmcs installation. Maybe it is /home/whmcs/ or /home/hosting/ or /home/sales/
REPLACE_WITH_USERNAME – This should become the username that WiredTree gave you to login and get WHMCS updates
REPLACE_WITH_PASSWORD – This should become the password that WiredTree gave you to login and get WHMCS updates
REPLACE_WITH_ADMIN_DIR_NAME – This is the custom name of the admin directory on your server. If you just use admin, delete this mv line entirely.
REPLACE_WITH_DIROWNER – This is quite likely the same value as REPLACE_WITH_DIRNAME

This script assumes that you have WHMCS installed as its own website/subdomain. You can probably adjust it to your needs if you run it in a sub-directory. If you make this customization, send it back to me and I’ll add it to the article.

After the upgrade process, you must visit the site, follow the update steps and then go back and delete the “install” directory from the installation.  That should do it!