How to create a Custom Provider for XML Sitemaps in WordPress 5.5

20 Jun 2020 20:27 | Geplaatst door Marcel

WordPress 5.5 will be released in August 2020 and the feature plugin for XML Sitemaps has been merged this June. You can read about the original proposal on the development blog make/core.

This feature generates a `sitemap.xml` in the document root to make it more easy for search engines to index your website.
It will list all pages, posts, tags, categories and users. It also includes support for Custom Post Types and Custom Taxonomies. You can also extend it to index custom datatypes. That could be from a custom plugin that uses its own database table and its own functions to display that.

This blogpost is about creating a custom provider for your own data.
Please understand that for custom post types or taxonomies you don’t have to do anything.

First you create a custom provider, that will generate pagination and the url_list.

/**
 * Get set of sitemap link data.
 *
 * @since 5.5.0 (use your plugin version)
 */
if ( class_exists( 'WP_Sitemaps_Provider' ) ) {

class WP_Sitemaps_My_Plugin extends WP_Sitemaps_Provider {

	/**
	 * WP_Sitemaps_My_Plugin constructor.
	 *
	 * @since 5.5.0 (use your plugin version)
	 */
	public function __construct() {
		// Use ofcourse your own names here.
		$this->name        = 'plugin_prefix';
		$this->object_type = 'plugin_prefix';
	}


	/**
	 * Gets a URL list for a sitemap.
	 *
	 * @since 5.5.0 (use your plugin version)
	 *
	 * @param int    $page_num       Page of results.
	 * @param string $object_subtype Optional. Default empty.
	 *
	 * @return array $url_list Array of URLs for a sitemap. 
	 */
	public function get_url_list( $page_num, $object_subtype = '' ) {
		// use a function from your plugin to fetch this data
		$pages = plugin_prefix_get_url_list();

		$url_list = array();
		foreach ( $pages as $page ) {

			// No need to add 'lastmod', it is only a hint to search engines.
			$sitemap_entry = array(
				'loc'     => $page,
			);

			$url_list[] = $sitemap_entry;
		}

		return $url_list;

	}


	/**
	 * Gets the max number of pages available for the object type.
	 *
	 * @since 5.5.0 (use your plugin version)
	 *
	 * @see WP_Sitemaps_Provider::max_num_pages
	 *
	 * @param string $object_subtype Optional. Default empty.
	 *
	 * @return int Total page count.
	 */
	public function get_max_num_pages( $object_subtype = '' ) {
		// again, use a function from your own plugin to fetch this data.
		$pages = plugin_prefix_get_my_pagination();

		return count( $pages );

	}
}
}

This is all for the Custom Provider that you need. I do hope you understand that you need to code your own pagination and your list of urls :)

Now to integrate it with the Sitemaps in WordPress 5.5 you can use 2 ways.
First option is to use a filter and add the instance of your Provider class.

function plugin_prefix_wp_sitemaps_register_providers( $providers ) {

	if ( class_exists( 'WP_Sitemaps_Provider' ) ) {
		$provider = new WP_Sitemaps_My_Plugin();
		$providers['plugin_prefix'] = $provider;
	}

	return $providers;

}
add_filter( 'wp_sitemaps_register_providers' , 'plugin_prefix_wp_sitemaps_register_providers' );

The other option is to call a procedural function call in an action hook.

function plugin_prefix_wp_sitemaps_register_providers() {

	if ( function_exists('wp_register_sitemap') && class_exists( 'WP_Sitemaps_Provider' ) ) {
		$provider = new WP_Sitemaps_My_Plugin();
		wp_register_sitemap( 'plugin_prefix', $provider );
	}

}
add_action( 'init', 'plugin_prefix_wp_sitemaps_register_providers' );

Both these hooks to register your custom provider should end up the same.

WordPress 5.5 will be released in August 2020 and the feature plugin for XML Sitemaps has been merged this June. You can read about the original proposal on the development blog make/core. This feature generates a `sitemap.xml` in the document…
Read more...

How to avoid global variables in PHP

19 Jun 2020 21:09 | Geplaatst door Marcel

In PHP variables are usually only available in the scope of a function or a class. For ‘easyness’ people sometimes (often?) revert to global variables. Variables can then become available in the global scope and can be pulled into any function where you might need it.

Problem is dat you might easily overwrite that variable. All you need is a typo like this:

function get_css_class( $post ) {
	global $variable;
	if ( $variable = 126 ) { // do something }
}

And only because you use the `=` for assigning a value to the variable you have now overwritten it, while you only wanted to test it with `==`.

A good way to not use global variables is to use static variables in a function that is both setter and getter. Like this function:

function get_css_class( $post ) {
	static $css_class_static;
	if ( $css_class_static ) {
		return $css_class_static;
	}
	$class = 'anything-i-want'; 
	$css_class_static = $class;
	return $css_class_static;
}

You can see here that if the variable `$css_class_static` was not set before, it gets assigned a value and saved as a static variable. The next time the function gets called, it is already set and it gets returned without having to calculate it again.
This function is both a setter (first time it’s called) and a getter (anytime it is called).
The advantage is that your variable is not in the global scope and is thus modularized and not easily overwritten.
Do pay attention to the fact that this static variable only exists during the same request, just like any global variable.

If you are using WordPress and want to go one step further you can even use the cache by using the `wp_cache_add()` and `wp_cache_get()` functions. Make sure to use keys that are unique to that data.
Also be aware that in most setups this data will only exist during the same request. Be sure to test this though, software like Memcached and Redis might be involved that make cache data survive requests.
Most software systems will have a cache system like this, and you might want to look into it.

In PHP variables are usually only available in the scope of a function or a class. For ‘easyness’ people sometimes (often?) revert to global variables. Variables can then become available in the global scope and can be pulled into any…
Read more...

Chess quote

06 Mar 2020 18:57 | Geplaatst door Marcel

Chess quote:

There are two moves you can make, one is good, one is bad. When you don’t look, you will play the bad move.

Chess quote: There are two moves you can make, one is good, one is bad. When you don’t look, you will play the bad move.
Read more...

Idea density in chess and programming

26 Oct 2019 19:20 | Geplaatst door Marcel

Ideas are a dime a dozen they say, and I can agree with that. Ideas can be cheap and easy, the execution is what matters. An idea can sound great in theory, but in practice it can turn out to be quite different.
Not every idea that gets thought of or spoken out loud will be executed. Most will remain not executed, and for some that might be better.

In chess most ideas can be executed. During a game you can think of hundreds of ideas. Inside the game itself you can execute some, and afterwards in the analysis all of them can be executed on the board, looked at and spoken about. You can talk them over with your opponent; what did you think here, how did you like this move, how would you respond, how do you see this position. You can also talk about the game with other people, be they clubmates or teammates. Positions are easy to put on the board, and the pieces are easy to make moves with and see a new position.
I would assume, the stronger of a chessplayer you are, the more ideas you can think of, make better decisions which ideas are the best, execute them on the board, and from the new position start again with that process. For stronger chessplayers this might cost less mental energy. I am only a clubplayer at a level of about 1500 ELO, and often in my games I see small series of weaker moves, where apparently I am not sharp enough.

I have wondered for some time about chess rating and people who program. I have the anecdotal experience that most people who play chess and are also programmer/developer, are more at clublevel, not master or grandmaster. Just today I thought of something that you could call idea density. In programming you can have maybe a hundred ideas on a day, but you can only execute maybe one, a few, but not often more than a handfull. I can imagine for a strong chessplayer, like around 2000 ELO and above, this can become a bit boring. They might enjoy seeing more ideas come to fruition, and choose a different kind of job that brings them more of what they enjoy and what they are good at.

Ideas are a dime a dozen they say, and I can agree with that. Ideas can be cheap and easy, the execution is what matters. An idea can sound great in theory, but in practice it can turn out to…
Read more...

Classic Stylo theme for PHP-Fusion 9

03 Oct 2019 12:35 | Geplaatst door Marcel

The Stylo theme was a classic desktop theme for PHP-Fusion version 7 and 8. PHP-Fusion 9 comes with themes that are following the modern responsive design. Some users prefer the classic desktop design of Stylo, and in my experience it is mostly users on mobile who prefer Stylo.

On Github I placed a version of Stylo that works on PHP-Fusion version 9.
Take a look at Github for Stylo.

The Stylo theme was a classic desktop theme for PHP-Fusion version 7 and 8. PHP-Fusion 9 comes with themes that are following the modern responsive design. Some users prefer the classic desktop design of Stylo, and in my experience it…
Read more...

Wallpaper with the bridge for trains in Zwolle

29 May 2019 22:35 | Geplaatst door Marcel

I could not find good pictures of the recent train bridge in Zwolle, so I decided to make some pictures. And yes, I do know it is seven years after building the bridge :)

I could not find good pictures of the recent train bridge in Zwolle, so I decided to make some pictures. And yes, I do know it is seven years after building the bridge :)
Read more...