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/

Direct link that will delete a WordPress post (PHP)

This is a bit of very badly written PHP code that will add a link under each post.
When clicking on “Delete Post” the post will instantly get deleted.

Only when you’re logged in as a administrator you’ll see the link.
Add this to your themes index.php file somewhere inside the loop.

if (current_user_can('administrator')) {
	
    global $post;
	$thepostid = $post->ID;
	echo "<center>The Post ID: " . $thepostid . " ";
	$thepostdeletelink = get_delete_post_link ( $id = $thepostid, $deprecated = '', $force_delete = false );

	echo "<a href=\"$thepostdeletelink\" target=\"iframedelete\">Delete</a></center>";
	echo '<iframe src="#" name="iframedelete"  width="2" height="2" scrolling="no"></iframe><br>';
}

Use it carefully since when clicked, it deletes the post instantly. That’s all.

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.

Get Latest Post In WordPress (Tiny PHP Code)

Here’s a useful code snippet in PHP for WordPress to get a link to the newest (latest, freshest… you get the idea) post in WordPress.

Code starts here:

$latest = get_posts(array('numberposts' => 1));
$url = get_permalink($latest[0]->ID);
echo "<a href='" . $url . "'>Click here to read our latest post!</a>";

Yeah, that’s it. Have a great day!