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


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


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


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.

# ----------------------------
# 2013-12-02
# ----------------------------
configure() {
	MYSQL="/usr/bin/mysql --batch --skip-column-names --max_allowed_packet=100M -u${db_user} -p${db_pass} ${db_name} -e"

replace() {
	"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
		${MYSQL} "update ${db_name}.wp_posts SET post_content = REPLACE(REPLACE(REPLACE(post_content, 't', ' '), 'r', ' '), 'n', ' ') WHERE ID = ${post_id};"




Print Friendly, PDF & Email

Leave a Reply