1Remove hook get_post_excerpt được add trong class

https://topvietnamlist.com

Bạn đang muốn remove hook get_post_excerpt được add trong class Kadence_Blocks_Pro_Postgrid_Block (plugin kadence-blocks-pro). Vì hook này được add trong __construct(), nên bạn không thể remove trực tiếp bằng string — phải dùng đúng instance của class.

wp-content\themes\kadence-child\functions.php

/**
 * Get Post Loop Excerpt
 *
 * @param array $attributes Block Attributes.
 */
function get_post_excerpt_custom( $attributes ) {
  if ( isset( $attributes['displayExcerpt'] ) && true === $attributes['displayExcerpt'] ) {
    $excerpt = get_the_excerpt();
    if ( isset( $attributes['excerptCustomLength'] ) && true === $attributes['excerptCustomLength'] ) {
      // If excerpt_length is less than our custom length, change it for our call, then revert
      $excerpt_length = apply_filters( 'excerpt_length', 55 );
      if ( $excerpt_length < $attributes['excerptLength'] ) {
        add_filter(
          'excerpt_length',
          function ( $length ) use ( $attributes ) {//phpcs:ignore
            return $attributes['excerptLength'];
          },
          999
        );
        $excerpt = get_the_excerpt();
        add_filter(
          'excerpt_length',
          function ( $length ) use ( $excerpt_length ) {//phpcs:ignore
            return $excerpt_length;
          },
          999
        );
      }
      $words = explode( ' ', $excerpt );
      if ( count( $words ) > $attributes['excerptLength'] ) {
        $excerpt = rtrim( implode( ' ', array_slice( $words, 0, $attributes['excerptLength'] ) ), '.' ) . __( '<a href='.get_the_permalink().' class="span-read-more"> ...read more</a>', 'kadence-blocks-pro' );
      }
    }
    echo $excerpt;
  }
}
add_action('init', function() {
	global $wp_filter;
	if ( isset($wp_filter['kadence_blocks_post_loop_content']) ) {
		foreach ($wp_filter['kadence_blocks_post_loop_content']->callbacks as $priority => $callbacks) {
			foreach ($callbacks as $key => $callback) {
				if ( is_array($callback['function']) ) {
					$object = $callback['function'][0];
					$method = $callback['function'][1];
					if ( is_object($object)  && get_class($object) === 'Kadence_Blocks_Pro_Postgrid_Block' && $method === 'get_post_excerpt') {
						remove_action('kadence_blocks_post_loop_content', array($object, 'get_post_excerpt'),$priority);
					}
				}
			}
		}
	}
}, 99);
add_action( 'kadence_blocks_post_loop_content', 'get_post_excerpt_custom', 21 );

✅ Cách chuẩn để remove action

✔️ Cách 1: Remove sau khi plugin load (khuyên dùng)


💡 Giải thích

  • add_action dùng $this → WordPress lưu callback dạng:

  • Bạn không có reference tới object đó, nên phải:

    • Duyệt $wp_filter

    • Tìm đúng instance

    • Remove bằng chính object đó


🚀 Cách 2 (clean hơn nếu biết hook khởi tạo class)

Nếu plugin có global instance (ví dụ $kadence_blocks_postgrid_block) thì có thể:

👉 Nhưng Kadence thường không expose global, nên cách 1 vẫn chắc chắn nhất.


⚠️ Lưu ý quan trọng

  • Hook phải chạy sau khi plugin đã add action → dùng priority cao (99 hoặc init)

  • Nếu không remove được, thử:

    hoặc


🎯 Bonus: Override luôn function (nếu cần custom)

Nếu bạn muốn thay nội dung excerpt:


Nếu bạn muốn mình tối ưu hơn nữa

Mình có thể:

  • Trace chính xác nơi class được init trong plugin

  • Viết hook remove ngắn gọn 1 dòng

  • Hoặc override đúng chuẩn Kadence (clean hơn, không hack $wp_filter)

👉 Chỉ cần gửi mình đoạn file init của plugin hoặc class load là mình làm cho bạn bản tối ưu nhất.

Last updated