Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » WordPress

WordPress Remove Extra Line Breaks

Submitted by on December 2, 2013 – 3:09 pm

Frequently, imported posts or posts created by auto-blogging plugins will create unnecessary tabs, newlines, and line breaks. This has a negative impact on readability and aesthetics of your site. If you cannot address the problem at its source, there is a relatively simple way of fixing the issue retroactively.

Below is a quick script that will scan all your posts matching the predefined slug and replace all tabs, newlines, and line breaks with spaces. In most cases, the end result would be smoother-flowing text. Having said that, you should back up your database before making any changes.

The script matches the slug with wildcards (LIKE “%${slug}%”). This works well if you want to make changes to posts in multiple categories that share a similar name. Alternatively, you can replace this with explicit matching.

#!/bin/bash
# ----------------------------
# igor@krazyworks.com
# 2013-12-02
# ----------------------------
configure() {
	db_name="database_name"
	db_user="dbusername"
	db_pass="dbuserpass"
	slug="slug_keyword"
	MYSQL="/usr/bin/mysql --batch --skip-column-names --max_allowed_packet=100M -u${db_user} -p${db_pass} ${db_name} -e"
}

replace() {
	${MYSQL} 
	"SELECT ID FROM ${db_name}.wp_posts p 
	LEFT OUTER JOIN ${db_name}.wp_term_relationships r ON r.object_id = p.ID 
	LEFT OUTER JOIN ${db_name}.wp_term_taxonomy x ON x.term_taxonomy_id = r.term_taxonomy_id 
	LEFT OUTER JOIN ${db_name}.wp_terms t ON t.term_id = x.term_id 
	WHERE p.post_status = 'publish' 
	AND p.post_type = 'post' 
	AND t.slug LIKE '%${slug}%' ;" | sort -n | while read post_id
	do
		${MYSQL} "update ${db_name}.wp_posts SET post_content = REPLACE(REPLACE(REPLACE(post_content, 't', ' '), 'r', ' '), 'n', ' ') WHERE ID = ${post_id};"
	done
}

# RUNTIME

configure
replace

 

Print Friendly, PDF & Email

Leave a Reply