-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetabox.php
More file actions
89 lines (73 loc) · 2.24 KB
/
Copy pathmetabox.php
File metadata and controls
89 lines (73 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Plugin Name: Custom Metabox Plugin
* Plugin URI: https://in.linkedin.com/
* Description: Display input field on front end using checkbox
* Version: 1.0
* Author: Nima
* Author URI: https://twitter.com/?lang=en
**/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( !class_exists( 'WpMetabox' )) {
class WpMetabox{
public function __construct(){
add_action( 'add_meta_boxes',array($this, 'wp_add_author_name'));
add_action( 'save_post', array($this, 'wp_author_name_save' ));
add_filter( 'the_content', array($this, 'wp_author_name_get' ) );
}
/**
create meta box and specify on which post type it should be available and were to display.
*/
public function wp_add_author_name(){
add_meta_box( 'author-details-id', 'Display author details',array($this, 'wp_add_author_form'), 'post', 'side', 'high');
}
public function wp_add_author_form($post){
include plugin_dir_path(__FILE__).'form.php';
}
/**
*save the data collected from meta boxes to db
*/
public function wp_author_name_save( $post_id ){
$inputs = [
'author-name-input',
'author-enable-check',
];
foreach ( $inputs as $input ) {
if (!isset($_POST['author_name_nonce_field']) || !wp_verify_nonce($_POST['author_name_nonce_field'], 'save_author_name')) {
wp_nonce_ays( '' );
}
else{
if ( !current_user_can( 'edit_post', $post->ID )) {
return $post_id;
}
if(isset($_POST['author-enable-check'])){
update_post_meta( $post_id, $input,sanitize_text_field( $_POST[$input] ));
}
if(isset($_POST['author-name-input'])){
update_post_meta( $post_id, $input,sanitize_text_field( $_POST[$input] ));
}
}
}
}
/**
*retrieve data stored in db and display in front end
* @return type content
*/
public function wp_author_name_get($content){
$id = get_the_ID();
$check_box_value = get_post_meta( $id, 'author-enable-check', true );
if($check_box_value == 'True'){
$author_name = get_post_meta( $id, 'author-name-input', true );
return "Author Name : ".$author_name.$content;
}else{
return $content;
}
}
}
$objcheckbox = new WpMetabox();
}else{
exit();
}
?>