Ben Lobaugh Online

I think, therefore I am. I am, therefore I sail

Category: Web Development Page 1 of 30

Cookies disappear after adding Set-Cookies in .htaccess? Here is how to get them back

I needed to set a cookie via .htaccess, to ensure it was always in the browser, regardless of what the website was doing. Setting a cookie is rather easy, it will look similar to:

Header set Set-Cookie "cookieName=value; Expires=Wed, 15 May 2222 07:28:00 GMT"

The next morning I attempted to login to the site and could not. It turned out the login cookies were not being set. The issue was insidious. Re-read the line above, at first glance it seems fine when you want to set a cookie, however if there are other cookies that need to run you need to add the cookie.

This three letter change resolved the issue and allows all the other cookies to operate properly:

Header add Set-Cookie "siglock=hello-world; Expires=Wed, 15 May 2222 07:28:00 GMT"

Canonical URL plugin for WordPress released!

Have you run into this scenario? You have content that needs to live on multiple sites but you are concerned about SEO issues duplicating said content?

As search engines crawl the web looking for pages to include in their search indexes they may run across content duplicated from another site. When that happens the search engine will first attempt to determine which site is the “source of truth”. The site with duplicated content will be penalized in rankings. If no source can be found both sites rankings will be penalized. An SEO nightmare.

Is it possible to distribute the same content to multiple sites without incurring this penalty? The answer is a resounding YES! All you need to do is let the search engine know where the real of the article is, the canonical article. This is done by including a tag in the html head area. It is for search engines, humans do not typically see it.

WordPress has some built in canonical abilities however in WordPress itself there is no way to set the canonical url. I tend to have articles published all over the web that I would like to copy to this site for posterity. Today I whipped up a little plugin that provides a simple url field to add the article’s canonical url in a search engine friendly way.

It is as simple as adding the url as you create the article!

Publish your post and WordPress will handle the rest.

Here is an example post that I wrote on team building for WebDevStudios.

9 Critical Concepts for Leading High-Performance Teams

You may notice there is also a brief disclaimer at the top of the article. That is optional and may be helpful to some readers and/or required by the site you are reposting from to comply with guidelines.

If you want to use this plugin on your own site simply head over to Github and download the plugin!

Find total number of post records on a WordPress Multisite in MySQL

I was recently asked for the total number of posts on a WordPress website. Normally as simple as looking at the number in the dashboard, however in this instance the WordPress install was a Multisite with nearly 100 sites. That would have taken far too long to do manually, but what about having MySQL do the heavy lifting for me?

The information_schema database contains data about every database and table on the server. One of the metrics it stores is the count of rows in a table. We can use that to extrapolate roughly how many entries are in all the posts tables combined. This is not an exact science, however it will get you close to the real numbers. I argue that it will typically be “close enough” on larger installs, such as the one I was looking at.

In the following query, just replace the database name with your database and run it.

SELECT SUM( table_rows )
FROM information_schema.TABLES 
	table_schema = "{{YOUR_DATABASE_NAME}}" 
	AND table_name LIKE '%_posts';

Run a wp-cli command across the network

Need to run a wp-cli command across a network of WordPress sites?

Here is an example of how to add/update an option on every site on the network using wp-cli.

wp site list --field=url | xargs -n1 -I % wp --url=% option update my_option my_value

This is really two commands in one. Lets break them down:

wp site list --field=url

This command will get a list of all the sites on the network. The output is a list of the urls for the given site. Passing the url to a command on a network chooses the site to run the command on.

xargs -n1 -I % wp --url=% option update my_option my_value

Here is the kicker. This command takes the output of the previous command into xargs. Xargs then run the new wp command with each site’s url in it.

Find and update WordPress posts with comments older than NNN days

I have a use case where I need to disable comments on posts in WordPress where the last comment was more than one year, or 365 days, ago.

Posts can be found with this query:

SELECT p.ID, p.post_title, p.post_date, p.comment_status, p.`ping_status`
FROM wp_posts AS p
     SELECT c.comment_post_ID
     FROM wp_comments AS c
     WHERE DATEDIFF( curdate(), c.comment_date ) <= 365

The outer query takes the inner query as the search parameter. The inner query is finding the latest comment for each post based on the number in the WHERE condition. 365 represents 365 days, or 1 year.

Therefore, this query is getting the latest comment for post where the comment is less than 1 year old.

Now let’s say you need to update the posts to disable comments. The goal is to disable commenting for all posts that have not had a comment within the last year. It can be accomplished with the following:

UPDATE wp_posts
SET comment_status='closed', ping_status='closed'
		SELECT c.comment_post_ID
		FROM wp_comments AS c
		WHERE DATEDIFF( curdate(), c.comment_date ) >= 365

Here again the number 365 represents the number of days.

Check the posts and you should see the comments have been closed.

Page 1 of 30

Powered by WordPress & Drip City Coffee