Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Hooking into PHP functions

From: Dreamweaver and WordPress: Building Sites

Video: Hooking into PHP functions

WordPress functions are coded to have what are referred to as hooks, so that they can be easily customized. There are two basic types of hooks: actions and filters, the latter of which we've used repeatedly in our functions.php file so far. Let's take a closer look at the code to create a filter hook first and then add an action hook. As the name suggests, a filter shifts and frequently modifies the data before it's displayed in the browser. I have opened now my functions.php file as you find within the tpa theme folder, and I am going to scroll down.

Hooking into PHP functions

WordPress functions are coded to have what are referred to as hooks, so that they can be easily customized. There are two basic types of hooks: actions and filters, the latter of which we've used repeatedly in our functions.php file so far. Let's take a closer look at the code to create a filter hook first and then add an action hook. As the name suggests, a filter shifts and frequently modifies the data before it's displayed in the browser. I have opened now my functions.php file as you find within the tpa theme folder, and I am going to scroll down.

Here you see two filter hooks. A filter hook has two parts: the function and the add_filter statement. The filter function is often used to define new settings for a built-in function. Let's take a look at the new_excerpt_ length function that we see starting on line 27. This is a pretty basic function that changes the length of the excerpt. By default it's set at 55. With the function new_excerpt_ length, we are changing that value to 40.

Right after we define the new function, we call the add_filter to hook it into place. add_filter takes two arguments: the first argument names the WordPress function, while the second specifies the custom function that's going to filter it. The same basic concept can be applied to our second custom function new_excerpt_more, which changes the output at the end of an excerpt from being a bracketed unlinked ellipsis to, in this case, a linked phrase read more. Again, the add_filter function on line 36 names the built-in WordPress function excerpt more and then the new function that's going to filter it, new excerpt more.

An action hook is different from a filter hook in that the function is triggered in response to a particular event, like a post being published for example, as opposed to changing how data is presented, which is what filter hooks do. We don't currently have any action hooks in our functions.php file, so let's add one. If you've ever looked at the code for a homepage generated by WordPress, you'll notice that WordPress by default injects a good number of meta-type code in the head section.

Not all of them, in my estimation, totally necessary. Let me show you what I mean. Let's go over to our index.php from the site root. We are in Live view and I want to enter into live code now. Then here's a perfect example right up top, a wlwmanifest-type link, and then right after it is the meta tag with the generator. Well, I think I want to get rid of both of those. So let's create an action hook that will remove them.

Go back to our functions.php page, and I'll go right after the last add_filter, staying within the PHP block, Tab in, and start my new function with the keyword function, and then give it a name remove_header_info, put our opening and closing curly braces, and then in the middle of it I'm going to use another WordPress function called remove_action.

This takes two arguments. The first argument is the name of the WordPress function that inserts the code. In this case, it's wp_head, which we put in single quotes. Then after the closing single quote put a comma to enter your second argument. This is the code that you want to remove. All of the names like this are listed for you in the WordPress Codex. So I'm typing in wlwmanifest_link. And let's close our parentheses and put a semicolon.

We are going to repeat that same line. Very similar. So remove_action(wp_head), followed by the code we want to remove, which is wp_generator. Close off our parentheses and semicolon. And I would just like filter hooks have an add_filter to activate them, we have an add action function that we need to bring in. Within its parentheses, again, there are two arguments. The first is what is the trigger for this action? And the trigger for this is a function called init, and it's encased in single quotes, followed by a comma. And the second argument is the name of the function that should be added.

Of course, it's going to be our new custom function name remove_header_info. All right, we will close our parentheses and semicolon and save our page. Now let's head over to index.php. Here are the two tags that we are hoping to get rid of. Let's refresh the page and the tags are now gone. How do you know which actions or functions can act as triggers? The WordPress Codex has a great list called the Action Reference and once you go to that page, you can scroll down and you'll see just tons of available actions that you can use.

The more you investigate them, the more you'll find that filter and action hooks can take you a long way towards creating just the site you and your client are looking for.

Show transcript

This video is part of

Image for Dreamweaver and WordPress: Building Sites
Dreamweaver and WordPress: Building Sites

21 video lessons · 16781 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Dreamweaver and WordPress: Building Sites.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.