/* function to create sitemap.xml file in root directory of site */
add_action("wp_insert_post", "eg_create_sitemap");
add_action("publish_post", "eg_create_sitemap");
// add_action("publish_page", "eg_create_sitemap");
add_action( "save_post", "eg_create_sitemap" );
function eg_create_sitemap() {
global $wpdb;
$post = get_post();
if ( str_replace( '-', '', get_option( 'gmt_offset' ) ) < 10 ) {
$tempo = '-0' . str_replace( '-', '', get_option( 'gmt_offset' ) );
} else {
$tempo = get_option( 'gmt_offset' );
}
if( strlen( $tempo ) == 3 ) { $tempo = $tempo . ':00'; }
$postsForSitemap = get_posts( array(
'numberposts' => 50,
'orderby' => 'modified',
'post_type' => array( 'product' ),
'order' => 'DESC'
) );
$sitemap .= '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= "\n" . '<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> <channel> <title>GM Products</title> <link>http://gadgets-mall.com</link> <description> The Gadgets Mall is the perfect online store for anyone looking for electronic gadgets, cameras, headphones & earphones, MP3 players, hi-fi speaker systems, GPS devices, smart phones, laptops, tablets, accessories, Alexa and Google devices as well as renewed electronics. </description>' . "\n";
foreach( $postsForSitemap as $post ) {
setup_postdata( $post);
$postdate = explode( " ", $post->post_modified );
$sitemap .= "\t" . "<item>" . "\n";
// $id = $post->ID;
$sitemap .= "\t\t" . "<g:link>" . get_permalink( $post->ID ) . '</g:link>';
// $sitemap .= "\t\t\t<news:publication_date>" . $postdate[0] . 'T' . $postdate[1] . $tempo . " / " . $postdate[0] . 'T' . $postdate[1] . $tempo . "</news:publication_date>\n";
$sitemap .= "\t\t\t" . '<g:title>' . htmlspecialchars(get_the_title( $post), ENT_XML1 | ENT_QUOTES, 'UTF-8') . '</g:title>' . "\n";
$sitemap .= "\t" . "<g:id>" . $post->ID . "</g:id>" . "\n";
// htmlspecialchars($string, ENT_XML1 | ENT_QUOTES, 'UTF-8');
$content = $post->post_content;
$excerpt = wp_trim_words( $content, 100);
$sitemap .= "\t\t\t" . '<g:description>' . htmlspecialchars($excerpt, ENT_XML1 | ENT_QUOTES, 'UTF-8');
$sitemap .= get_the_title( $post) . '</g:description>' . "\n";
$thumbnail_ID = get_post_thumbnail_id($post->ID);
$thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'large');
if (is_array($thumbnail)) {
$thbm = array_shift(explode('?', $thumbnail[0]));
//$thbm = str_replace( 'https://', 'https://i0.wp.com/', $thbm );
$sitemap .= "\t\t\t" . '<g:image_link>' . $thbm . '</g:image_link>' . "\n";
$price = $wpdb->get_row( $wpdb->prepare(
"SELECT salePrice FROM gm_ads_products WHERE post_id = %d",
$post->ID
) );
$price = $price->salePrice;
$priceold = $wpdb->get_row( $wpdb->prepare(
"SELECT price FROM gm_ads_products WHERE post_id = %d",
$post->ID
) );
$priceold = $priceold->price;
$sitemap .= "\t\t\t" . '<g:price>' . $priceold . ' USD</g:price>' . "\n";
$sitemap .= "\t\t\t" . '<g:sale_price>' . $price . ' USD</g:sale_price>' . "\n";
// $nextyear = mktime(date("Y")+1);
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($postdate[0])) . " + 31 day"));
$sitemap .= "\t\t\t" . '<g:sale_price_effective_date>' . $postdate[0] . 'T' . $postdate[1] . $tempo . '/' . $newEndingDate . 'T' . $postdate[1] . $tempo . '</g:sale_price_effective_date>' . "\n";
$brand = $wpdb->get_row( $wpdb->prepare(
"SELECT * FROM gm_ads_attributes WHERE (attr_name LIKE '%Brand Name%' OR attr_value LIKE '%Brand Name%') AND post_id = %d",
$post->ID
) );
$brand = $brand->attr_value;
$sitemap .= "\t\t\t" . '<g:brand>' . htmlspecialchars($brand, ENT_XML1 | ENT_QUOTES, 'UTF-8') . '</g:brand>' . "\n";
$mpn = $wpdb->get_row( $wpdb->prepare(
"SELECT product_id FROM gm_ads_ali_meta WHERE post_id = %d",
$post->ID
) );
$mpn = $mpn->product_id;
$sitemap .= "\t\t\t" . '<g:mpn>' . $mpn . '</g:mpn>' . "\n";
}
$sitemap .= "\t" . '<g:condition>new</g:condition> <g:availability>in stock</g:availability></item>' . "\n";
}
$sitemap .= '</channel></rss>';
$fp = fopen( ABSPATH . "products.xml", 'w' );
fwrite( $fp, $sitemap );
fclose( $fp );
}