Author Archives: Keith Slater

How to change your domain name in WordPress

Changing your domain name in WordPress should be an easy task, but for some reason it’s not. All of the site’s assets are and links are hardcoded in the database to the old domain name.

You can easily update the domain name by running a few SQL scripts (I recommend making a backup of your database before doing this) –

UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com');

UPDATE wp_posts SET post_content = replace(post_content, 'http://olddomain.com', 'http://newdomain.com');

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com');

While you’re at it, change your new domain to use https (it has several advantages).


If you have a WordPress site and are looking for help (in St. Louis or not), you can contact me.

Converting a WordPress site from http to https (ssl)

Starting with Chrome 62 (coming out in October 2017), pages that contain input fields will display as Not Secure in Google Chrome. Eventually Chrome plans to label all sites that are not https as Not Secure so this is the perfect time to make the switch. There are other reasons to enable https – it’s more secure & Google search results will give you a slight boost.

Luckily, enabling https is pretty easy to do on most servers. If you host supports it, turn it on and you should be good. There’s also Letsencrypt, which I prefer. Either way, enabling SSL is the way to go.

Some WordPress sites may have problems once you enable SSL. A common problem is “mixed content” warnings. This happens when your https site is also loading data over http. WordPress has a tendency to create static URLs to content. These URLs usually contain http://.

There’s a fairly simple database query that you can run that will fix the majority of those issues with images –

UPDATE wp_posts
SET post_content = (REPLACE(post_content, 'src="http://', 'src="https://'))
WHERE INSTR(post_content, 'jpeg') > 0
OR INSTR(post_content, 'jpg') > 0
OR INSTR(post_content, 'gif') > 0
OR INSTR(post_content, 'png') > 0;

It’s possible that the theme you are using may have hardcoded the assets to http (hopefully not). If that’s the case, then you will have to go through the code to manually figure out what’s going on.

You’ll also want to change your site URL in general settings to be https instead of http –

Switching from http to https in WordPress

Once your site is fully working over https, then you want to force your site to always use https. You can do that through a .htaccess file like this –

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Or you can use any of the numerous WordPress plugins to do this for you.


If you have a WordPress site and are looking for help (in St. Louis or not), you can contact me.

Solving slow curl and wget calls in CentOS

I recently ran into an issue with very slow curl and wget calls. After diagnosing for a while I realized that it had something to do with resolving DNS. My nameserver entries were set as 8.8.8.8 and 8.8.4.4 so I didn’t think the name servers were the problem.

After a lot of trial an error these are the steps I took to fix this-

In /etc/resolv.conf –
options single-request-reopen
nameserver 8.8.8.8
nameserver 8.8.4.4
The single line – options single-request-reopen seems to do most of the heavy lifting. Now after you do that run
sudo service network restart
Check the /etc/resolv.conf file and make sure that the changes are still there. If they are not, then you will need to do this:
In /etc/sysconfig/network-scripts/ifcfg-eth0
#DNS1=8.8.8.8
#DNS2=8.8.4.4
PEERDNS=no
sudo service network restart
Then make the above changes again.
From what I can tell, there is some sort of conflict between ipv4 and ipv6. Even though the above changes will fix the problem, you might also want to disable ipv6 until the bugs are sorted out.
In /etc/sysctl.conf –
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
Then run
sudo sysctl -p
So basically if you’re running into any problems with your server connecting to other servers hopefully this will help. I specifically ran into this issue with WordPress and the Jetpack plugin as well as WordPress’s dashboard.
I’m using CentOS 7 however I believe this issue is in many other Linux distro’s.

Yii 2 – Fix weird page navigation with # (hash) symbol as you go to a new page

I ran into an issue with Yii 2 where I would click a link, it would go to that link but at the same time on the URL a little # symbol would appear then dissapear. It looks like this causes no issues until you try to navigate back. When you hit back, the page wouldn’t load but the URL would go back. Refreshing would fix it but that still isn’t good.

I figured out that this happened when using Pjax. Make sure that any links in a Pjax block have the data-pjax=”0″ parameter. So the URL would need to be like:

<a href=”/site/home” data-pjax=”0″>Home</a>

This is actually documented in the Yii 2.0 manual so if you are looking for more information you can check there.

Get & Send iPhone Location to Server via JSON with iOS 7

I recently made an app that gets a user’s location and sends it to our server. The app will update every 1000 meters when it is active and will update only during major changes when it is inactive. The location will not always been 100% correct or up to date (depending on a lot of circumstances) but if you’re wanting to get an approximate location of your users, this should work pretty well.

This code lives in my AppDelegate since I want it to run no matter what they’re doing in my app.


- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Start location services
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

// Only report to location manager if the user has traveled 1000 meters
locationManager.distanceFilter = 1000.0f;
locationManager.delegate = self;
locationManager.activityType = CLActivityTypeAutomotiveNavigation;

[locationManager stopMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{

// Check if running in background or not
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
isInBackground = YES;
}
NSLog(@"Location Manager isInBackground: %hhd", isInBackground);

if (isInBackground) {

// If we're running in the background, run sendBackgroundLocationToServer
         [self sendBackgroundLocationToServer:[locations lastObject]];
     } else {
         // If we're not in the background wait till the GPS is accurate to send it to the server
if ([[locations lastObject] horizontalAccuracy] < 100.0f) {
             [self sendDataToServer:[locations lastObject]];
         }
     }

}

-(void) sendBackgroundLocationToServer:(CLLocation *)location
{
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
         [[UIApplication sharedApplication] endBackgroundTask:bgTask];
     }];

     // Send the data
     [self sendDataToServer:location];

     if (bgTask != UIBackgroundTaskInvalid) {
         [[UIApplication sharedApplication] endBackgroundTask:bgTask];
         bgTask = UIBackgroundTaskInvalid;
     }
}

-(void) sendDataToServer:(CLLocation *)newLocation
{
    NSLog(@"Sending Data to Server");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // I also want to send the battery level to the server. Get battery level
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    float batteryLevel = [[UIDevice currentDevice] batteryLevel];

    float lat = newLocation.coordinate.latitude;
    float lng = newLocation.coordinate.longitude;
    NSLog(@"Accuracy: %f", newLocation.horizontalAccuracy);
    NSString *userId = [[NSUserDefaults standardUserDefaults] stringForKey:@"userId"];

    // This is the data I am sending to the server
    // I am sending a userID that the server recognizes
    // I am sending the latitude and longitude of the user as well as their speed course and battery life
    // I am also sending the horizontal & vertical accuracy so I can see how accurate the gps location was
    NSString *post = [[NSString alloc] initWithFormat:@"login_id=%@&latitude=%f&longitude=%f&speed=%f&course=%f&battery_level=%f&horizontal_accuracy=%f&vertical_accuracy=%f",
       userId,
       lat,
       lng,
       [newLocation speed],
       [newLocation course],
       batteryLevel,
       newLocation.horizontalAccuracy,
       newLocation.verticalAccuracy];

     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

     NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
     NSString *urlstring = [NSString stringWithFormat:@"%@webservice/post_logins_location.php", kBaseURL];
     [request setURL:[NSURL URLWithString:urlstring]];
     [request setHTTPMethod:@"POST"];
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
     [request setHTTPBody:postData];

     NSError *error;
     NSURLResponse *response;
     NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

     if (!error) {
         jsonResults = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];
         NSLog(@"GPS Send results: %@", jsonResults);
    } else {
         NSLog(@"Error sending GPS data to server");
    }
 });
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Went to Background");
// Only monitor significant changes
    [locationManager startMonitoringSignificantLocationChanges];
}

I chose to use startMonitoringSignificantLocationChanges when in the background. From what I’ve read this only updates when the phone changes radio towers and is not very accurate. For my purposes, this was fine. I’m just trying to get an idea on where the phone is.

And that should do it. Please let me know if I have any syntax errors (I’m sure I do).