Auto add ALT to WordPress featured images

I found this bit of PHP code that worked fine. What it does is that if your featured images in WordPress have empty ALT tags, then it will use the WordPress post title to fill them.

Add the following code to your theme functions.php file. And not the WordPress functions.php file…

function lwp_37481_featured_alt_text($metadata, $object_id, $meta_key, $single) {
    if(isset($meta_key) && $meta_key == '_wp_attachment_image_alt'
        && get_post_thumbnail_id() == $object_id
        && $single === true
    ){
        $original_value = get_post_meta(
            $object_id, '_wp_attachment_image_alt', false
        );
        if(empty($original_value)){
            return get_the_title();
        }
    }
    return $metadata;
}
add_filter('get_post_metadata', 'lwp_37481_featured_alt_text', 10, 4);

I got the code from this guy: https://letswp.io/post-title-alt-text-featured-image/

Fix: WordPress Sitemap Error: “XML declaration allowed only…”

Have you seen a error similar to the one below while trying to access your WordPress Sitemap?

This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

Wordpress sitemap.xml error

I’ve looked around and one guy solved the problem by removing some PHP-code that indicates the PHP code ending or whatever you call it “?>” in his themes functions.php file.

Because apparently he had 2 of them which caused the sitemap error for him and someone else too in that thread.

But that was not the problem for me. The problem was because some idiot (me) had made a plugin and left linebreaks and spaces in the plugin PHP-file.

So check your WordPress plugins. Deactivate one at a time and hit F5.

Have fun! 😉

Move WordPress to new domain with full URL redirects

Disclaimer: If you break something, too bad. You’re responsible for your actions. This worked for me.
Take backups of your database and files for both domains!

It’s usually not recommended to switch domains since search engines might take some time or not index your moved stuff at all for some reason. Also, frequent visitors might stop visiting your site and old links might stop working after a while if you let the old domain expire…

But lets say you really want to move the domain.
First, it’s important that you use the same permalink settings on both sites otherwise this will not work.

Ok, lets begin, login to WP Admin.
Go to “Tools” and choose “export”.

Export everything and save it as a XML-file.
Don’t worry about the pictures and stuff, WordPress will download that later automatically.

Now, go to your new site and under “Tools” you’ll see “Import”.

Just import it, it might take a while if your site is huge. Don’t click the button twice.

Is the Import OK?
Good. If not… Well, Google and find out what’s wrong.

If it’s good, then login to SFTP/FTP or whatever and open your “.htaccess”-file on the old server and place this bit of code there:

# BEGIN WordPress

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.nordicnode\.com
RewriteRule (.*) https://www.nordicnode.com/$1 [R=301,L]

# END WordPress

Replace my domain name with yours by the way. 😉
Hopefully you got it working with the permanent redirects now!

Add a “Delete Post” Link to your WordPress Posts!

Add a simple delete link right on your website that is running WordPress. Tested with WordPress version 5.1.

Since the PHP code is written by me it looks like crap, but it doesn’t really matter since the admin is the only one who will see it.

Add this code inside the WordPress loop and reload your website.
If you have logged in as Admin you should see a “[Delete Post]” link somewhere.

When you click it a dialog will pop up, asking you to confirm to delete the post.
I didn’t want the page to reload every time I did delete stuff, that’s why the small iframe is added. It will simply load the delete link inside it. (Told you it was crap code, but it works.)

WordPress Code:
<?php if ( is_super_admin() ) { ?>
<p><a onclick="return confirm('Do you want to delete this post?')" href="<?php echo get_delete_post_link( $post->ID ) ?>" target="deleteframe">[Delete Post]</a></p><iframe name="deleteframe" width="200px" height="35px"><a href="https://www.nordicnode.com/">NordicNode</a></iframe>
<?php } ?>

That’s it!

Force HTTPS and/or WWW on your WordPress site!

As you probably know by now. Google is starting to add a new ranking factor to their ever-changing algorithm.

It’s SSL that they want us to use but it’s a good thing since the data you send and receive from a server will be encrypted and a bit harder for people that might try to gather information like credit card numbers, passwords and other sensitive information.

First, make sure you’re happy with your Permalinks settings. Once that is done, open the file named “.htaccess” and after taking a backup of it, change it to the similar bit of “code” below:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# Force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
</IfModule>
# END WordPress

Just add the “Force www” part for adding www if you want it. The SSL part below is what’s important.

Save the file and try going to some of your old URLs.