The Difference between do_action, add_action and add_filter
The Foundation: do_action
do_action creates the hook for things to hang. This is at the core of WordPress and even frameworks like Genesis and many other themes and plugins. do_action is the first domino in the chain of events with hooks. However, alone, it means nothing and does nothing. Simply, it tells WordPress to search to see if any functions are attached to it to fire. So do_actions will look something like this:
[php]<?php do_action( 'my-home' ); [/php]
It can also take and pass variables:
[php]<?php do_action( 'my-home' , $var1 , $var2 ); [/php]
The Difference between add_action and add_filter
The difference is primarily and technically semantic. Technically speaking, you can use them interchangeably, but it wouldn't follow code common sense or code "mentality" as one writer said. Filters should filter information, thus receiving information/data, applying the filter and returning information/data, and then used. However, filters are still action hooks.
WordPress defines add_filter as "Hooks a function to a specific filter action," and add_action as "Hooks a function on to a specific action."
add_action
Now, add_actions are a bit different. They hang items on the do_action hook and $priority determines the order.
Say, for example, you have this in your functions.php file:
[php]<?php
add_action( 'hook' , 'bob' );
add_action( 'hook' , 'andy' );
[/php]
The general order is "chronological" meaning if action 'bob' appears (coded/read by the server) before action 'andy', then the action hook 'bob' will fire first. However, with $priority, this order can be interrupted and changed. So...
[php]<?php
add_action( 'hook' , 'bob' , 10 );
add_action( 'hook' , 'andy' , 5 );
[/php]
With add_actions, variables and information may be passed back and forth as needed in the various functions as need. It should also be noted that not all actions are void of arguments or parameters. Some actions do have parameters, so:
[php]<?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?>[/php]
So, it would look something like this:
[php]<?php add_action( 'hook_name' , 'my_function_name' , 10 , 2 ); ?>[/php]
Extended Version Example
[php]<?php
function echo_comment_id( $comment_ID )
{
echo "I just received $comment_ID";
}
add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );[/php]
add_filter
Now WordPress (and my theme) passes much of this page through various filters that check, validate, correct, and even modify various parts. Once it passes through the filter, the information then is applied to an action.
So here is the add_filter function:
[php]<?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?>[/php]
This generally looks something like this:
[php]<?php
add_filter( 'filter_name' , 'my_filter_function_name' , 10 , 3 );
function filter_name( $val, $attr, $content = null ) {
//do something
}
?>[/php]
Just like add_actions go with do_actions, the same is true for filters logically speaking. With add_filter you must have apply_filters. Without the filter being called or applied then the filter means nothing, logically.
So, logicaly, in our example with filter 'filter_name', this would modify content/information that is coded like this:
[php]<?php
// Allow plugins/themes to override the default caption template.
$output = apply_filters( 'filter_name', $output , $val, $attr , $content );
if ( $output != '' )
return $output;
[/php]
So, the code is basically saying, āTake the value of the `$output` variable, apply any filters attached to the āfilter_nameā hook passing the variables $val, $attr, $content to the filter function (whatever that may be and if it accepts them), and assign the filtered value back to the `$output` variableā.
However, technically speaking, PHP is rather forgiving and instead of filtering anything, it can function like add_action, adding the filter to a do_action, not filtering anything, which adds to the confusion. So if you have the following, it will work (though not good form):
[php]</php
do_action( 'my_action' );
add_filter( 'my_action' , 'my_function');
function my_function() {
//do something
}[/php]
Since the add_filter did not have any filters being applied, it worked as an add_action.
How to Get WordPress's Custom Menu Description
So recently, someone wanted to use menu descriptions as a tagline of sorts for a variety of pages, categories, archives, etc. instead of using basic metaboxes on pages (since this would also apply to category pages, etc.). So I wrote up a simple function that I believe may be helpful for others. So here is the code. To use, simply copy the function to your functions.php file and then use the function wherever you'd like.
[php]<?php
function wps_get_menu_description( $args = array() ) {
global $post;
// Default
$defaults = array(
'echo' => false,
'format' => '',
'description' => '',
'location' => 'primary',
'classes' => 'post-description'
);
$args = wp_parse_args( $args, $defaults );
extract( $args , EXTR_SKIP );
// Get menu
$menu_locations = get_nav_menu_locations();
$nav_items = wp_get_nav_menu_items( $menu_locations[ $location ] );
// Cycle through nav items
foreach ( $nav_items as $nav_item ) {
if ( ( is_page() || is_single() || is_archive() ) && ( $nav_item->object_id == $post->ID ) ) {
$description = $nav_item->description;
}
elseif ( ( is_category() ) && ( $nav_item->object == 'category' ) ) {
$cat = get_query_var( 'cat' );
if ( $nav_item->object_id == $cat )
$description = $nav_item->description;
}
}
// Get output formatting
if ( $format == 'html' )
$output = apply_filters( 'wps_get_menu_description_output' , '<div class="'. $classes .'">' . $description . '</div>' , $args );
else
$output = $description;
// Echo description
if ($echo)
echo $output;
// Return description
return $output;
}
[/php]
To call the function, you just set the args in an array like this:
wps_get_menu_description( array('echo' => true, 'format'=>'html', 'classes' => 'post-info') );
With most frameworks, like Genesis/Thesis, you may need to call it like this (example reflects Genesis):
[php]<?php
// Add and Customize a Tagline under the Page Title
add_action('genesis_after_post_title', 'my_tagline');
function my_tagline() {
wps_get_menu_description( array('echo' => true, 'format'=>'html', 'classes' => 'post-info') );
}[/php]
Word of Warning: Please check your descriptions, and do not assume that there is nothing there if you didn't put it there. Your theme or plugins may have inserted content there as I've seen the description contain the_content automagically.
Do you see anything that I missed? Is there something else that you wish was added?
Pinkalicious Release Sale!
Pinkalicious is not ājust another WordPress themeā. It has built in features that will get your website up and running quickly, and it will look delicious! Featuring an awesome pink and chocolate color pallette, it is the perfect theme for blogs or even an eCommerce site. [purchase_link id="15517" text="Purchase" style="button" color="gray"]
The home page features an image slider that will allow you to present your posts, products, or services in a spectacular fashion. An optional 1, 2, or 3 column widgetized area under the slider is a quick and painless way to present your most critical information in a way that you want. Furthermore, it is packaged with a portfolio page template making portfolios easy!
Pinkalicious also has some funaliciousness! Introducing two unique features:
- the very first Grid Page Template and
- the very first Grid Posts widget
While both of these features are based on the Genesis Grid Loop, there isn't a single Genesis Child theme that has incorporated these features so integrated and easy to use. These fantastic features also make Genesis Grids easy to implement for any category, term, taxonomy, or post type regardless of location. Do you want one category for features and another for grid posts? NO Problem! It is completely flexible, and easy to use!
The theme is extensively styled and the color scheme is very ā¦ pinkalicious.
Give Pinkalicious a try ā Pinkalicious Demo | [purchase_link id="15517" text="Purchase" style="button" color="gray"]
Announcing My First Two New Genesis Child Themes: MinFocus & Pinkalicious
Here are my first two developed Child Themes. While I have built many custom child themes for clients, many of them have morphed into examples that I would rather not show. Some have been from PDF (probably originally Word), AI, and PSD to WordPress Genesis Child Theme. So here are my first two Genesis Child Themes.
MinFocus
MinFocus is a flexible theme that could be used for a photoblog, blog or other personal website. Besides the default Genesis templates, it offers a Portfolio template and an extremely flexible Grid Blog template.
The home page features a space for a slider which is widgetized for easy customization. Furthermore, it easily integrates with WP-Cycle becoming auto-enabled upon activation.Ā And MinFocus contains a default image setting for posts without a thumbnail or image inserted into the post.
Pinkalicious
Pinkalicious is a Genesis Child theme that can also be used for a photoblog, blog or other personal website. Besides the default Genesis templates, it too offers a Portfolio template and an extremely flexible Grid Blog template.
The home page features a slider that can be customized in the theme settings page, and contains a default image setting for posts without a thumbnail or image inserted into the post.
Pinkalicious Demo | [purchase_link id="15517" text="Purchase" style="button" color="gray"]
Please let me know if you would like to give one of these a test drive via a comment or Twitter (@wp_smith)
- « Previous Page
- 1
- …
- 18
- 19
- 20
- 21
- 22
- …
- 25
- Next Page »