What I have learned about WordPress in my experience is that it is very flexible and pluggable through the use of hooks, actions and filters.
Actions are actually hooks which are executed at specific points in code execution or on some event trigger. Filters are used to adjust a content before saving to the database or before rendering to browser.
What I wanted to do is to add a custom link to page listings, so I needed a filter to do that. As I needed to add those links only to pages and not posts or any other post type, I used a filter page_row_actions.
Basic usage of filters in WordPress is in a few steps:
- Create a PHP function to filter the content (in our case, add links)
- Hook that PHP function to a WordPress filter using add_filter method
So let’s create our PHP filter function, so add this code to your theme’s functions.php file (or the plugin if you’re making one):
[code lang=”php”]
function add_facebook_link($actions, $page_object)
{
$actions[‘facebook_link’] = ‘<a href="https://facebook.com" class="facebook_link">’ . __(‘Go to Facebook’) . ‘</a>’;
return $actions;
}
[/code]
This function is receiving 2 parameters, actions, which is all the links on the page listings, and page_object which is the actual post object for the page. After that, it adds new link to the actions array and returns the new array for rendering. Simple.
Now, we can add the filter by adding this to your theme’s functions.php file:
[code lang=”php”]
add_filter(‘page_row_actions’, ‘add_facebook_link’, 10, 2);
[/code]
It’s quite easy, first parameter is the filter we want to use, second is the name of the function, third is priority (useful to control priorities of filter functions if we have multiple functions on the same filter) and fourth is the number of parameters our filter function has.
If you add that and refresh your admin All pages screen, you should see Go To Facebook link as the last link below every page.
Good job.
Remove a link
What if we want to remove a link below pages. Easy, we just unset that from our actions array:
[code lang=”php”]
function remove_trash_link($actions, $page_object)
{
unset $actions[‘trash’];
return $actions;
}
add_filter(‘page_row_actions’, ‘remove_trash_link’, 10, 2);
[/code]
To get a feeling of what to do with our page_object parameter, next example will add a link to search Google for current page title, useless, but just an example:
[code lang=”php”]
function search_google($actions, $page_object)
{
$actions[‘google_link’] = ‘<a href="http://google.com/search?q=’ . $page_object->post_title . ‘" class="google_link">’ . __(‘Search Google for Page Title’) . ‘</a>’;
return $actions;
}
add_filter(‘page_row_actions’, ‘search_google’, 10, 2);
[/code]
There are other useful filters available:
post_row_actions
media_row_actions
user_row_actions
I hope that this little tip will help you do awesome stuff with WordPress.
Did you ever use those filters? Share your experience in comments below.
Hi:
Thank you for sharing such a helpful post. I have a question though: how can I create a custom post editing page in admin while using the post_row_actions filter.
The idea comes from the add_menu_page function by using which you can render a new admin page and add a link to the new page in the left admin menu bar. What I want to do is creating such a custom editing page for each post but instead of adding a link to it in the left admin menu, add the link to under each post in the listing using the post_row_actions filter.
Is there a way to do that? thanks.
Comments are closed.