|
1
|
|
|
<?php |
|
2
|
|
|
namespace NirjharLo\WP_Plugin_Framework\Src; |
|
3
|
|
|
|
|
4
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Build a sample metabox in editor screen |
|
8
|
|
|
* |
|
9
|
|
|
* @author Nirjhar Lo |
|
10
|
|
|
* @package wp-plugin-framework |
|
11
|
|
|
*/ |
|
12
|
|
|
if ( ! class_exists( 'Metabox' ) ) { |
|
13
|
|
|
|
|
14
|
|
|
final class Metabox { |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Add Meetabox |
|
19
|
|
|
* |
|
20
|
|
|
* @return Void |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct() { |
|
23
|
|
|
|
|
24
|
|
|
//Adding the metabox. For custom post type use "add_meta_boxes_posttype" action |
|
25
|
|
|
add_action( 'add_meta_boxes', array( $this, 'register' ) ); |
|
|
|
|
|
|
26
|
|
|
add_action( 'save_post', array( $this, 'save' ), 10, 2 ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Metabox callback |
|
32
|
|
|
* |
|
33
|
|
|
* @return Html |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function register() { |
|
36
|
|
|
|
|
37
|
|
|
add_meta_box( |
|
|
|
|
|
|
38
|
|
|
'meta-box-id', |
|
39
|
|
|
esc_html__( 'MetaBox Title', 'textdomain' ), |
|
|
|
|
|
|
40
|
|
|
array( $this, 'render' ), |
|
41
|
|
|
// Declare the post type to show meta box |
|
42
|
|
|
'post_type', |
|
43
|
|
|
'normal', |
|
44
|
|
|
'core' |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Metabox render HTML |
|
51
|
|
|
* |
|
52
|
|
|
* @return Html |
|
53
|
|
|
*/ |
|
54
|
|
|
public function render() { |
|
55
|
|
|
|
|
56
|
|
|
wp_nonce_field( basename( __FILE__ ), 'metabox_name_nonce' ); ?> |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
<p> |
|
59
|
|
|
<label for="metabox_name"><?php _e( "Custom Text", 'textdomain' ); ?></label> |
|
|
|
|
|
|
60
|
|
|
<br /> |
|
61
|
|
|
<input class="widefat" type="text" name="metabox_field_name" id="metabox_field_name" value="<?php echo esc_attr( get_post_meta( $object->ID, 'metabox_name', true ) ); ?>" /> |
|
|
|
|
|
|
62
|
|
|
</p> |
|
63
|
|
|
<?php |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Save the Metabox post data |
|
69
|
|
|
* |
|
70
|
|
|
* @param Array $atts |
|
71
|
|
|
* |
|
72
|
|
|
* @return Html |
|
73
|
|
|
*/ |
|
74
|
|
|
function save( $post_id, $post ) { |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
//Check if doing autosave |
|
77
|
|
|
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
//Verify the nonce before proceeding. |
|
80
|
|
|
if ( !isset( $_POST['metabox_name_nonce'] ) || !wp_verify_nonce( $_POST['metabox_name_nonce'], basename( __FILE__ ) ) ) return; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
//Get the post type object. |
|
83
|
|
|
$post_type = get_post_type_object( $post->post_type ); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
//Check if the current user has permission to edit the post. |
|
86
|
|
|
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
if ( isset( $_POST['metabox_field_name'] ) ) { |
|
89
|
|
|
update_post_meta( $post_id, 'metabox_field_name', sanitize_text_field($_POST['metabox_field_name']) ); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} ?> |
|
94
|
|
|
|