User Tools

Site Tools


plugins:creation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
plugins:creation [2011/12/19 23:25]
n00dles101
plugins:creation [2017/01/19 22:08] (current)
lokothodida [Internationalization (I18N)]
Line 7: Line 7:
 This section describes the different parts of a plugin. For this example, lets use this basic plugin that echos "Hello World" at the bottom of every front-end page. [[http://​get-simple.info/​data/​uploads/​hello-world.txt|Download Example Plugin]] This section describes the different parts of a plugin. For this example, lets use this basic plugin that echos "Hello World" at the bottom of every front-end page. [[http://​get-simple.info/​data/​uploads/​hello-world.txt|Download Example Plugin]]
  
-<​code>​+<​code ​php>
 <?php <?php
 /* /*
Line 53: Line 53:
 This code could always be hardcoded, but it's best if you define the below code right after your registration. This code could always be hardcoded, but it's best if you define the below code right after your registration.
  
-<​code>​+<​code ​php>
 # get correct id for plugin # get correct id for plugin
 $thisfile=basename(__FILE__,​ "​.php"​);​ $thisfile=basename(__FILE__,​ "​.php"​);​
Line 60: Line 60:
 This code is what is used to display your plugin on the "​Plugins"​ tab of the control panel. It also allows you to set the type of page your plugin should be shown as. This code is what is used to display your plugin on the "​Plugins"​ tab of the control panel. It also allows you to set the type of page your plugin should be shown as.
  
-<​code>​+<​code ​php>
 # register plugin # register plugin
 register_plugin( register_plugin(
Line 79: Line 79:
 This code is what attaches your custom function to a particular hook or filter. This code is what attaches your custom function to a particular hook or filter.
  
-<​code>​+<​code ​php>
 # activate filter # activate filter
-add_action('​theme-footer','​hello_world',​array()); ​+add_action( '​theme-footer',​ '​hello_world',​ array() );
 </​code>​ </​code>​
  
 **Syntax:​** ​ **Syntax:​** ​
-add_action(//'​hook name'//,//'​function to call'//,//​Array of Args//​); ​+add_action( //'​hook name'//,​ //'​function to call'//,​ //Array of Args// );
  
  
 This code displays a link to a plugin specific page in the administration (it is a special case of //​add_action//​):​ This code displays a link to a plugin specific page in the administration (it is a special case of //​add_action//​):​
  
-<​code>​+<​code ​php>
 # add a link in the admin tab '​theme'​ # add a link in the admin tab '​theme'​
-add_action('​theme-sidebar','​createSideMenu',​array($thisfile,'​Hello World description'​));​+add_action( '​theme-sidebar',​ '​createSideMenu',​ array( $thisfile, 'Hello World description'​ ) );
 </​code>​ </​code>​
  
 **Syntax:​** ​ **Syntax:​** ​
-add_action('//​tab-name//​-sidebar','​createSideMenu',​array($thisfile,'//​link text//'​)); ​+add_action( '//​tab-name//​-sidebar',​ '​createSideMenu',​ array( $thisfile, '//​link text//'​ ) );
  
 **Note:** **Note:**
Line 102: Line 102:
  
 **Note:** **Note:**
-As of version 3.1 you can include multiple menu items on the sidebar by adding an action to the add_action function +As of version 3.1 you can include multiple menu items on the sidebar by adding an **//action//** parameter ​to the ''​add_action()'' ​function: 
-<​code>​+<​code ​php>
 # add a link in the admin tab '​theme'​ # add a link in the admin tab '​theme'​
-add_action('​theme-sidebar','​createSideMenu',​array($thisfile,'​Hello World description'​),'​action1'​);​ +add_action( '​theme-sidebar',​ '​createSideMenu',​ array( $thisfile, 'Hello World description',​ '​action1' ​); 
-add_action('​theme-sidebar','​createSideMenu',​array($thisfile,'​Hello World description 2'),'​action2'​);​+add_action( '​theme-sidebar',​ '​createSideMenu',​ array( $thisfile, 'Hello World description 2', '​action2' ​);
 </​code>​ </​code>​
 This will display 2 menu items on the Theme sidebar, you must check for each action (action1 and action2 above) in your code and display pages accordingly. ​ This will display 2 menu items on the Theme sidebar, you must check for each action (action1 and action2 above) in your code and display pages accordingly. ​
Line 114: Line 114:
 The functions in this section would be the functions that are called as part of the registration,​ a [[plugins:​hooks_filters|hook or a filter]]. The functions in this section would be the functions that are called as part of the registration,​ a [[plugins:​hooks_filters|hook or a filter]].
  
-<​code>​+<​code ​php>
 # functions # functions
 function hello_world() { function hello_world() {
Line 156: Line 156:
       - edit.png       - edit.png
 </​code>​ </​code>​
 +
 +==== Internationalization (I18N) ====
 +If your plugin displays messages to your user/​administrator,​ you will want to allow those messages to be localised to the language of the installation. You do this by having a ''/​lang''​ folder, which has PHP files for each language:
 +
 +<​code>​
 + - lang (directory)
 +    - .htaccess (with one line: Deny from all)
 +    - en_US.php
 +    - de_DE.php
 +    - it_IT.php
 +</​code>​
 +
 +Each file should have one variable: an ''​$i18n''​ array, which has ''​key => value''​ mappings for the various messages:
 +
 +<code php>
 +<?php
 +// en_US.php
 +$i18n = array();
 +$i18n['​PLUGIN_TITLE'​] = 'My Plugin';​
 +...
 +</​code>​
 +
 +To register your i18n array, call ''​i18n_merge''​ with the ID of your plugin (**before** ''​register_plugin''​ is called):
 +
 +<code php>
 +i18n_merge($plugin_id);​
 +</​code>​
 +
 +This registers ''​$i18n''​ for your plugin with the installation'​s language, but you might not have such a language file created. In order to set up a default language, you can register a fixed language if ''​i18n_merge''​ returns ''​false''​.
 +
 +<code php>
 +// Sets the default to English
 +if (!i18n_merge($plugin_id)) i18n_merge($plugin_id,​ '​en_US'​);​
 +</​code>​
 +
 +Or the clearer:
 +
 +<code php>
 +i18n_merge($plugin_id) || i18n_merge($plugin_id,​ '​en_US'​);​
 +</​code>​
 +
 +Once that is done, you can call a localised string by using ''​i18n_r''​ (to ''​return''​ the string) or ''​i18n''​ (to ''​echo''​ it). You do this by prefixing your key with your plugin'​s ID:
 +
 +<code php>
 +// Prints "My Plugin"​ if the language is English
 +i18n($plugin_id . '/​PLUGIN_TITLE'​);​
 +</​code>​
 +
 +For more information,​ go to [[plugins:​i18n|Plugins & Languages (I18N)]].
  
 ==== Data & Settings ==== ==== Data & Settings ====
Line 161: Line 210:
 If you need to save your data to a file on the server, we recommend saving it to a new folder within the ''​GSDATAOTHERPATH''​ path. For example: If your plugin needs to save the Google Analytics'​s UA-XXXXX id for the site, it would be best if you saved it within the folder ''/​path/​to/​getsimple/​data/​other/​my_plugin_folder/​ua-data.txt'',​ where ''/​my_plugin_folder/''​ is the folder you create and ''​ua-data.txt''​ is the file that holds your data. If you need to save your data to a file on the server, we recommend saving it to a new folder within the ''​GSDATAOTHERPATH''​ path. For example: If your plugin needs to save the Google Analytics'​s UA-XXXXX id for the site, it would be best if you saved it within the folder ''/​path/​to/​getsimple/​data/​other/​my_plugin_folder/​ua-data.txt'',​ where ''/​my_plugin_folder/''​ is the folder you create and ''​ua-data.txt''​ is the file that holds your data.
  
-When saving or accessing files and folders within a GetSimple installation,​ it is always best to use the defined constants set by the system. You can get the list of contents from the ''/​admin/​inc/​common.php''​ file, or by looking at our [[http://​code.google.com/​p/​get-simple-cms/​source/​browse/​trunk/​admin/​inc/​common.php|svn copy of it]].+<code php> 
 +// Set up the data 
 +$data = '<​Your Data Here>';​ 
 + 
 +// Set up the folder name and its permissions 
 +// Note the constant GSDATAOTHERPATH,​ which points to /​path/​to/​getsimple/​data/​other/​ 
 +$folder ​       = GSDATAOTHERPATH . '/'​ . $plugin_id . '/';​ 
 +$filename ​     = $folder . '​ua-data.txt';​ 
 +$chmod_mode ​   = 0755; 
 +$folder_exists = file_exists($folder) || mkdir($folder,​ $chmod_mode);​ 
 + 
 +// Save the file (assuming that the folder indeed exists) 
 +if ($folder_exists) { 
 +  file_put_contents($filename,​ $data); 
 +
 +</​code>​ 
 + 
 +When saving or accessing files and folders within a GetSimple installation,​ it is always best to use the defined constants set by the system ​(as illustrated above). You can get the list of contents from the ''/​admin/​inc/​common.php''​ file, or by looking at our [[http://​code.google.com/​p/​get-simple-cms/​source/​browse/​trunk/​admin/​inc/​common.php|svn copy of it]].
  
 ===== Scripts & Styles ===== ===== Scripts & Styles =====
Line 188: Line 254:
 To use your script you must first register it with the system. ​ To use your script you must first register it with the system. ​
  
-<​code>​+<​code ​php>
 // register_script($handle,​ $src, $ver, $in_footer=FALSE) // register_script($handle,​ $src, $ver, $in_footer=FALSE)
 // $handle name for the script, must be unique for each script loaded // $handle name for the script, must be unique for each script loaded
Line 201: Line 267:
 To queue and load a registered script use:  To queue and load a registered script use: 
  
-<​code>​+<​code ​php>
 // queue_script($name,​$where);  ​ // queue_script($name,​$where);  ​
 // $name name of the script to load // $name name of the script to load
Line 212: Line 278:
 To register a script from your plugin folder use  To register a script from your plugin folder use 
  
-<​code>​+<​code ​php>
 register_script('​pluginscriptname',​ $SITEURL.'​plugins/​my_plugin_folder/​your.script.js',​ '​0.1',​ FALSE); register_script('​pluginscriptname',​ $SITEURL.'​plugins/​my_plugin_folder/​your.script.js',​ '​0.1',​ FALSE);
 </​code>​ </​code>​
Line 223: Line 289:
 To use your own stylesheets you must first register them with the system. ​ To use your own stylesheets you must first register them with the system. ​
  
-<​code>​+<​code ​php>
 // register_sstyle($handle,​ $src, $ver) // register_sstyle($handle,​ $src, $ver)
 // $handle name for the style, must be unique for each style loaded // $handle name for the style, must be unique for each style loaded
Line 237: Line 303:
 To queue and load a registered style use:  To queue and load a registered style use: 
  
-<​code>​+<​code ​php>
 // queue_style($name,​$where);  ​ // queue_style($name,​$where);  ​
 // $name name of the style to load // $name name of the style to load
plugins/creation.1324337155.txt.gz · Last modified: 2013/04/19 14:57 (external edit)