Duplicating content across multiple sites on a wordpress network

236168_1670

When managing a WordPress network, there is no obvious way to easily duplicate content across multiple sites (other than copying and pasting, which also means that if you need to update it in all places at once, it needs to be done page at a time too.)

Enter ThreeWP Broadcast. Broadcast allows administrators to broadcast posts to any or all network sites. This duplicates the content as a child (and optionally keeps them linked). While linked, updating the master updates all children, while still allowing children to be edited individually too if you so prefer. It works a treat, the only issue I have with it so far is that there is no quick way to multi broadcast. If I add a new site to the network, and I want to duplicate the 10 shared content pages, I must edit all 10 pages and share them one by one (But this only needs doing when a new site is added, so it’s not the end of the world.)

Tagged with:
Posted in Wordpress

Sysadmin for dummies – Server monitoring

penguin

When I started working as a web developer 10 years ago, I had a basic understanding of Linux and managing a server, but about all that was actually needed was setting up the odd cron job. At the time, the common hosting arrangement was a reseller account. All that was, is an expanded version of the personal accounts most hosting providers offer for 5 to 10 euro per month. The only real difference was that instead of the two or three sites you could host there, you could host hundreds in the same account. It was handy in some ways (All maintenance was carried out by the hosting provider, it was cheaper and less work than a dedicated server, etc) and a pain in others (Want imagemagik installed? If it’s not there already, tough shit, best case scenario is you put in a request for it and it’s done months later. Same goes for modern versions of PHP, etc.).

Disclaimer: I am by no means a server admin, I know enough to muddle by, and I have not broken anything (yet).

In recent years this has changed, most companies (That I deal with anyway) seem to have moved away from that model, and instead offer VPS or cloud accounts. This is great for some reasons, (need something installed? Go ahead and do it!) and a pain in others (Need something installed? How the hell am I supposed to do that?). This has forced me, over time, to learn more and more about system administration and server management. What I would have dealt with a few years ago by sending in a support request, I now have to deal with myself. This includes everything from dealing with spammers, to hackers running exploit scanners or SQL injection attacks, to basic server admin (Updating PHP, managing backups, mail queue management etc).

So I figure I will do a series of posts on some basic things that I have picked up over time. I figure I will start with some tools that I use regularly to monitor the server. These are useful for a number of reasons.

top

The first of these tools is a shell command, top, top basically gives you a look at what is running on the system (Specifically, the items using up the most resources). It’s very similar to the task manager you might be familiar with if you work with windows in your day-to-day life.

The display is updated in real-time. pressing q quits out of it (And like most Linux commands, you can cancel out of it with ctrl+c too). By default, the output is ordered by the amount of CPU that the process is using, to change how it’s sorted, you generally use the < and > keys. This can vary from distro to distro though. So, for example, to see what process is using up the most memory, hit > once, press < to get back again.

I will generally use this for a couple of different things, the most common reason is if the server is running unusually slow, top is always the first thing I will check, in the past I have found everything from the plesk antivirus tool (Dr.Web) eating up loads of my ram, to a perl based denial of service tool running on a compromised FTP account. Now that you have identified the culprit, time to track down where it is.

This is pretty easy, to get a full path to the process, type ls -l /proc/$PID | grep exe replace $PID with the process id (The number in the PID column on the left of your top display.)

You can also stop the process by typing kill $PID or if that does not work, kill -9 $PID.

If it’s a service that’s gone a bit wrong, take a quick google to find out how to stop it, or see if you can find other people experiencing the same issue, if it’s something more malicious, I would suggest changing the usernames and passwords to any FTP accounts that point there, and take a look at the logs (Explained in a later post), and remove the file.

mod_status

The second tool I use is the Apache module mod_status. mod_status is great, it allows you to see a snapshot of how the httpd is doing. It shows you all current requests that are running on it. The process id, the amount of resources that is using, the originating IP, etc. mod_status is a great place to spot things like denial of service attacks, compromised accounts using php based mass mailers. broken php pages that are using up too many resources, or to find things like someone running SQL injection attacks made possible because you are such an awful programmer.

It’s pretty easy to set up. First thing we need to do is install enable it. On my install (cloudlinux running plesk) it’s at /etc/httpd/conf/httpd.conf, but if it’s not there try locate httpd.conf. Edit the file (I’ll supply a dummies intro to vi at a later date) and find the commented out section that refers to ‘server-status’. (The line will specifically be something like “<Location /server-status>”). Uncomment it, and change it to the following

Just replace $YOUR_IP_ADDRESS with your own ip address. You can now access the statistics by going to http://example.com/server-status (If you are running WordPress on the domain, you may need to make some .htaccess changes, as WordPress, generally, redirects all traffic to index.php)

Posted in Linux

Speeding Up Opencart – Using the cache class

broken-cart

Open cart actually has a built-in caching class, although it does not use it everywhere that it should. It’s quite easy to carry out, and when used appropriately, you should see some pretty major speed gains (On sites with very large databases anyway).

The caching opencart uses is pretty simple, it basically takes some information (usually an array, but potentially anything), serialises it and sticks it in a file. This allows us to cut down on the number of database queries we are running as we can just check if someone else has run the same query in the last hour, if they have, we just give them the stored results. The best time to use the caching is to store results of any query that runs on a very regular basis and is likely to return the same results now as it will in an hours time. For the example below I will use the category module (Which displays the categories in the sidebar).

/system/library/cache.php contains 4 relevant methods

The constructor basically works as a cleanup function, it checks the cache directory (/system/cache) for any cached information that’s more than an hour old, and if it finds anything, it deletes it. This keeps your information relevant. This runs automatically.

The get function is very simple. $this->cache->get(‘CACHED_ITEM_NAME’); This will return either the cached information (if it exists) or nothing.

The set function stores information in the cache. $this->cache->set(‘CACHED_ITEM_NAME’, ‘CACHED_ITEM_VALUE’);

And finally, the delete function : $this->cache->delete(‘CACHED_ITEM_NAME’); removes the item from the cache (This is handy if you want to avoid having to wait an hour for the cache to expire after changing things like category order in the backend.

Let’s take a look at our file (I am working with 1.5.1.3.1) open up catalog/controller/module/category.php, it’s a pretty simple controller, all it does is runs through the categories and sticks them in an array. The view file then sticks them into the sidebar. This block of code, which runs multiple SQL queries (depending on how nested your categories are) starts at line 29

We can fix this with just a couple of lines of code

All we are doing here is wrapping this entire section of code in an if block, and first checking if the file exists in the cache. If it does not, we run it like normal, and then store it to the cache. If it does, use the cached version instead. Simple as that, and you can use it anywhere that requires it with just a few lines of code.

Tagged with:
Posted in Opencart, PHP

Date formatting in MySQL

840874_86751664

For many years, I worked with MySQL on a pretty basic level, I carried out inserts, updates and deletes, and that’s about it. I think it might be beneficial (if anyone even reads this thing) to do a series of posts on some of the things that I was not aware of when I started off, that might be of help to others.

One of the staples in my classes folder was a function that takes a date from MySQL (In any of the relevant formats, usually datetime though) and then formats it (Using substr() or strtotime() and date()) with PHP into a nice human readable format for display on the website.

There is a more elegant solution however.

MySQL has a number of functions in place for dealing with dates and times so you can return the nicely formatted date direct from the database!

Most importantly for this example we have DATE_FORMAT.

In your database, you have a table news_stories that has a field date with a type of ‘date’

This will return a nice easy to read date (Such as 29th June, 2008) in your result set.

Tagged with: ,
Posted in PHP

Calculating the date a week starts, given the week number.

cal

I came across this problem recently while developing a time tracking application.

Typically, I have found that when viewing time sheets people prefer to be able to see the week at a glance. For this reason I will normally store them in a database with three int (day (From 1-7), Week (From 1-53) and year (From 0001 to 9999)). This allows me to easily pull a two-dimensional array from the database for output into the virtual time sheet

Now, this is all well and good, and it’s fine for most of us. But generally a customer will want to see something like:

Time sheet for week beginning 23rd of June 08

This also makes it easier from a useability perspective when displaying monthly or annual views. If you are looking to track down a time sheet for last February, it’s easier to jump to it if you are looking at it by date and not by number.

Looking at the php date related functions, there is no real obvious way to handle this. But don’t worry. We can do it!

The international standards for dealing with dates and times are covered by ISO 8601, and here is an easy to understand Wikipedia article on it.

So if you take a look at the week dates part of it, it gives us some mutually equivalent definitions for week 01

  • the week with the year’s first Thursday in it.
  • the week with 4 January in it.
  • the first week with the majority (four or more) of its days in the starting year.
  • the week starting with the Monday in the period 29 December – 4 January.

So, the easiest way to work with this from PHP, is to find out what date Jan 4th + $week_number weeks is. Assuming your current week number is stored in the variable $week, and the current year is stored in the variable $year this can be done as follows.

strtotime() is a very handy function that allows you to turn a variety of human readable date formats into UNIX timestamps. As you can see above it also allows you to perform additions and subtractions on it.

To breakdown that line, what I am doing is getting the time-stamp for the 4th of January this year + this week (less 1) weeks. The reason I subtract 1 up above is because January 4th is in week 1, not week 0.

So now we have a date in this week, but it’s not necessarily the Monday. But that’s fairly easy to find out from here with the following line.

All I am doing there is saying “Give me a time stamp for this date – (However many days we are past Monday) days.”.

So now we have our time stamp for the Monday of the relevant week. So let’s clean it up a bit and stick it in a function.

Now, all I have done here is added in functionality to get it to return a nice easy to read date. Just pass it the exact same formatting you would send to the php date() function.

Hope this helps someone out there!

Tagged with:
Posted in PHP

Disabling a WordPress Plugin via MySQL

php

If you somehow manage to block yourself out of the admin panel (In our case recently it was done with the fantastic WLB plugin by RightHere over on Code Canyon.) don’t despair. It’s actually pretty simple to disable some, or all plugins using phpmyadmin which most control panels have installed by default, or if not SQLbuddy will do the job just as well.

Once you are into your database, you are looking for the active_plugins row in the wp_options table. Either browse to it, or use the sql query below

If you edit that field, you should see something like this

That’s the serialised array now in our database. That’s just how an array is stored by php when passed through the serialize function.

The a:2 at the start tells us that it’s an array of 2 items and the blocks like i:0;s:50:”google-analytics-for-wordpress/googleanalytics.php”; list items of the array.

From here, we can empty or delete this row (Which will disable all plugins (But will keep their settings)) or edit the serialized array. In the case above, I deleted the second half of it (i:1;s:45:”white-label-branding/white-label-branding.php”;) and changed the 2 after the first a: to 1.

 

Tagged with:
Posted in PHP, Wordpress

Finding large log files

log

Recently we had an issue where the server was running low on space, It looked like some of the sites on the server were not rotating logs correctly, and I needed to find out which ones.

The following shell command, when run in the root of your web directory (In my case, /var/www) will find all log files over 100mb and list them.

 

Posted in Linux

Getting WordPress out of Maintenence Mode

level

Another quick fix.

I tried to update a theme there earlier, which failed. WordPress dumped the entire site (Including wp-admin) into maintenance mode with no obvious way to fix it.

Deleting the .maintenance file that appeared in the root directory of the site fixed it and brought it back.

Tagged with:
Posted in Wordpress

Speeding up OpenCart – Quick Fix

cart

We have been using open cart for e-commerce projects for a while now, most of the sites have a few hundred products, and it does the job fine. Recently however we experienced a massive slow down when dealing with a much larger project catelog (Approx 2500 in about 50 different categories). Before I started, the page load was 35.35 seconds on the home page. Now, this is running on the pretty slow dev server anyway, but 35 seconds is a bit insane.

After a quick dig, I found what I think is the culprit.

In /httpdocs/catalog/controller/module/category.php (The file that generates the category menu we use down the side of the site) there is the line

This appears twice, in a default install it’s on line 44 and 58 (Once for categorys, and once for sub categorys.) This is then, in the array below, passed back out as part of the category name.

This is what displays the little number next to the category name showing how many products are in the category. It’s a pretty small feature, which I don’t think adds much to the user experience, but the impact that it is having on page speed is pretty high. On every page load, it is running the getTotalProducts approx 50 odd times, so let’s just comment it out.

The improvement this has made has been pretty drastic. The load time of the home page is now down to just over 5 seconds. It’s still slow, so I need to look into some form of caching to bring it down further, but it’s still a pretty drastic improvement for all of 30 seconds work.

For your reference, the changed block of code now reads as follows (Lines 33 – 66)

 

 

Tagged with:
Posted in Opencart, PHP