Archive | WordPress

BASH script to change the Security Keys and SALTs in a wp-config.php file

I wanted to automatically change the Security Keys/SALTS when provisioning a new WordPress site. WordPress.com has a service that spits back random values. (https://api.wordpress.org/secret-key/1.1/salt/). The script below CURLs the values and then modifies a wp-config.php file with the new random values.

SALTS=$(curl -s https://api.wordpress.org/secret-key/1.1/salt/)
while read -r SALT; do
SEARCH="define('$(echo "$SALT" | cut -d "'" -f 2)"
REPLACE=$(echo "$SALT" | cut -d "'" -f 4)
echo "... $SEARCH ... $SEARCH ..."
sed -i "/^$SEARCH/s/put your unique phrase here/$(echo $REPLACE | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/" /Path/To/Your/wp-config.php
done <<< "$SALTS"

Don’t remember where I got the pieces of this, but here it is, I have been using it for a while and it seems to work well.
Hope that helps someone.

Installing WordPress via shell script(BASH)

We have been using a provisioning script that downloads the latest wordpress zip file, extracts into the right location it and sets up the DB connection. I wanted to take it a step further and eliminate the install.php page. The one that looks like this:Screen_Shot_2013-03-06_at_3.54.19_PM-2

 

So i sat down to figure out how to “Install WordPress” via shell script. Here is that command:

SITENAME="blog"
DOMAINNAME="company.com"
PASSWORD="MySecurePassword"
wp_install_result=$(php -r 'define("WP_SITEURL", "http://'$SITENAME.$DOMAINNAME'");define("WP_INSTALLING", true);require_once("./wp-load.php");require_once("wp-admin/includes/upgrade.php");$response=wp_install("TITLE", admin, "[email protected]", false, null, "'$PASSWORD'");echo $response;')

 
After WordPress is “installed”, we can now activate plugins.

Activating and deactivating WordPress plugins from a shell script (BASH)

I needed to update my WordPress site provisioning script to download, install and activate a WordPress plugin. The download is the easy part (I just use wget). But how do I activate the plugin. This is what I cam up with:

result=$(php -r 'require_once("./wp-load.php");require_once("wp-admin/includes/admin.php");activate_plugin("hello.php");')

And to be complete, to deactivate:

result=$(php -r 'require_once("./wp-load.php");require_once("wp-admin/includes/admin.php");deactivate_plugins("hello.php");')

SQL query to find number of WordPress posts per week

I wanted to know how many posts I have been creating each week. Here is a quick MySQL query to find out:

SELECT DISTINCT extract(week from date(post_date)) AS WeekNumber, count( * ) AS Posts 
FROM wp_posts where post_type = 'post' AND post_status = 'publish' AND post_date like '%2012-%' 
GROUP BY WeekNumber;

Returns:

+------------+-------+
| WeekNumber | Posts |
+------------+-------+
|          1 |     4 | 
|          2 |     3 | 
|          3 |     3 | 
|          4 |     3 | 
|          5 |     3 | 
|          6 |     2 | 
|          7 |     3 | 
|          8 |     1 | 
|          9 |     2 | 
|         10 |     2 | 
|         11 |     3 | 
|         12 |     2 | 
|         13 |     2 | 
|         14 |     3 | 
+------------+-------+

SQL query to find the number of WordPress posts this year!

I was compiling my year end wrap up (hits, posts, twitter followers), and I realized I did not know how many posts I created this year. I ran the following query again my WordPress database to find out.

select post_date,post_title from wp_posts where post_type = ‘post’ AND post_status = ‘publish’ AND post_date like ‘%2011-%’;

My goal was 2 a week for a year. I ended up with 116.

More on this later.

A script to loop through a WordPress database and search for text

We maintain both a WordPress development environment and a production environment. I have written a script that copies a WordPress site to the local machine. In that script I change the GUID (even thought this page says you shouldn’t), siteurl, and home. I change the GUID, because the site has not been publicly accessible, so I believe the warning on the documentation page does not apply.

One thing that I always wanted to do is make sure that the old (dev) site url is not elsewhere in the database. So I figured out this script below that loops thorough all the tables, and all the columns in the database searching for text, in this case the old site url.

DATABASENAME="mywpdb"
SEARCHTEXT="devURL"

for TABLE in $(mysql --batch --skip-column-names -e "use $DATABASENAME;show tables"); do
for COLUMN in $(mysql --batch --skip-column-names -e "use $DATABASENAME;SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"$TABLE"' AND TABLE_SCHEMA = '"$DATABASENAME"';"); do
query="use $DATABASENAME;select substring($COLUMN,1,100) as '"$TABLE":"$COLUMN"' from $TABLE WHERE $COLUMN like '%"$SEARCHTEXT"%';"
mysql -e "$query"
done
done

Send WordPress post to Google Plus (Not show Google+ on my WordPress site)

I was surprised that I could not find a plugin that would add a new WordPress post to Google+. I saw plugins that showed my Google+ profile card or added a +1 button to my site, but nothing to send a new post automatically to Google+. I can do this with the Twitter Tools plugin to Twitter, but I couldn’t find one for Google+.

I found a workaround – IFTTT. People have been raving about IFTTT.com, and I thought it was interesting, but I didn’t see a need. Then I stumbled on a post described how you can use Google Voice to send an SMS text to Google Plus. With the use of this IFTTH recipe, this post should show up in my Google+.

Let’s see if this works!

UPDATE: FAIL: Google+: Internal error occurred, please try later.

UPDATE2:Been working for a while!

BASH script to email if WordPress plugins or themes need updating

This one took me all day. But I got it. I wanted to have a script that could look through all WordPress sites and find if there are outdated themes or plugins. All I need to do is slap it into a cron job and I am good to go!

Here is the BASH code to do it.

 

UPDATESNEEDED=""

for installpath in $(find /var/www -name wp-config.php)
do
cd $(dirname $installpath)

THEMENEEDED=$(php -r 'require_once("./wp-load.php");
delete_site_transient("update_themes");
wp_update_themes();
$new = get_site_transient("update_themes");
echo count($new->response);')

if [ ! $THEMENEEDED = 0 ]; then
UPDATESNEEDED="$UPDATESNEEDED \n Site $installpath needs $THEMENEEDED theme(s) updated"
fi

PLUGSNEEDED=$(php -r 'require_once("./wp-load.php");
delete_site_transient("update_plugins");
wp_update_plugins();
$new = get_site_transient("update_plugins");
echo count($new->response);')

if [ ! $PLUGSNEEDED = 0 ]; then
UPDATESNEEDED="$UPDATESNEEDED \n Site $installpath needs $PLUGSNEEDED plugin(s) updated"
fi
done

if [ -n "$UPDATESNEEDED" ]; then
echo -e "$UPDATESNEEDED" | mail -s "Updates are needed" [email protected]
fi

I have been needing this script for a while.

php53 included in CentOS 5.6

I wish I read release notes, it would make my life easier. In my previous post, I was worried about CentOS 5.x not having a new enough version of PHP to run the soon to be release WordPress 3.2. Well, RedHat’s 5.6 release notes clearly say:

Version 5.3.3 of PHP is now available in Red Hat Enterprise Linux 5.6 as the separate php53 package

To move to the 5.3 version of PHP, I ran the following commands:

  • yum erase php\*
  • yum install php53 php53-gd php53-mysql php53-pdo php53-mbstring  php53-cli php53-devel php53-common php53-xml

That was easy.

Support for .vcf files in WordPress

Our developers created a website that had vCard files for each staff member. The problem was that when people clicked on them, rather than downloading them, some web browsers displayed the content of the file.

Ended up that apache did not know how to handle “.vcf” files. The change below is now part of my standard setup script:

sed -i.ORIG “/text\/xml-external-parsed-entity/a\text\/x-vcard\t\t\tvcf” /etc/mime.types