Now that we have a default message available as well, we can use that message in the comment form when one is not specified for a post. In order to do this change the 'edit_cm' function to look like shown below.
public function edit_cm($defaults){
global $wp_query;
$post_id = $wp_query->post->ID;
$custom_message = get_post_meta($post_id,
'ccm_title_reply',
true);
//if a message not specified for a post
if(empty($custom_message)){
//use the defualt message
$custom_message = get_option('ccm_title_reply');
}
//if a message is available show that
if(!empty($custom_message)){
$defaults["title_reply"] = wp_kses_data($custom_message);
}
return $defaults;
}
Now the full source code of the plugin will look like this.
<?php
/*
Plugin Name: Custom Comment Message
*/
if (!class_exists("CustomCommentMessage")) {
class CustomCommentMessage {
public function __construct()
{
add_action( 'add_meta_boxes',
array( &$this, 'add_ccm_meta_box' ) );
add_action('save_post',
array(&$this,'ccm_save_data') ) ;
add_filter('comment_form_defaults',
array(&$this, 'edit_cm') ) ;
add_action('admin_init',
array(&$this, 'ccm_admin_init') );
add_action('admin_menu',
array(&$this, 'add_ccm_admin_page') );
}
function ccm_admin_init(){
register_setting( 'ccm_options',
'ccm_title_reply' );
add_settings_section('ccm_main',
'Custom Comment Message',
array(&$this,'ccm_section_text'),
plugin_basename(__FILE__));
add_settings_field('plugin_text_string',
'Default Comment Message',
array(&$this,'ccm_setting_string'),
plugin_basename(__FILE__),
'ccm_main');
}
function ccm_section_text() {
?>
<p>Set the default comment
message that you would like to use.</p>
<?php
}
function ccm_setting_string() {
$option = get_option('ccm_title_reply');
?>
<input id='plugin_text_string'
name='ccm_title_reply'
size='40'
type='text'
value="<?php wp_kses_data(echo $option); ?>" />
<?php
}
function add_ccm_admin_page(){
add_options_page('Custom Comment Message Otions',
'Custom Comment Message',
'manage_options',
plugin_basename(__FILE__),
array(&$this,'print_ccm_admin_page'));
}
function print_ccm_admin_page(){
?>
<div>
<form method="post"
action="options.php">
<?php settings_fields('ccm_options'); ?>
<?php do_settings_sections(plugin_basename(__FILE__)); ?>
<input type="submit"
value="Save Changes"
name="Submit"/>
</form>
</div>
<?php
}
public function edit_cm($defaults){
global $wp_query;
$post_id = $wp_query->post->ID;
$custom_message = get_post_meta($post_id,
'ccm_title_reply',
true);
if(empty($custom_message)){
$custom_message = get_option('ccm_title_reply');
}
if(!empty($custom_message)){
$defaults["title_reply"] = wp_kses_data($custom_message);
}
return $defaults;
}
public function add_ccm_meta_box()
{
add_meta_box(
'ccm_meta_box'
,'Custom Comment Message'
,array( &$this, 'render_ccm_meta_box_content' )
,'post'
);
}
public function render_ccm_meta_box_content( $post )
{
wp_nonce_field( plugin_basename(__FILE__),
'ccm_noncename');
?>
<label for="ccm__title_reply">
Custom Comment Message Title :
</label>
<input type="text"
id="ccm_title_reply"
name="ccm_title_reply"
value="<?php echo get_post_meta($post->ID,
'ccm_title_reply',
true); ?>"
size="25" />
<?php
}
public function ccm_save_data( $post_id )
{
if ( !wp_verify_nonce( $_POST['ccm_noncename'],
plugin_basename(__FILE__) ) )
return $post_id;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return;
}
else if ( 'post' == $_POST['post_type'] &&
current_user_can( 'edit_page', $post_id ) )
{
$ccm = $_POST['ccm_title_reply'];
add_post_meta($post_id, 'ccm_title_reply', $ccm, true)
or
update_post_meta($post_id, 'ccm_title_reply', $ccm);
}
}
}
}
if (class_exists("CustomCommentMessage")) {
$ccmessage = new CustomCommentMessage();
}
?>
No comments:
Post a Comment