I haven’t posted something code orientated in a while and I thought this is a great snippet to share seeing as it’s something I’ve actually been working on.
Oh, now you’re intrigued?
I’m building an SEO Page Analyser web application. Why? Because I hate the darn ugly interfaces. So I’m not really building it because I need it, I’m building it because I want a better interface.
It will have the same functionality as other SEO site audit tools, just with a cleaner and more user-friendly UI.
Anyway, it’s been a complete learning curve for me building something that’s completely out of my depth and thought it could be of use to some of you (my lovely readers).
Let’s dive in shall we.
You’ll need two pages:
- index.php (or whatever you want to call it e.g. form.php) – this page will hold your form, which we are going to use to add a website URL too.
- analyse.php – this page will receive the form data and display the meta description held within that pages source code.
Form page (index.php)
So let’s get coding the index.php file:
<form method="GET" action="analyse.php"> <input type="url" name="target_url" placeholder="Enter a URL..." /> <input type="submit" value="ANALYSE" /> </form>
We’re using the GET method with our form so we can pass the form data into the URL. This allows users to visit the analyse.php page at a later date using a link with the passed form data within the URL.
Using the POST method would only allow the user to view the page until the page is closed. They would then have to fill the form out again to reach their dedicated analyse.php page with their passed data.
We’re then using the input element and giving it a type of URL. This will only automatically give us some form validation because you won’t be able to access the analyse.php page without entering a valid URL.
You’ll need to fill out the ‘name’ attribute for the input element as this is how we’ll call the passed URL.
And because I wanted to give the option to click a submit rather than a keyboard press, I added in a submit. Remember, you don’t need to have a submit button with only one form element.
Don’t forget to close off your elements properly either. That bugs me.
Display meta description (analyse.php)
Moving on to our second and last file, the analyse.php file which will display our meta description.
<?php $url = $_GET["target_url"]; $meta = get_meta_tags( $url ); $description = $meta['description']; echo '<p>' . $description . '</p>'; ?>
Firstly we’re going to retrieve the URL from the form within a variable called $url. We’re grabbing the URL that was passed through from the form via the predefined variable $_GET and calling the input name attribute (which I mentioned earlier).
We then need to set another variable which uses the get_meta_tags function and adding the URL variable we just set. We add this because we’re defining the location of where we want the meta description to be pulled in from.
Please note, you can actually add a standalone URL here rather than passing data:
<?php $meta = get_meta_tags( 'https://www.google.co.uk/' ); ?>
Next we want to define our output so we can display it later. To do this, we’re creating yet another variable and defining what meta type we want to pull in. Obviously we’re using the meta description, but there are other types you can define:
- Geo Position
And finally, we display the URLs meta description. We do this with echoing the variable we set above. I added HTML paragraph tags as I don’t want it displaying raw text, you should do it too. Don’t be an ugly coder.
Post special: displaying the meta description’s character count
For all of you that wanted to go that extra mile and display the character count of the meta description like SEO Mofo’s SERP Tool does, then read on.
<?php $url = $_GET["target_url"]; $meta = get_meta_tags( $url ); $description = $meta['description']; echo '<p>' . $description . '</p>'; // Character count $char_count = strlen($description); echo '<p>Character count: ' . $char_count . '</p>'; ?>
We’ve added two new lines of code now. First we’re going to create a new variable so we can display the character count. In order to display the character count, we need to use the strlen function and assign to pull the character length of our description (which is the string).
And then we’re displaying it using the echo constructor and calling out the $char_count variable to display the count.
That’s all from me folks. Any questions or problems with the snippets above, then please leave a comment.