2012-10-04 88 views
1

我試圖編輯錯誤消息,當您點擊添加註釋按鈕(來自管理面板)未鍵入評論。如何覆蓋WordPress的核心功能

這裏的wp_ajax_replyto_comment函數(從可溼性粉劑管理員/包含/ AJAX-actions.php)處理此錯誤消息。

function wp_ajax_replyto_comment($action) { 
    global $wp_list_table, $wpdb; 
    if (empty($action)) 
     $action = 'replyto-comment'; 

    check_ajax_referer($action, '_ajax_nonce-replyto-comment'); 

    set_current_screen('edit-comments'); 

    $comment_post_ID = (int) $_POST['comment_post_ID']; 
    if (!current_user_can('edit_post', $comment_post_ID)) 
     wp_die(-1); 

    $status = $wpdb->get_var($wpdb->prepare("SELECT post_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID)); 

    if (empty($status)) 
     wp_die(1); 
    elseif (in_array($status, array('draft', 'pending', 'trash'))) 
     wp_die(__('ERROR: you are replying to a comment on a draft post.')); 

    $user = wp_get_current_user(); 
    if ($user->exists()) { 
     $user_ID = $user->ID; 
     $comment_author  = $wpdb->escape($user->display_name); 
     $comment_author_email = $wpdb->escape($user->user_email); 
     $comment_author_url = $wpdb->escape($user->user_url); 
     $comment_content  = trim($_POST['content']); 
     if (current_user_can('unfiltered_html')) { 
      if (wp_create_nonce('unfiltered-html-comment') != $_POST['_wp_unfiltered_html_comment']) { 
       kses_remove_filters(); // start with a clean slate 
       kses_init_filters(); // set up the filters 
      } 
     } 
    } else { 
     wp_die(__('Sorry, you must be logged in to reply to a comment.')); 
    } 

    if ('' == $comment_content) 
     wp_die(__('ERROR: please type a comment.')); 

    $comment_parent = absint($_POST['comment_ID']); 
    $comment_auto_approved = false; 
    $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID'); 

    $comment_id = wp_new_comment($commentdata); 
    $comment = get_comment($comment_id); 
    if (! $comment) wp_die(1); 

    $position = (isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1'; 

    // automatically approve parent comment 
    if (!empty($_POST['approve_parent'])) { 
     $parent = get_comment($comment_parent); 

     if ($parent && $parent->comment_approved === '0' && $parent->comment_post_ID == $comment_post_ID) { 
      if (wp_set_comment_status($parent->comment_ID, 'approve')) 
       $comment_auto_approved = true; 
     } 
    } 

    ob_start(); 
     if ('dashboard' == $_REQUEST['mode']) { 
      require_once(ABSPATH . 'wp-admin/includes/dashboard.php'); 
      _wp_dashboard_recent_comments_row($comment); 
     } else { 
      if ('single' == $_REQUEST['mode']) { 
       $wp_list_table = _get_list_table('WP_Post_Comments_List_Table'); 
      } else { 
       $wp_list_table = _get_list_table('WP_Comments_List_Table'); 
      } 
      $wp_list_table->single_row($comment); 
     } 
     $comment_list_item = ob_get_contents(); 
    ob_end_clean(); 

    $response = array(
     'what' => 'comment', 
     'id' => $comment->comment_ID, 
     'data' => $comment_list_item, 
     'position' => $position 
    ); 

    if ($comment_auto_approved) 
     $response['supplemental'] = array('parent_approved' => $parent->comment_ID); 

    $x = new WP_Ajax_Response(); 
    $x->add($response); 
    $x->send(); 
} 

我想只覆蓋這一部分:

if ('' == $comment_content) 
     wp_die(__('ERROR: please type a comment.')); 

這裏是我的代碼,這是擺在我的主題的functions.php

function my_function_name() { 
    if ('' == $comment_content) 
     wp_die(__('My custom message here')); 
} 
add_filter('wp_ajax_replyto_comment', 'my_function_name'); 

不幸的是,我的代碼不能正常工作。請幫幫我!謝謝!

回答

7

有某些功能是「可插入的」,您可以在其中覆蓋它們,但那不是其中之一。

調整核心功能的唯一方法是使用動作掛鉤和過濾器,除非您想直接進入並編輯核心(壞習慣......升級核心將會覆蓋您的更改)。

如果在覈心中找到do_action('something');,則可以使用add_action('something','my_function');來掛鉤。

如果在覈心中發現apply_filters('something', $value),則可以使用add_filter('something', 'my_function');過濾$ value。

順便說一句,在上面的例子中,你會怎麼做:

function my_function($value){ 
    // manipulate $value 
    return $value; 
} 

確保返回的東西作爲濾波值。不幸的是,我似乎無法找到任何地方掛接到這個:(。

更多信息,請參見http://codex.wordpress.org/Plugin_API

如果您必須更改文本,你可以在頁面加載的JavaScript後做。

+0

+1對於* pluggable *函數的解釋,非常有用!謝謝! – vurquee