WordPress automatic meta description generation

Sometimes you’re just too lazy to create proper meta descriptions for a post or page and having a good(ish) meta description to display in your SERP is rather important for SEO.

I recently had to write a plugin to generate meta descriptions for all post types. The theory is, when you request the page, I have an action that runs on the

add_action('wp_head', 'xxx');

action. It then checks if you have a meta description for that page (assuming you have Yoast SEO installed), and if not, generates one based on a few parameters and appends it to your

<head></head>

section in your markup.

I’ll give you a base to start off with, this is by no means a complete and final version that you should use on your site. There is a lot more validation that needs to be done, but it’s a start. For now, you can put this in your functions.php file.

The bit of code below will check if you have a Yoast SEO meta description, if not, it will take the first sentence it finds in your content and use that as a meta description. Meta descriptions are 160 characters in length, anything over that will be truncated by Google.

add_action('wp_head', 'my_generate_custom_meta');

function my_generate_custom_meta() {
	global $wp_query;
	$yoast_meta_key = '_yoast_wpseo_metadesc';
	$post_id = get_the_ID();
	$yoast_meta_desc = get_post_meta($post_id, $yoast_meta_key, true);

	if (empty($yoast_meta_desc)) {
		$meta_desc = '';
		$title = strip_tags($wp_query->queried_object->post_title);
		$content = strip_tags($wp_query->queried_object->post_content);

		if (!empty($title) && !empty($content)) {
			$first_sentence = substr($content, 0, strpos($content, '.'));
			$meta_desc = $title . '. ' . $first_sentence;
			update_post_meta($post_id, $yoast_meta_key, $meta_desc);
			echo sprintf('', $meta_desc);
		}
		
	}
}

#Experiences

I’ve always gone through life living very frugal. Saving where I can. Whilst my friends have been living it up, going on holiday to fancy destinations, going on wine tastings, and generally just having fun.  This never really bothered me.  I never spent money on buying the next biggest television or buying a new car every year, things like that never really appealed to me. I enjoy living comfortably and that was enough for me.

A while ago I got married and my wife and I spent the most amazing week in Mauritius. Having our every whim catered to. Amazing. This really made us think. We’re constantly saving up money to buy our own house, or to replace the worn tyres on our car, or to replace a broken appliance. We never really spend money on ourselves, on experiences. Mauritius was an awakening, it showed us that life should be enjoyed.

In 2017, this will be our mission, spend more money on experiences. Sure, we will still save a lot, for other things, but we will take some time, and money, and go do something we normally would not.

Life is short, live a little. #Experiences.

How to fix the target=”_blank” exploit

I was bored whilst waiting for a Git repo to clone so I decided to play around a little with a very old exploit (four or five months old), so I wasn’t expecting much. The exploit I’m talking about is the target=”_blank” one. What happens is that when you open a link in a new tab, you have access to window.opener.location and that is accessible across origins (CORS).

All you need on your site to test the exploit is :

<script>
  if (window.opener) { opener.location = 'https://bukssaayman.co.za/you-have-been-hacked/'; }
</script>

Examples:
Click here : you’ll just see my site open in a new tab
Click here : and watch the original window change to my “you’re hacked page”

I was keen to test this on a few social media sites where I have links to my blog. I never expected Facebook to be susceptible to this exploit but it was one of the first that I found to be susceptible. On my profile’s about page, when you click on my website URL, it opens in a new tab and the old one redirects to my “you’re hacked page”. Crazy.

The possibilities with this is endless. You can setup a spoof page for any social network or site you want to steal users’ credentials for and once they click on your link on the real site, their logged in user will be redirected to your fake site where they will be logged out, asking them to log back in, and so you steal their login details.

I alerted Facebook via their bug bounty program explaining the whole situation. After about 10mins I went to check and they fixed the bug, but had given no response on my ticket I logged. At the time of writing this blog entry, it’s been a few days and I still have not received a response. I’m not expecting an award, just an acknowledgement that I helped them out would be great.

Anyway, if your blog or site is affected by this, contact me to help you sort it out.

WordPress Custom Post Type names restricted to max 20 characters

Ever spotted this error:

Notice
: register_post_type was called
incorrectly
. Post type names must be between 1 and 20 characters in length. Please see
Debugging in WordPress
for more information. (This message was added in version 4.2.) in
XXXX\wp-includes\functions.php
on line
3897

Thrown here:

wp-includes/post.php

	if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
		_doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2' );
		return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) );
	}

So It would appear that due to legacy issues, your custom post type cannot exceed 20 characters.

What are these “legacy issues” I speak of? The post_type field in the wp_posts table in the database where the type of post (your custom post type’s name) is saved, is a VARCHAR(20)!

Screen Shot 07-06-16 at 08.58 AM

I have no words.

Running: Sat, 28 May 2016 11:04:59

I’ve been focusing a lot on weight training the past few months. My leg muscles have increased a lot from squatting 150-260kg. All the added leg muscles aren’t exactly helping me in running. I’m preparing for the Impi Challenge in November 2016. I need to do a lot more running to get fit enough to tackle the 18km route.

  • Activity: Running
  • Distance: 10.28 km
  • Duration: 00:55:43
  • Average Speed: 11.07 kmh
  • Average Pace: 05:25 min/km