-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjdm-custom-cta.php
183 lines (147 loc) · 5.21 KB
/
jdm-custom-cta.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Plugin Name: JDM Custom CTA
* Plugin URI: http://labs.jdmdigital.co/code/jdm-custom-cta/
* Description: There are many cases when theme developers may want the ability to add a call-to-action (or CTA) button to their theme that's easily editable from the WordPress backend. This reusable plugin does just that, and nothing more.
* Version: 0.9
* Author: JDM Digital
* Author URI: http://jdmdigital.co
* License: GPLv2 or later
* GitHub Plugin URI: https://github.com/jdmdigital/jdm-custom-cta
* GitHub Branch: master
*/
// Adds a box to the main column on the Post edit screen.
function jdm_add_meta_box() {
$screens = array( 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'jdm_sectionid',
'Call to Action (CTA)',
'jdm_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'jdm_add_meta_box' );
// Prints the box content.
function jdm_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'jdm_meta_box', 'jdm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$ctaurl = get_post_meta( $post->ID, 'ctaurl', true ); // the URL
$ctatxt = get_post_meta( $post->ID, 'ctatxt', true ); // the text
echo '<p style="margin-bottom:1em;">Setup custom call to action (CTA) buttons here. Just paste the URL (starting http:// or https://) and enter the text the button should say. If you want to remove the button, just remove the URL and the button will disappear when you click "Update."</p>';
echo '<table class="form-table"><tbody>';
echo '<tr>';
echo '<th scope="row"><label for="ctaurl">Button Link:</label></th>';
echo '<td><input type="url" id="ctaurl" class="large-text" name="ctaurl" value="' . esc_attr( $ctaurl ) . '" placeholder="http://" /></td>';
echo '</tr><tr>';
echo '<th scope="row"><label for="ctatxt">Button Text:</label></th>';
echo '<td><input type="text" id="ctatxt" class="normal-text" name="ctatxt" value="' . esc_attr( $ctatxt ) . '" placeholder="Click Here" style="" /></td>';
echo '</tr></tbody></table>';
echo '<p><b>Note:</b> Leave these fields blank to remove the button from this page.</p>';
}
/**
* When the post is saved, saves our custom data.
* @param int $post_id The ID of the post being saved.
*/
function jdm_save_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['jdm_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['jdm_meta_box_nonce'], 'jdm_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
/* OK, it's safe for us to save the data now. */
// Sanitize user input.
$ctaurl = sanitize_text_field( $_POST['ctaurl'] );
$ctatxt = sanitize_text_field( $_POST['ctatxt'] );
// Add or Update the meta field in the database.
if ( ! update_post_meta ($post_id, 'ctaurl', $ctaurl) ) {
add_post_meta($post_id, 'ctaurl', $ctaurl, true );
};
if ( ! update_post_meta ($post_id, 'ctatxt', $ctatxt) ) {
add_post_meta($post_id, 'ctatxt', $ctatxt, true );
};
}
add_action( 'save_post', 'jdm_save_meta_box_data' );
/*
CTA Plugin Functions
Reference: http://labs.jdmdigital.co/code/jdm-custom-cta/
*/
// Checks if the ctaurl post meta field is set/and not empty
if(!function_exists('have_cta')) {
function have_cta(){
global $page, $post;
$ctaurl = get_post_meta( $post->ID, 'ctaurl', true ); // the URL
if(isset($ctaurl) && !empty($ctaurl) ) {
return true;
} else {
return false;
}
} // end have_cta()
}
if(!function_exists('has_cta')) {
function has_cta(){
global $page, $post;
$ctaurl = get_post_meta( $post->ID, 'ctaurl', true ); // the URL
if(isset($ctaurl) && !empty($ctaurl) ) {
return true;
} else {
return false;
}
} // end has_cta()
}
// Returns the HTML CTA link with the parameter class(es)
if(!function_exists('get_cta')) {
function get_cta($class = NULL){
global $page, $post;
if( is_null($class) ) {
$class = 'btn-cta';
} else {
$class .= ' btn-cta';
}
$ctaurl = get_post_meta( $post->ID, 'ctaurl', true ); // the URL
$ctatxt = get_post_meta( $post->ID, 'ctatxt', true ); // the text
if(!isset($ctatxt) || empty($ctatxt) ) {
$ctatxt = 'Click Here'; // default
}
if(isset($ctaurl) && !empty($ctaurl) ) {
return '<a href="'.$ctaurl.'" class="'.$class.'">'.$ctatxt.'</a>';
}
} // end get_cta()
}
// Echos get_cta(), basically
if(!function_exists('the_cta') && function_exists('get_cta')) {
function the_cta($class = NULL){
global $page, $post;
//$post = get_post( $id );
if( is_null($class) ) {
$class = 'btn-cta';
} else {
$class .= ' btn-cta';
}
$ctaurl = get_post_meta( $post->ID, 'ctaurl', true ); // the URL
$ctatxt = get_post_meta( $post->ID, 'ctatxt', true ); // the text
if(!isset($ctatxt) || empty($ctatxt) ) {
$ctatxt = 'Click Here'; // default
}
if(isset($ctaurl) && !empty($ctaurl) ) {
echo '<a href="'.$ctaurl.'" class="'.$class.'">'.$ctatxt.'</a>';
}
} // end the_cta()
}
?>