New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

Dreamweaver and WordPress: Building Sites
Illustration by

Hooking into PHP functions


From:

Dreamweaver and WordPress: Building Sites

with Joseph Lowery

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.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Dreamweaver and WordPress: Building Sites
1h 42m Intermediate Feb 24, 2012

Viewers: in countries Watching now:

In this course, author Joseph Lowery shows how to create a robust WordPress-based site using Dreamweaver. The course lays out the essentials of planning a site, explains how to implement custom sidebars, and demonstrates building page templates. The course also explains how to integrate PHP functionality and extend the WordPress database.

Topics include:
  • Understanding development environment requirements
  • Working with existing WordPress content
  • Handling standard pages
  • Managing full posts
  • Coding a basic template
  • Adding a custom sidebar
  • Working with WordPress functions
  • Inserting PHP code in posts
  • Including a recordset
  • Outputting data
  • Linking to dynamic pages
Subjects:
Web CMS
Software:
Dreamweaver WordPress
Author:
Joseph Lowery

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.

There are currently no FAQs about Dreamweaver and WordPress: Building Sites.

 
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.
Upgrade now


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 Upgrade now

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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

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.