This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
hello_world_plugin_extended [2017/01/21 20:06] lokothodida [Create the file structure] |
hello_world_plugin_extended [2017/01/22 20:53] lokothodida |
||
---|---|---|---|
Line 7: | Line 7: | ||
* Show how to save data to the ''data/other/'' path | * Show how to save data to the ''data/other/'' path | ||
- | ===== Create the file structure ===== | + | ===== File structure ===== |
+ | Create the following files/folders: | ||
<code> | <code> | ||
- hello_world.php | - hello_world.php | ||
Line 20: | Line 22: | ||
===== hello_world.php ===== | ===== hello_world.php ===== | ||
+ | Here is where we register the plugin. We shall start out by defining some useful constants (this will be cleaner than defining global variables like ''$thisfile'' and referencing them later - just give the constants informative names): | ||
- | <file php hello_world.php> | + | <code php> |
<?php | <?php | ||
+ | // === Constants === | ||
+ | define('HELLO_WORLD', basename(__FILE__, '.php')); // Plugin ID | ||
+ | define('HELLO_WORLD_PLUGINPATH', GSPLUGINPATH . HELLO_WORLD . '/'); // Path to the plugin's assets folder | ||
+ | define('HELLO_WORLD_CONFIGFILE', GSDATAOTHERPATH . 'hello_world_config.json'); // File name for the plugin's user-configuration file | ||
+ | </code> | ||
+ | |||
+ | Next, we load the internationalized strings that are in the plugin's ''/lang'' directory (which we will define soon): | ||
+ | |||
+ | <code php> | ||
+ | // === Internationalization === | ||
+ | i18n_merge(HELLO_WORLD) || i18n_merge(HELLO_WORLD, 'en_US'); // Loads English language file by default | ||
+ | </code> | ||
+ | |||
+ | We will define a number of helpful functions in a separate file, ''common_functions.php'', and load them at this point: | ||
+ | |||
+ | <code php> | ||
+ | // === Load functions === | ||
+ | require_once(HELLO_WORLD_PLUGINPATH . 'common_functions.php'); | ||
+ | </code> | ||
+ | |||
+ | Then we register the plugin. Note that this uses one of the assumed functions that we will define, ''hello_world_i18n'', where there will be language-dependent strings (namely for the plugin name and description): | ||
+ | |||
+ | <code php> | ||
+ | // === Plugin, Hook and Filter Registration === | ||
+ | register_plugin( | ||
+ | HELLO_WORLD, // Plugin id | ||
+ | hello_world_i18n('PLUGIN_NAME'), // Plugin name | ||
+ | '0.1.0', // Plugin version | ||
+ | 'You', // Plugin author | ||
+ | 'http://www.you.com', // Author website | ||
+ | hello_world_i18n('PLUGIN_DESCRIPTION'), // Plugin description | ||
+ | 'theme', // Page type - on which admin tab to display | ||
+ | 'hello_world_admin' // Main function (administration) | ||
+ | ); | ||
+ | </code> | ||
+ | |||
+ | Lastly, we register the plugin hooks - in this case, one for loading a sidebar menu item, and another for the theme footer: | ||
+ | |||
+ | <code php> | ||
+ | // Echoes message in footer | ||
+ | add_action('theme-footer', 'hello_world_footer'); | ||
+ | |||
+ | // Creates sidebar link | ||
+ | add_action('theme-sidebar', 'createSideMenu', array(HELLO_WORLD, hello_world_i18n('PLUGIN_SIDEBAR'))); | ||
+ | </code> | ||
+ | |||
+ | This is the full file: | ||
+ | |||
+ | <file php hello_world.php> | ||
<?php | <?php | ||
// === Constants === | // === Constants === | ||
Line 55: | Line 107: | ||
===== hello_world/lang/en_US.php ===== | ===== hello_world/lang/en_US.php ===== | ||
+ | The PHP files in the ''/lang'' directory just need to define an ''$i18n'' array of internationalized strings. Once we've merged the plugin's language strings with ''i18n_merge'', these strings will be available via ''i18n('hello_world/STRING_NAME', true)'', which we will alias ''hello_world_i18n('STRING_NAME')'' | ||
<file php en_US.php> | <file php en_US.php> | ||
Line 70: | Line 123: | ||
</file> | </file> | ||
- | ===== hello_world/admin.php ===== | + | ===== hello_world/common_functions.php ===== |
- | <file php admin.php> | + | |
- | <h3><?php echo hello_world_i18n('ADMIN'); ?></h3> | + | |
- | + | ||
- | <form method="post"> | + | |
- | <input name="message" value="<?php echo $config['message']; ?>"> | + | |
- | <button name="save-configuration"><?php echo hello_world_i18n('SAVE'); ?></button> | + | |
- | </form> | + | |
- | </file> | + | |
- | + | ||
- | ==== hello_world/common_functions.php ==== | + | |
<file php common_functions.php> | <file php common_functions.php> | ||
<?php | <?php | ||
Line 169: | Line 212: | ||
echo '<div class="' . $class . '">' . $message . '</div>'; | echo '<div class="' . $class . '">' . $message . '</div>'; | ||
} | } | ||
+ | </file> | ||
+ | |||
+ | ===== hello_world/admin.php ===== | ||
+ | Lastly we define the look of the administration panel. | ||
+ | |||
+ | <file php admin.php> | ||
+ | <h3><?php echo hello_world_i18n('ADMIN'); ?></h3> | ||
+ | |||
+ | <form method="post"> | ||
+ | <input name="message" value="<?php echo $config['message']; ?>"> | ||
+ | <button name="save-configuration"><?php echo hello_world_i18n('SAVE'); ?></button> | ||
+ | </form> | ||
</file> | </file> |