]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - user/plugins/sample-page/plugin.php
Core sample plugin: administration page
[Github/YOURLS.git] / user / plugins / sample-page / plugin.php
1 <?php\r
2 /*\r
3 Plugin Name: Sample Admin Page\r
4 Plugin URI: http://yourls.org/\r
5 Description: A example of a plugin administration page to save user defined option\r
6 Version: 1.0\r
7 Author: Ozh\r
8 Author URI: http://ozh.org/\r
9 */\r
10 \r
11 // Register our plugin admin page\r
12 yourls_add_action( 'plugins_loaded', 'ozh_yourls_samplepage_add_page' );\r
13 function ozh_yourls_samplepage_add_page() {\r
14         yourls_register_plugin_page( 'sample_page', 'Sample Admin Page', 'ozh_yourls_samplepage_do_page' );\r
15         // parameters: page slug, page title, and function that will display the page itself\r
16 }\r
17 \r
18 // Display admin page\r
19 function ozh_yourls_samplepage_do_page() {\r
20 \r
21         // Check if a form was submitted\r
22         if( isset( $_POST['test_option'] ) )\r
23                 ozh_yourls_samplepage_update_option();\r
24 \r
25         // Get value from database\r
26         $test_option = yourls_get_option( 'test_option' );\r
27 \r
28         echo <<<HTML\r
29                 <h2>Sample Plugin Administration Page</h2>\r
30                 <p>This plugin stores an integer in the option database</p>\r
31                 <form method="post">\r
32                 <p><label for="test_option">Enter an integer</label> <input type="text" id="test_option" name="test_option" value="$test_option" /></p>\r
33                 <p><input type="submit" value="Update value" /></p>\r
34                 </form>\r
35 HTML;\r
36 }\r
37 \r
38 // Update option in database\r
39 function ozh_yourls_samplepage_update_option() {\r
40         $in = $_POST['test_option'];\r
41         \r
42         if( $in ) {\r
43                 // Validate test_option. ALWAYS validate and sanitize user input.\r
44                 // Here, we want an integer\r
45                 $in = intval( $in);\r
46                 \r
47                 // Update value in database\r
48                 yourls_update_option( 'test_option', $in );\r
49         }\r
50 }