As you can see, the default excerpt length is still 150, but we’ve also applied
some filters to it. A filter allows you to write a function that modifies the
value of something — in this case, the excerpt’s length. The name (or tag) of
this filter is excerpt_length, and if no functions are attached to it, then its
value will remain 150. Let’s see how we can now use this to modify the
default value.
First, we have defined a function that does nothing but return a number. At
this point, nothing is using the function, so let’s tell WordPress that we want
to hook this into the excerpt_length filter.
We’ve successfully changed the default excerpt length in WordPress,
without touching the original function and without even having to write a
custom excerpt function. This will be extremely useful, because if you
always want excerpts that are 200 characters long, just add this as a filter
and then you won’t have to specify it every time.
Suppose you want to tack on some more text, like “Read on,” to the end of
the excerpt. We could modify our original function to work with a hook and
then tie a function to that hook, like so:
This hook is placed at the end of the function and allows us to modify its
end result. This time, we’ve also passed the output that the function would
normally produce as a parameter to our hook. The function that we tie to
this hook will receive this parameter.
All we are doing in our function is taking the original contents of $excerpt
and appending our “Read on” text to the end. But if we choose, we could
also return the text “Click the title to read this article,” which would replace
the whole excerpt.
While our example is a bit redundant, since WordPress already has a better
function, hopefully you’ve gotten to grips with the thinking behind hooks.
Let’s look more in depth at what goes on with filters, actions, priorities,
arguments and the other yummy options available.
Filters And Actions
Filters and actions are two types of hooks. As you saw in the previous
section, a filter modifies the value of something. An action, rather than
modifying something, calls another function to run beside it.
A commonly used action hook is wp_head. Let’s see how this works. You
may have noticed a function at the bottom of your website’s head section
named wp_head(). Diving into the code of this function, you can see that it
contains a call to do_action(). This is similar to apply_filters(); it
means to run all of the functions that are tied to the wp_head tag.
Let’s put a copyright meta tag on top of each post’s page to test how this
works.
!e Workflow Of Using Hooks
While hooks are better documented nowadays, they have been neglected a
bit until recently, understandably so. You can find some good pointers in the
Codex, but the best thing to use is Adam Brown’s hook reference, and/or
look at the source code.
Say you want to add functionality to your blog that notifies authors when
their work is published. To do this, you would need to do something when a
post is published. So, let’s try to find a hook related to publishing.
Can we tell whether we need an action or a filter? Sure we can! When a post
is published, do we want to modify its data or do a completely separate
action? The answer is the latter, so we’ll need an action. Let’s go to the
action reference on Adam Brown’s website, and search for “Publish.”
The first thing you’ll find is app_publish_post. Sounds good; let’s click
on it. The details page doesn’t give us a lot of info (sometimes it does), so
click on the “View hook in source” link next to your version of WordPress
(preferably the most recent version) in the table. This website shows only a
snippet of the file, and unfortunately the beginning of the documentation is
cut off, so it’s difficult to tell if this is what we need. Click on “View complete
file in SVN” to go to the complete file so that we can search for our hook.
In the file I am viewing, the hook can be found in the
_publish_post_hook() function, which — according to the
documentation above it — is a “hook to schedule pings and enclosures when
a post is published,” so this is not really what we need.
With some more research in the action list, you’ll find the publish_post
hook, and this is what we need. The first thing to do is write the function that
sends your email. This function will receive the post’s ID as an argument, so
you can use that to pull some information into the email. The second task is
to hook this function into the action. Look at the finished code below for the
details.
Smashing
ที่คุณสามารถดู , เริ่มต้นตัดตอนความยาวยังเป็น 150 เลย แต่เราก็ยังใช้
ตัวกรองบางมัน ตัวกรองช่วยให้คุณสามารถเขียนฟังก์ชันที่ปรับเปลี่ยน
ค่าบางอย่าง ในกรณีนี้ความยาวของข้อความที่ตัดตอนมา . ชื่อ ( หรือแท็ก )
excerpt_length กรองนี้ และถ้าไม่มีฟังก์ชั่นที่แนบมากับมัน แล้วค่าของ
จะยังคง 150 เรามาดูวิธีที่เราสามารถใช้เพื่อปรับเปลี่ยน
ค่าโดยปริยาย
ก่อนอื่นเราต้องกำหนดฟังก์ชันที่ไม่ได้ทำอะไรแต่กลับหมายเลข ที่
จุดนี้ ไม่มีอะไรใช้ ฟังก์ชัน ดังนั้นบอกคลิป ที่เราต้องการ
ตะขอนี้เข้าไปในกรอง excerpt_length
เราสามารถเปลี่ยนค่าเริ่มต้นข้อความที่ตัดตอนมาความยาวใน WordPress
โดยไม่ต้องสัมผัสฟังก์ชั่นเดิมโดยไม่ต้องเขียน
เองตัดตอนฟังก์ชันนี้จะเป็นประโยชน์อย่างมาก เพราะถ้าคุณ
ต้องการตัดตอนที่ 200 ตัวอักษรยาว เพียงแค่เพิ่มนี้เป็นตัวกรอง
แล้วคุณจะไม่ต้องระบุทุกครั้ง
สมมติว่าคุณต้องการเพิ่มบางข้อความ เช่น " อ่านต่อ " ที่ส่วนท้ายของข้อความที่ตัดตอนมา
. เราสามารถปรับเปลี่ยนฟังก์ชันเดิมของเราที่จะทำงานกับตะขอและ
แล้วผูกฟังก์ชันเบ็ดที่เช่นดังนั้น :
ตะขอนี้วางไว้ในตอนท้ายของหน้าที่ และช่วยให้เราสามารถปรับเปลี่ยนของ
จบผล ในครั้งนี้ เรายังผ่านออกที่ฟังก์ชันจะ
โดยปกติผลิตเป็นพารามิเตอร์เบ็ดของเรา ฟังก์ชันที่เราผูกกับเบ็ดนี้จะรับค่า
ทั้งหมดนี้ เรากำลังทำในการทำงานของเราคือ เอาเนื้อหาเดิมของ $ ตัดตอน
และท้ายของเรา " อ่าน " ข้อความสุดท้ายแต่ถ้าเราเลือก เราสามารถ
กลับไปยังข้อความ " คลิกที่ชื่อเรื่องเพื่ออ่านบทความนี้ " ซึ่งจะแทนที่ข้อความที่ตัดตอนมาทั้งหมด
ในขณะที่ตัวอย่างของเราเป็นบิตมากเกินไป เนื่องจาก WordPress มีฟังก์ชันดีกว่า
, หวังว่าคุณจะเข้าใจความคิดเบื้องหลัง hooks .
กันเถอะ ดูเพิ่มเติมในเชิงลึกที่เกี่ยวข้องกับตัวกรอง , ลำดับการกระทำ , ,
ข้อโต้แย้งและอร่อยอื่น ๆตัวเลือกพร้อมใช้งาน ตัวกรองและการกระทำ
ตัวกรองและการกระทำเป็นสองประเภทของตะขอ ตามที่คุณเห็นในส่วนก่อนหน้า
, ตัวกรองปรับค่าบางอย่าง การกระทำ มากกว่า
ปรับเปลี่ยนบางอย่าง เรียกฟังก์ชันอื่นที่วิ่งอยู่ข้างๆ ที่ใช้กันทั่วไปคือการตะขอ
wp_head . ลองมาดูวิธีการทำงานนี้ คุณ
อาจจะสังเกตเห็นการทำงานที่ด้านล่างของส่วนหัวของเว็บไซต์
ชื่อ wp_head() . ดําน้ําในรหัสของฟังก์ชันนี้ คุณสามารถดูว่ามีการเรียก do_action()
. นี้จะคล้ายกับ apply_filters() ;
หมายถึงการใช้ทั้งหมดของฟังก์ชันที่เกี่ยวข้องกับ wp_head แท็ก .
เอาลิขสิทธิ์แท็ก meta ที่ด้านบนของโพสต์แต่ละหน้า เพื่อทดสอบว่าทำงานนี้
.
! !และเวิร์กโฟลว์ใช้ตะขอ
ในขณะที่ตะขอดีกว่าเอกสารในปัจจุบัน พวกเขาได้ถูกทอดทิ้งเป็น
บิตจนกระทั่งเมื่อเร็ว ๆ นี้มากๆเลย คุณสามารถหาบางอย่างที่ดี ชี้ใน
Codex แต่สิ่งดีที่สุดที่ใช้คืออดัมสีน้ำตาลตะขออ้างอิง และ / หรือ
ดูซอร์สโค้ด สมมติว่าคุณต้องการที่จะเพิ่มฟังก์ชันให้บล็อกของคุณที่แจ้งผู้เขียนเมื่อ
ผลงานที่ได้รับการตีพิมพ์ ที่ต้องทำแบบนี้คุณจะต้องทำอะไรเมื่อ
โพสต์มีการเผยแพร่ ดังนั้น เรามาลองหาตะขอที่เกี่ยวข้องกับการพิมพ์ เราสามารถบอกได้ว่า
เราต้องมีการกระทำ หรือกรอง ? เราทำได้ ! เมื่อโพสต์
เผยแพร่ เราต้องการแก้ไขข้อมูลหรือการกระทำต่างหาก
อย่างสมบูรณ์ ? คำตอบคือ หลัง ดังนั้นเราต้องมีการกระทำ ไป
ปฏิบัติการอ้างอิงในเว็บไซต์ของอดัมสีน้ำตาลและค้นหา " เผยแพร่ "
สิ่งแรกที่คุณจะพบคือ app_publish_post . ฟังดูดี ลองคลิก
บน รายละเอียดหน้าไม่ได้ให้ข้อมูลจํานวนมาก ( บางครั้งก็ไม่ ) ,
คลิก " ดูตะขอในแหล่ง " ลิงค์ถัดไปของ WordPress
รุ่นของคุณ ( โดยเฉพาะรุ่นล่าสุด ) โต๊ะ เว็บไซต์นี้จะแสดงเพียง
ตัวอย่างของแฟ้มและน่าเสียดายที่จุดเริ่มต้นของเอกสาร
ตัด จึงยากที่จะบอกได้ว่า นี่เป็นสิ่งที่เราต้องการ คลิกที่ " ดูสมบูรณ์
ไฟล์ใน SVN " เพื่อไปที่ไฟล์เสร็จสมบูรณ์ เพื่อให้เราสามารถค้นหาเบ็ดของเรา .
ในไฟล์ผมดูตะขอสามารถพบได้ในฟังก์ชัน _publish_post_hook()
-
ซึ่งตามเอกสารข้างต้นมันเป็นเบ็ด " ตารางส่ง Ping และเปลือกเมื่อ
โพสต์เผยแพร่ " นี่ไม่ใช่สิ่งที่เราต้องการ
กับงานวิจัยเพิ่มเติมในการกระทำ คุณจะหาตะขอ publish_post
และนี้คือสิ่งที่เราต้องการ สิ่งแรกที่ต้องทำคือเขียนฟังก์ชัน
ส่งอีเมล์ของคุณ ฟังก์ชันนี้จะได้รับการโพสต์ของ ID เป็นอาร์กิวเมนต์ ดังนั้น
คุณสามารถใช้มันเพื่อดึงข้อมูลในอีเมล์ งานที่สองคือ
ตะขอฟังก์ชั่นนี้ไปสู่การปฏิบัติ ดูเสร็จรหัสด้านล่าง
ดีรายละเอียด
การแปล กรุณารอสักครู่..
