Friday, March 6, 2020

Google Compatible Sitemap for Flatpress

EDIT (2021.02.09): This sitemap now comes as part of the standard installation as of Flatpress v1.2, and as such, the following is now simply for historical references ONLY. I am very happy/proud that something I slightly helped towards is now part of Flatpress as standard!

You can also still remove the “?page=” from the static page urls in the now included sitemap by locating the line “$loc = BLOG_BASEURL . ‘?page=’ . $currentstatic;” and simply changing it to “$loc = BLOG_BASEURL . ‘’ . $currentstatic;”.

Recently, I was looking for a plugin to generate a sitemap for my Flatpress blog, and I came across 2. The plugin was called “Dynamic Sitemap for Flatpress” by “Hubert’s World”, and there were 2 versions of it, both of which were created by “Hubert’s World” in 2011 and 2012, respectively.

Sadly, I was never able to get the Dynamic Sitemap plugin working myself, but then after a bit of searching, I came across Igor Kromin’s blog, in which he made his own sitemap code, and it couldn’t be simpler to use (seriously, the guy is a master at Flatpress).

The code I’ll share below also includes his updated version of the code, which allows the sitemap to also index static page links after the blog entries. You can see his linked blog entry for more details on that.

First off, make a file called “sitemap.php” in the root of your flatpress install (where index.php is), and fill it with the following code:

<?php
    require_once('defaults.php');
    require_once(INCLUDES_DIR.'includes.php');
    if (function_exists('system_init')) {
        system_init();
    }
    else {
        plugin_loadall();
    }
    header('Content-Type: text/xml; charset=utf-8');
    error_reporting(E_ALL);
    echo("<?");
?>xml version="1.0" encoding="UTF-8"<?php echo("?>"); ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc><?=BLOG_BASEURL?></loc>
    <lastmod><?=date("c");?></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
<?
    $q = new FPDB_Query(array('start'=>0, 'count'=>-1, 'fullparse'=>true), null);
    while($q->hasMore()) {
        list($id, $e) = $q->getEntry();
        $offset = $fp_config['locale']['timeoffset'];
        if (isset($e['lastupdate'])) {
                $lastmod = $e['lastupdate'] - (60 * 60 * $offset);
        }
        else {
                $lastmod = $e['date'] - (60 * 60 * $offset);
        }
        $loc =  BLOG_BASEURL . "index.php?x=entry:" . $id;
?>
  <url>
    <loc><?=$loc?></loc>
    <lastmod><?=date("c", $lastmod);?></lastmod>
  </url>
<?php
    }

$ss = static_getlist();
foreach($ss as $k => $val) {
  $s = static_parse($val);
  $i = BLOG_BASEURL . '?page=' . $val; 
  
  $offset = $fp_config['locale']['timeoffset'];
  $d = $s['date'] - (60 * 60 * $offset);
  
  $d = date('c', $d);
  echo('<url><loc>' . $i . '</loc><lastmod>' . $d . '</lastmod></url>');
}

?>
</urlset>

Simply save that, and in your browser, go to “YOUR-BLOG-URL.com/sitemap.php” and you’ll have a fully generated sitemap.

Now, if you’re using prettyurls, then the urs in the sitemap won’t match what your blog url usually says… these links will still work, but you’re using prettyurls for a reason, right?!

We can easily fix this by changing a single line of code. In your “sitemap.php” file, find the line that says:

$loc =  BLOG_BASEURL . "index.php?x=entry:" . $id;

And change it to:

$loc = get_permalink($id);

Then, if you want to remove “?page=” from the URLs of your static pages in the sitemap, simply find the line that says:

$i = BLOG_BASEURL . '?page=' . $val;

And change it to:

$i = BLOG_BASEURL . '' . $val; 

That should fix everything!

As an added part to this post, if you don’t want to include Static pages for whatever reason, simply change:

<?php
    }

$ss = static_getlist();
foreach($ss as $k => $val) {
  $s = static_parse($val);
  $i = BLOG_BASEURL . '?page=' . $val; 
  
  $offset = $fp_config['locale']['timeoffset'];
  $d = $s['date'] - (60 * 60 * $offset);
  
  $d = date('c', $d);
  echo('<url><loc>' . $i . '</loc><lastmod>' . $d . '</lastmod></url>');
}

?>
</urlset>

To:

<?php
    }
?>
</urlset>

And that should be it!
A live preview of this blog’s very own sitemap.php can be seen at https://www.clockwor … com/flat/sitemap.php

According to the original blog, this code generates a “Google compatible sitemap”, which is what I think we’re all pretty much after.
You can obviously go on to modify the code as you wish, but this is a good start, I think. :)

Flatpress Disqus Installation Guide

This guide assumes you already have a Disqus site created, and you’ve got your Disqus Universal Code ready. If not, please visit https://disqus.com/profile/signup/ and do that first.

First things first, we’re going to be using the leggero default theme, but this can be done with any theme at all.

Go ahead and open up your theme’s “theme.conf.php”, and underneath the line that says

$theme ['default_style'] = leggero';

You should add the following:

$theme['hassingle'] = true;

That will make sure your blog entries are now displaying as their own entries on their own single page permalinks, which is a must imo.

It took me a good HOUR to find that line of code, and I randomly found it on a flatpress news update about “FlatPress 0.804 Vivace” from all the way back in May 2008 (!!!!), which can be seen here. I really think it should be added to the wiki’s general knowledge on Flatpress. Anyway, back to the guide…

Now you’re going to want to make a new file in the same directory called “single-default.tpl”, and you’re going to want to copy the contents of your “entry-default.tpl” like so, and then we’re going to paste that into our newly created single-default.tpl:

	<div itemscope itemtype="http://schema.org/BlogPosting" id="{$id}" class="entry {$date|date_format:"y-%Y m-%m d-%d"}">
				{* 	using the following way to print the date, if more 	*} 
				{*	than one entry have been written the same day,		*} 
				{*	 the date will be printed only once 				*}

		{$date|date_format_daily:"<h2 class="date">`$fp_config.locale.dateformat`</h2>"}

				<h3 itemprop="name">
				<a href="{$id|link:post_link}">
				{$subject|tag:the_title}
				</a>
				</h3>
				{include file=shared:entryadminctrls.tpl}

				<span itemprop="articleBody">
				{$content|tag:the_content}
				</span>

				<ul class="entry-footer">

				<li class="entry-info">Posted by <span itemprop="author">{$author}</span> at
				{$date|date_format}
				<span itemprop="articleSection">
				{if ($categories)} in {$categories|@filed}{/if}
				</span>
				</li> 

				{if !(in_array('commslock', $categories) && !$comments)}
				<li class="link-comments">
				<a href="{$id|link:comments_link}#comments">{$comments|tag:comments_number} 
					{if isset($views)}(<strong>{$views}</strong> views){/if}
				</a>
				</li>
				{/if}

				</ul>

	</div>

Now we need to do some editing!

Find the following code

				{if !(in_array('commslock', $categories) && !$comments)}
				<li class="link-comments">
				<a href="{$id|link:comments_link}#comments">{$comments|tag:comments_number} 
					{if isset($views)}(<strong>{$views}</strong> views){/if}
				</a>
				</li>
				{/if}

Delete that code, and then underneath the

simply in your Disqus universal code, like below:

<P> <div id="disqus_thread"></div>  
{literal}<script>  
  
/** 
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. 
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/  
/* 
var disqus_config = function () { 
this.page.url = YOUR-SITE-PAGE.URL/YOUR-PAGE.php;  // Replace PAGE_URL with your page's canonical URL variable 
this.page.identifier = YOUR_UNIQUE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable 
}; 
*/  
(function() { // DON'T EDIT BELOW THIS LINE  
var d = document, s = d.createElement('script'); 
s.src = 'https://YOUR-DISQUS-SITE-NAME.disqus.com/embed.js';  
s.setAttribute('data-timestamp', +new Date());  
(d.head || d.body).appendChild(s);  
})();  
</script>  
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>{/literal}  
    </div>  

You can obviously place this where is best suited in your own code if your theme doesn’t contain a “<ul>” here, etc, but towards the bottom after the rest of the content is probably best.

To note, you MUST wrap your Disqus universal code in {literal}{/literal} tags, as script tags in the body of a page that aren’t wrapped in them is going to confuse smarty, and smarty is going to throw errors at you, which is NOT cool. This fixes that, and was something else I had to find out by searching through Smarty support forums. Anyway, back to the guide again…

Now that you’ve done that, in the same folder, you’re going to want to create a new file called “single.tpl”, and then you’re going to want to copy the contents of your index.tpl like so, and then we’re going to paste that into our newly created single.tpl:

{include file=header.tpl}
		<div id="main">

		{entry_block}

			{entry}
			{include file='entry-default.tpl'}
			{/entry}

			<div class="navigation">
				{nextpage}{prevpage}
			</div>

		{/entry_block}
		</div>

		{include file=widgets.tpl}

{include file=footer.tpl}

Now, we’re going to do a bit more editing!

Replace the following line

{include file='entry-default.tpl'}

with

{include file='single-default.tpl'}

Now we need to edit your entry-default.tpl to add the Disqus comments link and comment count. When you’ve opened that file, find the following line

<a href="{$id|link:comments_link}#comments">{$comments|tag:comments_number}

and replace it with

<a href="{$id|link:post_link}#disqus_thread">Comments</a>

And finally, we’re going to add the following discord count script right above the tag in your footer.tpl file:

<script id="dsq-count-scr" src="//flat-1.disqus.com/count.js" async></script>

And there you have it, your Disqus comments system will be fully operational, and not to mention that all of your posts are now on their own pages, making the whole thing a bit nicer. You might have to visit your blog entry at least once for the counter to work, but that is a fault with Disqus and not Flatpress or this modification.

If you just so happen to run into any problems, then I’ll try to help, but it’s a pretty simple guide and I’m sure it will be easy enough to implement!

Tongara’s Flatpress Spoiler Tags Plugin v1.0 Guide

So, I’ve created my first plugin. As per the title, this plugin adds spoiler tags to Flatpress that you can use in posts and static pages. It was made with help from the “BBCode Plugin Tips” on the wiki, and requires the BBCode plugin by Hydra & NoWhereMan that comes as standard with Flatpress.

I found a post on the old Flatpress archive forum asking for this all the way back in 2012, but no one ever answered or took up the challenge. I decided to change that, and you can download my plugin by clicking HERE.

With this post, I’ll teach you how to use my Flatpress Spoiler Tags Plugin.

Below is a standard spoiler tag. You can use it by simply wrapping your content in [spoiler][/spoiler] tags. The spoiler has a default text of “Spoiler”, as shown below:

Spolier

This Content is HIDDEN!

You can define the text of the clickable spoiler field by defining the attribute like [spoiler=YourText] on the opening of the tag. This can be seen below with the word “Spoiler1”:

Spoiler1

This Content is HIDDEN!

Sadly, BBCode doesn’t allow spaces in attributes and will stop parsing after the first word. Luckily, we can use a workaround to add a space to our spoiler attribute using an “&nbsp;” between words. This is shown below with the words “Spoiler 2”:

Spoiler 2

This Content is also HIDDEN!

You can style the spoiler very easily by editing the spoiler.css file found in the plugin’s /res folder. The code uses HTML5’s “<details>” and “<summary>” elements, making it very easy to create hidden content on websites.

Sadly, these elements currently don’t work for users of Internet Explorer, but according to user “Finesse” on THIS post on a StackOverflow question on this issue, adding the following polyfill inside the “<body>” of your blog will fix this:

<script src="//cdn.jsdelivr.net/npm/details-polyfill@1/index.min.js" async></script>

You can generally find the “<body>” tag in your theme’s header.tpl file.

That’s it, I hope you enjoy using the plugin!

Credit of course goes to Hydra & NoWhereMan for their BBCode plugin that comes as standard with Flatpress, the “BBCode Plugin Tips” on the wiki, and of course “Finesse” from StackOverflow for the Internet Explorer fix.