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