]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - user/plugins/sample-page/plugin.php
Best practice: add nonce to the sample plugin
[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 // No direct call\r
12 if( !defined( 'YOURLS_ABSPATH' ) ) die();\r
13 \r
14 // Register our plugin admin page\r
15 yourls_add_action( 'plugins_loaded', 'ozh_yourls_samplepage_add_page' );\r
16 function ozh_yourls_samplepage_add_page() {\r
17         yourls_register_plugin_page( 'sample_page', 'Sample Admin Page', 'ozh_yourls_samplepage_do_page' );\r
18         // parameters: page slug, page title, and function that will display the page itself\r
19 }\r
20 \r
21 // Display admin page\r
22 function ozh_yourls_samplepage_do_page() {\r
23 \r
24         // Check if a form was submitted\r
25         if( isset( $_POST['test_option'] ) ) {\r
26                 // Check nonce\r
27                 yourls_verify_nonce( 'sample_page' );\r
28                 \r
29                 // Process form\r
30                 ozh_yourls_samplepage_update_option();\r
31         }\r
32 \r
33         // Get value from database\r
34         $test_option = yourls_get_option( 'test_option' );\r
35         \r
36         // Create nonce\r
37         $nonce = yourls_create_nonce( 'sample_page' );\r
38 \r
39         echo <<<HTML\r
40                 <h2>Sample Plugin Administration Page</h2>\r
41                 <p>This plugin stores an integer in the option database</p>\r
42                 <form method="post">\r
43                 <input type="hidden" name="nonce" value="$nonce" />\r
44                 <p><label for="test_option">Enter an integer</label> <input type="text" id="test_option" name="test_option" value="$test_option" /></p>\r
45                 <p><input type="submit" value="Update value" /></p>\r
46                 </form>\r
47 \r
48 HTML;\r
49 }\r
50 \r
51 // Update option in database\r
52 function ozh_yourls_samplepage_update_option() {\r
53         $in = $_POST['test_option'];\r
54         \r
55         if( $in ) {\r
56                 // Validate test_option. ALWAYS validate and sanitize user input.\r
57                 // Here, we want an integer\r
58                 $in = intval( $in);\r
59                 \r
60                 // Update value in database\r
61                 yourls_update_option( 'test_option', $in );\r
62         }\r
63 }