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:
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 beclasses. Put this directory inside themy_plugindirectory 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 beMy_Plugin.php. Put this file in theclassesdirectory of your plugin like so:plugins/my_plugin/classes/My_Plugin.php
-
In the
My_Plugin.phpfile, write a class and a method that returns the links for the sitemap as follows:
Editingplugins/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 methodlinks_for_sitemap()must bestatic methodbecause the method will be called statically. The functionwww_on_url()is added on$sLink1to 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 fromadd_wwwtono_wwwor 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$sSitemapEntryfor$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_Classand the method islinks_for_sitemapso the shortcode isclass|My_Plugin|links_for_sitemap. The format for the shortcode isclass|className|methodName|argument_1:::argument_2:::argument_3.... Supposing the methodlinks_for_sitemap()expected 3 arguments namely arg_1, arg_2 and arg_3, then the shortcode would beclass|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 simplyfunctionName|argument_1:::argument_2:::argument_3.... So for example, if the methodlinks_for_sitemap()was written without a class, then the shortcode would belinks_for_sitemap. If the methodlinks_for_sitemap()expected 3 arguments namely arg_1, arg_2 and arg_3, then the shortcode would belinks_for_sitemap|arg_1:::arg_2:::arg_3
-
Create a directory named
includes. The name of this directory must remainincludesand must not change. Put this directory inside themy_plugindirectory like soplugins/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 theincludesdirectory of your plugin like soplugins/my_plugin/incudes/admin_menu.php
-
In the
admin_menu.phpwrite a code to make reference to the shortcode like so:
Editingplugins/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 remainadmin_includes.phpand must not change. Put this file inside theincludesdirectory 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 methodlinks_for_sitemap()isMy_Plugin.php. So, in theadmin_includes.php, write a code to make reference to theMy_Plugin.phplike so:
Editingplugins/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.phpto theMy_Plugin.phpfile, the shortcode will work to generate links for the sitemap.$aPrivatePageMiddle_[]is an array of files that should be included in theadmin/accounts.php







