Move WordPress database

Description:

Moving an existing WordPress database is the easiest solution for changing website domain, duplicating content and so on, when we don’t want to lose posts, configurations and other meta data. However, this brings along also the connection to the former parent url, and that might cause issues with wp-admin and other internal paths.

Solution:

WordPress maintains home and siteurl information in the options database table, which can be updated so:

select * from wp_options where option_name = ‘siteurl’\G;

UPDATE wp_options set option_value = ‘http://new-website’ where option_name = ‘siteurl’;

select * from wp_options where option_name = ‘home’\G;

UPDATE wp_options set option_value = ‘http://new-website’ where option_name = ‘home’;

Then, all urls will point to the new site. To set the guid of imported posts, just update -replace the url in the posts:

UPDATE wp_posts SET guid = REPLACE(guid, “http://old-site”, “http://new-site”) WHERE guid LIKE “%http://old-site%”;

 

Posted in SQL, Wordpress | Tagged , , | Leave a comment

Grant privileges with MySql

Description:

Grant privileges on a number of databases containing the same prefix.

Solution:

The tricky part is to take care of the right quotes to use, in particular the backtick ( ` )

Syntax is then as follows:

GRANT ALL ON `wordpress_%`.* TO ‘user’@’localhost’ IDENTIFIED BY ‘longasspassword’;

ALL can be, of course whichever required from SELECT DELETE INSERT….. .

Also use a strong password generator, like http://strongpasswordgenerator.com/ is much better then anything you could come up with.

Posted in Uncategorized | Leave a comment

Ignore file permissions with git

Description:

Running a git status command shows modified files, with only changed file permissions. This can be due the IDE, for example PHPStorm is known for being a bit too nosy.

diff –git a/myproject/index.php b/myproject/index.php

old mode 100644
new mode 100755

Solution:

Git configuration contains an entry that defines if file permission changes should be considered as changes or not. Do disable this, just run the following:

git config core.filemode false

 

Posted in Version control | Tagged , , , , , | Leave a comment

Query posts in WordPress

Description:

Querying through the posts can be useful for creating special container pages. Returning the posts you require is only a matter of using the right attributes.

Solution:

A query in WordPress looks like a series of GET variables:

query_posts(‘posts_per_page=-1&cat=’.7.’&orderby=title&order=ASC’)

This will return all posts that are in category 7, and will order them alphabetically.

WordPress offers also other functions that help query for the right posts:

have_posts(); – boolean, determines if the query was successful

the_post(); – focuses on one post from the iteration, allows different operations

get_the_category(); – returns the category of the post

get_the_title(); – returns the title of the focused post

More example under: https://codex.wordpress.org/Function_Reference/

Posted in Wordpress | Tagged , , , , , | Leave a comment

Secure copy files between hosts

Description:

Use scp (secure copy) to copy data between different servers, from local or remote hosts.

Solution:

Syntax for secure copying is easy, you just need to provide the source and destination of the file/folder.

Depending where you are copying to and from, you might need to specify the host name.

Copy from remote server to local is like this:

$ scp username@host:foobar.txt local/directory

$ scp root@192.123.123.111:/root/htdocs/file.txt /myfolder/projects/www

Same with remote-to-remote or local to remote.

Posted in Linux, Tools | Tagged , , , , | Leave a comment

Create symlink

Description:

A symlink (or symbolik) link allows creating an alias for a file or folder in a different folder with a different name. This can come in handy for accessing special folders in the structure that don’t need to be made public, or creating special rules or exception in server access.

Solution:

A symlink is created easy with the following command:

$ ln -s {/path/to/file-or-folder} {symlink-alias}

For example:

$ ln -s /htdocs/myproject/important_folder /htdocs/mylink

Then we can set up for example a location block in our nginx configuration, that points to that folder:

location /visible_link/ {
root /htdocs/mylink;
}

Posted in Linux, Tools | Tagged , , , | Leave a comment

Run PHP Compatibility checks for upgrading version

Description:

In order to bring the PHP version up to date, we need to run compatibility test on our projects.

Solution:

The CodeSniffer tool that can be installed on each system gives us the most precise results. Follow the guide to have the CodeSniffer up and running (skip the steps you don’t require):

1. CodeSniffer requires PEAR, so if you don’t have it, you can install it:

$ pear

This should output the manual, or if it is not installed, the “unknown command” error. For the latter, you need to install PEAR:

$ lynx -source http://pear.php.net/go-pear | php

Now PEAR is installed.

2. Install the CodeSniffer (first check with command $ phpcs if is not already there):

pear install PHP_CodeSniffer-2.0.0a1

3. Add the PHPCompatibility standard to CodeSniffer:

cd /usr/share/php/PHP/CodeSniffer/Standards
git clone git://github.com/wimg/PHPCompatibility.git PHPCompatibility

4. Go to your project and run the compatibility tests:

phpcs –standard=PHPCompatibility –extensions=php,phtml *

If you receive “Fatal error: Maximum function nesting level of ‘100’ reached, aborting!”, you need to add/edit the value of xdebug.max_nesting_level in your php.ini file:

$ locate php.ini

$ vim /etc/php5/cgi/php.ini

xdebug.max_nesting_level = 1000

xdebug.max_nesting_level of 1000 should be enough for any medium to big size project.

5. Read and fix the errors presented in the output of the test

 

 

Posted in Best practices, Linux, PhP | Tagged , , , , , | Leave a comment