1

Hi I am building a news website. I am trying to automate it.

  1. News url (metabox)
  2. Title
  3. Content area
  4. Image (featured image option on wordpress)

When paste the site url I would like the other metabox to pull the data from the source http://www.bbc.co.uk/news/technology-34527439

I want title and description to pulled form bbc website

  1. News url: http://www.bbc.co.uk/news/technology-34527439
  2. Title: Should be fetched from <title>
  3. Content area: should be from <meta name="description".>
  4. Image: Should be first image on the content

I see you can use php cURL any more pointers to wordpress would be much appreciated http://www.php.net/manual/en/curl.examples-basic.php

Thanks

  • Have you tried using something like RSS Importer to automatically import an RSS feed from the BBC site? – Aric Watson Oct 14 '15 at 12:14
  • @AricWatson we want to add selected urls manually – Shovan Oct 14 '15 at 12:39
0

Here you go:

class UrlToTitleConverter{

    public function convert($Url){
        $response = wp_remote_get($Url);
        if ( is_array( $response ) ) {
            $body = $response['body'];
            $title = substr($body, strpos($body, '<title>')+7);
            $title = substr($title, 0, strpos($title, '</title>'));
        }
        return $title;
    }
}
-1

Try this one

$tags    = get_meta_tags('http://www.bbc.com/news/technology-34527439');
$title   = $tags['twitter:title'];
$content = $tags['description'];
$image   = $tags['twitter:image:src'];

If you're OK using the twitter:title and the twitter:image:src. You'll also have to vet every source you're trying to pull from to see if they have a consistent meta schema. If it alters from the bbc.com site just do a switch statement on some identifier for the site you're pulling tags from to build a data array.

Output from URL provided.

 Array
(
    [description] => The UK's National Crime Agency says cyber-attackers have stolen more than £20m from British bank accounts.
    [x-country] => us
    [x-audience] => US
    [cps_audience] => US
    [cps_changequeueid] => 259204896
    [twitter:card] => summary_large_image
    [twitter:site] => @BBCWorld
    [twitter:title] => Online attackers steal £20m from UK bank accounts - BBC News
    [twitter:description] => The UK's National Crime Agency says cyber-attackers have stolen more than £20m from British bank accounts.
    [twitter:creator] => @BBCWorld
    [twitter:image:src] => http://ichef.bbci.co.uk/news/560/cpsprodpb/37A1/production/_86114241_thinkstock.jpg
    [twitter:domain] => www.bbc.com
    [apple-mobile-web-app-title] => BBC News
    [application-name] => BBC News
    [msapplication-tileimage] => http://static.bbci.co.uk/news/1.91.0426/windows-eight-icon-144x144.png
    [msapplication-tilecolor] => #bb1919
    [mobile-web-app-capable] => yes
    [robots] => NOODP,NOYDIR
    [theme-color] => #bb1919
    [viewport] => width=device-width, initial-scale=1, user-scalable=1
)

FYI - wordpress has a wrapper for cURL that's helpful

wp_remote_get( $url, $args );

https://codex.wordpress.org/Function_Reference/wp_remote_get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.