วิธีเพิ่มตัวกรองที่มี 2 ส่วน?


9

ฉันต้องการแก้ไข $ path ในตัวกรองต่อไปนี้ มันมี 1 อินพุตและ 2 args

function documents_template( $template = '' ) {

       $path = DOCUMENTS_INCLUDES_DIR . '/document/' . $template;

  return apply_filters( 'document_template', $path, $template );
}

นี่คือฟังก์ชั่นของฉันเพื่อเพิ่มตัวกรองมันได้รับข้อความแสดงข้อผิดพลาดวิธีทำให้ถูกต้อง

function my_template( $template = '' ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'document_template','my_template', 10, 2 );

ฉันพยายามที่จะเปลี่ยนค่าตอบแทนของฉันดังต่อไปนี้มันไม่ทำงาน:

return apply_filters( 'my_template', $path, $template);

ด้วยคำตอบด้านล่างตัวกรองใหม่ของฉันยังไม่ทำงานดังนั้นอาจเป็นเพราะตัวกรองของฉันอยู่ในชั้นเรียนหรือไม่ นี่คือรหัสใหม่ที่สมบูรณ์:

Class My_Class{
  function __construct() {
     add_filter( 'document_template', array( $this, 'my_template',10, 2 ) );
  }
 function my_template( $path, $template ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
 }
}

คำตอบ:


10
function my_locate_template( $path, $template ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'documents_template','my_locate_template', 10, 2 );

add_filter รับ 4 ตัวแปร ครั้งแรกและครั้งที่สองจะต้อง 1. ชื่อของตัวกรอง 2. ชื่อของฟังก์ชัน ที่สามคือลำดับความสำคัญ (เมื่อฟังก์ชันถูกไล่ออก) และข้อที่สี่คือปริมาณของพารามิเตอร์ หากคุณกำหนดจำนวนของอาร์กิวเมนต์คุณต้องใส่ไว้ในฟังก์ชันของคุณด้วย ตัวอย่างเช่น,

add_filter( 'the_filter','your_function', 10, 1 );
function your_function($var1) {
   // Do something
}

ถ้าตัวกรองรองรับอาร์กิวเมนต์เพิ่มเติม (ในกรณีนี้ 3)

   add_filter( 'the_filter','your_function', 10, 3 );
    function your_function($var1, $var2, $var3) {
       // Do somthing
    }

อ่าน codex ทั้งหมดสำหรับข้อมูลเกี่ยวกับadd_filter ()


function documents_template( $template = '' ) {

       $path = DOCUMENTS_INCLUDES_DIR . '/document/' . $template;

  return apply_filters( 'document_template', $path, $template );
}

function my_template( $path, $template ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'document_template','my_template', 10, 2 );

รหัสนี้ใช้ได้สำหรับฉัน คุณลองนี่ไหม


ในการเปลี่ยนชั้นเรียนของคุณ:

add_filter( 'document_template', array( $this, 'my_template',10, 2 ) );

ถึง:

add_filter( 'document_template', array( $this, 'my_template'), 10, 2  );

เพิ่มอินพุตเพิ่มเติมในฟังก์ชั่น my_template ไม่ทำงานเช่นกัน
Jenny

คุณได้รับข้อผิดพลาดหรือไม่?
Rob Vermeer

ใช่ข้อผิดพลาดทั้งหมดจะเหมือนกัน: call_user_func_array คาดว่าพารามิเตอร์ 1 จะเป็นการโทรกลับที่ถูกต้องอาร์เรย์ต้องมีสมาชิกสองคนแน่นอน
Jenny

ฉันแก้ไขคำตอบของฉัน หากคุณลบรหัสข้อผิดพลาดหายไปใช่ไหม
Rob Vermeer

ใช่ฉันแทนที่รหัสด้วยสิ่งนี้ แต่ก็ยังไม่ทำงาน
Jenny

2

สิ่งเหล่านี้ต้องตรงกัน แต่ไม่:

apply_filters( 'document_template', $path, $template );

และ

add_filter( 'documents_template','my_template', 10, 2 );

document_template ! = documents_template

มิฉะนั้นทุกอย่างดูถูกต้อง

แก้ไข

เดี๋ยวก่อนทุกอย่างดูไม่ถูกต้อง ฉันไม่คิดว่าคุณต้องการเพิ่มพารามิเตอร์ในนิยามฟังก์ชันการเรียกกลับของคุณ แต่คุณต้องกำหนด$templateภายในการเรียกกลับหรือเพียงแค่ส่งกลับโดยไม่แก้ไข ดังนั้นแทนที่สิ่งนี้:

function my_template( $template = '' ){

...ด้วยสิ่งนี้:

function my_template(){

เช่น:

function my_template(){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'documents_template','my_template', 10, 2 );

แก้ไข 2

โอเคผิดเล็กน้อยในส่วนของฉัน ลองนี่เป็นการโทรกลับของคุณ:

function my_template( $path, $template ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'documents_template','my_template', 10, 2 );

ขออภัยในความผิดพลาดของ 'document' = 'เอกสาร' ขณะนี้มีฟังก์ชั่นใหม่ของคุณมันยังไม่ทำงาน ข้อผิดพลาดกล่าวว่าคาดว่า call_user_func_array 1 พารามิเตอร์ที่จะโทรกลับที่ถูกต้องอาร์เรย์ต้องมีสองสมาชิก
เจนนี่

ความผิดพลาดของฉัน; คุณไม่จำเป็นต้องเพิ่มพารามิเตอร์นิยามฟังก์ชันการเรียกกลับ ดูคำตอบที่อัปเดต
Chip Bennett

รหัสใหม่ไม่ทำงานเช่นกัน ฉันลอง print_r จะเห็นว่าเส้นทางของฉันถูกต้อง
Jenny
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.