1

I would like to show the title of the URL page (such as: https://www.google.it/) given as parameter in my custom shortcode plugin. Here is my code:

function shortcode_out($atts) {
    $atts = shortcode_atts( array(
        'link' => '/',
        'newtab' => false
    ) , $atts);


    if ($atts['newtab'] == true)
        return '<a target=_blank href='.$atts['link'].'>'.{GET_TITLE_OF_$atts['link']}.'</a>';
    else
        return '<a href='.$atts['link'].'>'.{GET_TITLE_OF_$atts['link']}.'</a>';
}

How can I achieve this?

Joe
  • 245
  • 2
  • 4
  • 13

1 Answers1

3

External URL's

You will have to grab the contents of the webpage, and grab the title out of it. Be wary of this though as it can significantly slow down the loading speed of your page depending on how many links you are trying to grab and how long their servers take to pass the content.

Doing this also requires you to parse HTML with regex which is generally something to avoid.

This is what the end result will look like:

function shortcode_out($atts) {
    $atts = shortcode_atts( array(
        'link'   => '/',
        'newtab' => false
    ) , $atts);

    //get the URL title
    $contents = file_get_contents($atts['link']);
    if ( strlen($contents) > 0 ) {
        $contents = trim(preg_replace('/\s+/', ' ', $contents));
        preg_match("/\<title\>(.*)\<\/title\>/i", $contents, $title);
        $site_title = $title[1];
    } else {
        $site_title = 'URL could not be found';
    }


    if ($atts['newtab'] == true)
        return '<a target=_blank href='.$atts['link'].'>'.$site_title.'</a>';
    else
        return '<a href='.$atts['link'].'>'.$site_title.'</a>';
}

Internal URL's

If you want to grab an internal URL, then there's actually a WordPress function that can handle this for you: url_to_postid(). Once you have the post ID, you can use get_the_title() to retrieve the post title like this:

$post_id    = url_to_postid($url);
$title      = get_the_title($post_id);

This is what the end result would look like:

function shortcode_out($atts) {
    $atts = shortcode_atts( array(
        'link'   => '/',
        'newtab' => false
    ) , $atts);

    //get the post title
    $post_id    = url_to_postid($atts['link']);
    $title      = get_the_title($post_id);

    if ($atts['newtab'] == true)
        return '<a target=_blank href='.$atts['link'].'>'.$title.'</a>';
    else
        return '<a href='.$atts['link'].'>'.$title.'</a>';
}

url_to_postid will return int(0) if it can't resolve the URL, so if you wanted to be extra careful, you could always change the $title variable to check first like this:

$title = ($post_id ? get_the_title($post_id) : 'Post could not be found');
Frits
  • 7,341
  • 10
  • 42
  • 60
  • Unfortunately it does not work in this way. The link I give as parameter is not a wordpress one. For example, it might be an external URL such as: https://www.google.it/ . So I need to get the title of the page www.google.it – Joe Jun 09 '18 at 07:45
  • 1
    Oh I see! Give me a second, will edit my answer with that option :) – Frits Jun 09 '18 at 07:47
  • Sure! Thanks for helping :) – Joe Jun 09 '18 at 07:48
  • 1
    Anytime @Joe - have a look, I've added the external URL grabbing method to the top of the answer. I've left the internal option in for anyone else who stumbles upon this. :) – Frits Jun 09 '18 at 08:01
  • Are there any other ways to parse the HTML page and get the title of the page? – Joe Jun 09 '18 at 09:06
  • The only other way that I can think of off the top of my head is using php's [`get_meta_tags($url)`](http://php.net/manual/en/function.get-meta-tags.php) and grabbing the twitter title, like this: `$title = get_meta_tags($url)['twitter:title']` The only problem with this is if the site doesn't have their meta tags set up, this won't work. So you might want to use a conditional to check if there is a `['twitter:title']` (although VERY few sites don't have this...) in the tag array, and if not, scrape the title out of the html... – Frits Jun 09 '18 at 09:13