Đây là code hoàn chỉnh việc lấy dữ liệu từ 1 link bài viết ở báo VnEXpress để đưa vào trang WordPress tự động.
Code được viết bằng php, cách sử dụng chỉ việc lưu dưới dạng php và đưa lên thư mục gốc của WP:
<?php
include_once('simple_html_dom.php');
$url = 'https://vnexpress.net/ba-me-lai-xe-khap-nuoc-my-truy-tim-ke-sat-hai-con-gai-4386126.html';
$html = new simple_html_dom();
$html->load_file($url);
#$str ->load($html ->save());
#echo $html;
$tieude = $html->find('title',0);
#66: star
#59: tên store and ID
#56: Chỉ lấy ảnh trong đó thì có icon
$content = $html->find('div',57);
$content1 = $content->find('h1',0);
//Get Tiêu đề
$get_tieude = $html->find('h1',0);
//Get icon
$get_des = $html->find('p.description',0);
$get_content = $html->find('article.fck_detail',0);
$get_content1 = $get_content->find('img',0);
$get_content2 = $get_content->find('p');
$get_content3 = $get_content->find('p,img');
?>
<h1>Tiêu đề: <?=$get_tieude->plaintext?></h1>
<h1>Des: <?=$get_des->plaintext?></h1>
<?=
$x1="";
$count = count($get_content3);
for ($i = 0; $i < $count; $i++) {
if ($get_content3[$i]->getAttribute('data-src') != null) {
$xx=$get_content3[$i]->getAttribute('data-src');
$x1=$x1 . "<img src='" . $xx . "'>";
}
else $x1=$x1 . $get_content3[$i];
}
/*
for ($i = 0; $i < $count; $i++) {
if ($get_content3[$i]->getAttribute('data-src') != null) {
?> <img src='<?=$get_content3[$i]->getAttribute('data-src');?>' alt="Icon"><?php
}
else echo $get_content3[$i];
}
*/
?>
<h2>------------------</h2>
<h1>Content: <?=$get_content1->getAttribute('data-src')?></h1>
<h1>Content: <? $x1; ?></h1>
<h2>------------------</h2>
<?=
require(dirname(__FILE__) . '/wp-load.php');
$text=$x1;
global $user_ID;
$new_post = array(
'post_title' => $get_tieude->plaintext,
'post_content' => $text,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'taxonomies' => array('post_tag'),
'post_category' => array(6)
);
$post_id = wp_insert_post($new_post);
$count = count($get_content2);
for ($i = 0; $i < $count; $i++) {
echo $get_content2[$i];
}
?>