Letting Your Plugin of The FOJ Web Application Create Links for Sitemap

Sitemap is created for The FOJ website. You may want your plugin to add links to the sitemap. Pages and posts will automatically be added to the sitemap so you don't have to create sitemap for pages and posts that your plugin creates. However, if your plugin has other links that are not pages or posts then you may want to add such links to the sitemap.

Before creating links for sitemap, let's learn the structure of a simple plugin:

NOTE: It is important to read this tutorial step by step as it could be difficult to understand the various parts if you jump the steps.

Below is the directory structure of a simple plugin:

Folder structure of a simple plugin of The FOJ

The directories are:
plugins > my_plugin >

  • classes
  • css
  • includes
  • js

The my_plugin directory is the name of an example plugin and the main directory that contains the other directories which are classes, css, includes and js. The my_plugin directory is inside the plugins directory of The FOJ like so plugins/my_plugin.

Not all the directories may be neccessary in your plugin. Also the names of the directories of your plugin don't have to be the same as listed above. You can change them except the inlcudes directory which must remain the same name.

To Have Your Plugin Add Links To The Sitemap, Follow The Steps Below:

  • Create a directory named classes. The name of this directory can be any name but for the sake of this example, let is be classes. Put this directory inside the my_plugin directory like so: plugins/my_plugin/classes

  • Create a PHP file as a class and name it My_Plugin.php. You can name this file any name you want but for the sake of this example, let it be My_Plugin.php. Put this file in the classes directory of your plugin like so: plugins/my_plugin/classes/My_Plugin.php

  • In the My_Plugin.php file, write a class and a method that returns the links for the sitemap as follows:

    Editing plugins/my_plugin/classes/My_Plugin.php
    
    <?php
    class My_Plugin{ //class
    	
    	//Returns links ready for sitemap
    	static function links_for_sitemap(){
    		$sSitemapEntry = '';
    		$sLink1 = 'https://example.com';
    		
    		//add www to $sLink1
    		$sLink1 = www_on_url($sLink1, 'add_www');
    		
    		$sLink2 = 'https://www.google.com';
    		
    		//Sitemap entry for link 1
    		$sSitemapEntry .= "\t" . '<url>' . "\n";
    			$sSitemapEntry .= "\t\t" . '<loc>'; 
    				$sSitemapEntry .=  $sLink1 . '/';
    			$sSitemapEntry .= '</loc>' . "\n";
    		$sSitemapEntry .= "\t" . '</url>' . "\n\n";
    		
    		
    		//Sitemap entry for link 2
    		$sSitemapEntry .= "\t" . '<url>' . "\n";
    			$sSitemapEntry .= "\t\t" . '<loc>'; 
    				$sSitemapEntry .=  $sLink2 . '/';
    			$sSitemapEntry .= '</loc>' . "\n";
    		$sSitemapEntry .= "\t" . '</url>' . "\n\n";
    		
    		return $sSitemapEntry;
    		
    	}
    }
    ?>
    


    In the code above, two links are being prepared for sitemap creation. The method links_for_sitemap() must be static method because the method will be called statically. The function www_on_url() is added on $sLink1 to add "www" to the link. www_on_url($sParamUrl, $sParamAction = 'add_www') will add "www" to a link if the link does not have www and if the link has www, it will not add www to it again. But if you change the second parameter from add_www to no_www or something else, then it will remove www from the link if the link has www. If the link does not have www, it will return the link as it is.

  • The simplest format of the sitemap entry is like this:
    
    <url>
    	<loc>
    		https://www.example.com/ 
    	</loc>
    </url>
    


    The above code is what is being generated in $sSitemapEntry for $sLink1. The same is being done for $sLink2.

  • The method or function that is handling the preparation of the links for sitemap should return the prepared links as it is done in the method links_for_sitemap.

  • Next, write a shortcode for your method or function that will be used to call your method or function.

  • For example, in the code above the class is My_Class and the method is links_for_sitemap so the shortcode is class|My_Plugin|links_for_sitemap. The format for the shortcode is class|className|methodName|argument_1:::argument_2:::argument_3.... Supposing the method links_for_sitemap() expected 3 arguments namely arg_1, arg_2 and arg_3, then the shortcode would be class|My_Plugin|links_for_sitemap|arg_1:::arg_2:::arg_3.

    If a function is written without a class, the format for the shortcode is simply functionName|argument_1:::argument_2:::argument_3.... So for example, if the method links_for_sitemap() was written without a class, then the shortcode would be links_for_sitemap. If the method links_for_sitemap() expected 3 arguments namely arg_1, arg_2 and arg_3, then the shortcode would be links_for_sitemap|arg_1:::arg_2:::arg_3

  • Create a directory named includes. The name of this directory must remain includes and must not change. Put this directory inside the my_plugin directory like so plugins/my_plugin/includes.

  • Create a PHP file named admin_menu.php. The name of this PHP file must remain the same and must not change. Put this file inside the includes directory of your plugin like so plugins/my_plugin/incudes/admin_menu.php

  • In the admin_menu.php write a code to make reference to the shortcode like so:

    Editing plugins/my_plugin/incudes/admin_menu.php
    
    <?php
    $aSitemapFunction[] = 'class|My_Plugin|links_for_sitemap';
    ?>
    


  • Next, create a PHP file named admin_includes.php. The name of this file must remain admin_includes.php and must not change. Put this file inside the includes directory of your plugin.

  • Finally, in the admin_includes.php, write a code to include the file that contains the sitemap method or function. In our example, the file that contains the sitemap method links_for_sitemap() is My_Plugin.php. So, in the admin_includes.php, write a code to make reference to the My_Plugin.php like so:

    Editing plugins/my_plugin/includes/admin_includes.php
    
    <?php 
    
    //Include the My_Plugin.php file in the middle of admin/account.php
    $aPrivatePageMiddle_[] = '../plugins/my_plugin/classes/My_Plugin.php';
    
    ?>
    


  • Once you have added reference in the admin_includes.php to the My_Plugin.php file, the shortcode will work to generate links for the sitemap. $aPrivatePageMiddle_[] is an array of files that should be included in the admin/accounts.php