This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Last revision Both sides next revision | ||
hello_world_plugin_extended [2017/01/22 20:53] lokothodida |
hello_world_plugin_extended [2017/01/22 21:21] lokothodida [hello_world/common_functions.php] |
||
---|---|---|---|
Line 124: | Line 124: | ||
===== hello_world/common_functions.php ===== | ===== hello_world/common_functions.php ===== | ||
+ | The first functions that we need to define are ''hello_world_i18n'' (''i18n'' alias), ''hello_world_admin'' (displays the administration panel) and ''hello_world_footer'' (echoes the message into the theme footer). The latter two will just be stubs that get filled in later: | ||
+ | |||
+ | <code php> | ||
+ | // Alias for getting i18n strings for this plugin | ||
+ | function hello_world_i18n($key) { | ||
+ | return i18n_r(HELLO_WORLD . '/' . $key); | ||
+ | } | ||
+ | |||
+ | // Administration panel | ||
+ | function hello_world_admin() { | ||
+ | // ... | ||
+ | } | ||
+ | |||
+ | // Hook for the theme footer | ||
+ | function hello_world_footer() { | ||
+ | // ... | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Now consider the operations that we want to have for the plugin data. The administrator will be able to create a custom message, save it, and then this message will be printed in the theme footer. So we shall create functions for saving this message into a file the ''data/other'' folder. To future-proof this plugin, we will save the data as ''key=>value'' pairs, so the message will be a value with a designated key (ideally the key ''message''): | ||
+ | |||
+ | <code php> | ||
+ | // Given an array of key=>value mappings, save that data to the configuration file | ||
+ | function hello_world_save_config($config = array()) { | ||
+ | $file = HELLO_WORLD_CONFIGFILE; | ||
+ | $contents = json_encode($config); // Encodes the array into a valid JSON string | ||
+ | |||
+ | return file_put_contents($file, $contents); // Saves the JSON string into the configuration JSON file | ||
+ | } | ||
+ | |||
+ | // Load plugin configuration from the JSON file as an array | ||
+ | function hello_world_get_config() { | ||
+ | $file = HELLO_WORLD_CONFIGFILE; | ||
+ | $contents = file_get_contents($file); | ||
+ | $json = json_decode($contents); // Decodes the string | ||
+ | $config = (array) $json; // Converts the object into a PHP array | ||
+ | |||
+ | return $config; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | When the plugin is first loaded, the configuration file might not exist. So let's create some utility functions that detect if the file exists, and which create some default data: | ||
+ | |||
+ | <code php> | ||
+ | // Check that the configuration file exists | ||
+ | function hello_world_config_exists() { | ||
+ | return file_exists(HELLO_WORLD_CONFIGFILE); | ||
+ | } | ||
+ | |||
+ | // Create a default configuration file | ||
+ | function hello_world_init() { | ||
+ | $config = array('message' => hello_world_i18n('HELLO_WORLD')); | ||
+ | |||
+ | return hello_world_save_config($config); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Now we can expand on the administration panel. First we ensure that the configuration file exists: | ||
+ | |||
+ | <code php> | ||
+ | // Administration panel | ||
+ | function hello_world_admin() { | ||
+ | // Initialize the configuration file | ||
+ | $init = hello_world_config_exists() || hello_world_init(); | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Then we will load the configuration data, and load the ''admin.php'' file (which will use this data and display the admin panel): | ||
+ | |||
+ | <code php> | ||
+ | // Administration panel | ||
+ | function hello_world_admin() { | ||
+ | // Initialize the configuration file | ||
+ | $init = hello_world_config_exists() || hello_world_init(); | ||
+ | |||
+ | // still more to add... | ||
+ | |||
+ | // Show the admin panel page | ||
+ | $config = hello_world_get_config(); | ||
+ | include(HELLO_WORLD_PLUGINPATH . '/admin.php'); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | The admin panel will have a ''<form>'' whose method is ''POST'' and whose action is simply the same page. There will be a text field with the name ''message'', and the submit button in said form will have the name ''save-configuration''. So if we want to save the configuration, we will need to check if the ''$_POST'' variable is populated with the ''save-configuration'' key: | ||
+ | |||
+ | <code php> | ||
+ | function hello_world_admin() { | ||
+ | // Initialize the configuration file | ||
+ | $init = hello_world_config_exists() || hello_world_init(); | ||
+ | |||
+ | // Check if we need to save the configuration | ||
+ | $save_changes = isset($_POST['save-configuration']); | ||
+ | |||
+ | if ($save_changes) { | ||
+ | $save_success = hello_world_save_config($data); | ||
+ | } | ||
+ | |||
+ | // Show the admin panel page | ||
+ | $config = hello_world_get_config(); | ||
+ | include(HELLO_WORLD_PLUGINPATH . '/admin.php'); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | Lastly, we need the footer hook to actually load the message: | ||
+ | |||
+ | <code php> | ||
+ | // Hook for the theme footer | ||
+ | function hello_world_footer() { | ||
+ | $config = hello_world_get_config(); | ||
+ | echo '<p>' . $config['message'] . '</p>'; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | This is the full file. It slightly expands on the administration panel by printing error messages and a introducing a placeholder for validating the submitted data: | ||
+ | |||
<file php common_functions.php> | <file php common_functions.php> | ||
<?php | <?php |