exclude_pages = '101,102';
* will exclude pages with ids 101 and 102.
*/
$this->exclude_pages = '';
$this->sitemap_xml = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'sitemap.xml';
$this->robots_txt = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'robots.txt';
$this->cron_event = __CLASS__ . 'cronevent';
register_activation_hook(__FILE__, array(&$this, 'activate'));
register_deactivation_hook(__FILE__, array(&$this, 'deactivate'));
add_shortcode('sitemap', array(&$this, 'output_sitemap'));
add_action($this->cron_event, array(&$this, 'generate_sitemap_xml'));
if (!file_exists($this->robots_txt))
$this->generate_robots_txt();
}
function activate() {
wp_schedule_event(time(), 'hourly', $this->cron_event);
}
function deactivate() {
wp_clear_scheduled_hook($this->cron_event);
}
function generate_robots_txt() {
$home_xml = trailingslashit(home_url()).'sitemap.xml.gz';
$data = "User-agent: *\n";
$data .= "Disallow:\n";
$data .= "Sitemap: $home_xml";
file_put_contents($this->robots_txt, $data);
}
function generate_sitemap_xml() {
$sitemap_data = $this->get_sitemap_xml_content();
file_put_contents($this->sitemap_xml, $sitemap_data);
/* gzip sitemap */
$gzf = gzopen($this->sitemap_xml.'.gz', 'w9');
gzwrite($gzf, $sitemap_data);
gzclose($gzf);
}
function get_sitemap_xml_content() {
$urls = array(home_url());
/* collect links to pages */
$args = array(
'exclude' => $this->exclude_pages
);
$pages = get_pages($args);
foreach ($pages as $page)
$urls[] = get_page_link($page->ID);
/* collect links to posts */
$archive_query = new WP_Query('posts_per_page=1000');
while ($archive_query->have_posts()) {
$archive_query->the_post();
$urls[] = get_permalink($post->ID);
}
/* reset post data */
wp_reset_postdata();
/* generate output */
$output = "\n";
$output .= "