1 | <?php |
||
2 | /* |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
3 | Plugin Name: Sample Admin Page |
||
0 ignored issues
–
show
|
|||
4 | Plugin URI: http://yourls.org/ |
||
0 ignored issues
–
show
|
|||
5 | Description: A example of a plugin administration page to save user defined option |
||
0 ignored issues
–
show
|
|||
6 | Version: 1.0 |
||
0 ignored issues
–
show
|
|||
7 | Author: Ozh |
||
0 ignored issues
–
show
|
|||
8 | Author URI: http://ozh.org/ |
||
0 ignored issues
–
show
|
|||
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> |
||
0 ignored issues
–
show
|
|||
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 | } |
||
64 |