Hooks in WordPress

Definition :

A Hook is a generic term in WordPress that refers to places where you can add your own code or change what WordPress is doing or outputting by default. Two types of hooks exist in WordPress: actions and filters. This hook code will be implement in functions.php file

Actions:

An Action in WordPress is a hook that is triggered at specific time when WordPress is running and lets you take an action. This can include things like creating a widget when WordPress is initializing or sending a Tweet when someone publishes a post.

Example:

add_action( ‘show_user_profile’, ‘my_show_extra_profile_fields‘ );
add_action( ‘edit_user_profile’, ‘my_show_extra_profile_fields‘ );
add_action( ‘personal_options_update’, ‘my_save_extra_profile_fields‘ );
add_action( ‘edit_user_profile_update’, ‘my_save_extra_profile_fields‘ );

function my_save_extra_profile_fields( $user_id ) {
update_usermeta( $user_id, ‘user_name’, $_POST[‘user_name’] );
update_usermeta( $user_id, ‘user_address’, $_POST[‘user_address’] );
update_usermeta( $user_id, ‘user_city’, $_POST[‘user_city’] );
update_usermeta( $user_id, ‘user_state’, $_POST[‘user_state’] );
update_usermeta( $user_id, ‘user_zipcode’, $_POST[‘user_zipcode’] );
update_usermeta( $user_id, ‘user_phone’, $_POST[‘user_phone’] );
}

 

function my_show_extra_profile_fields( $user ) {
?>

<h3>Extra profile information</h3>

<!– ////////////////////////////////// Additional Field start///////////////////////////////////// –>
<?php

$a = get_user_meta( $user->ID);
//print_r($a);
//echo $a[‘user_phone’][0];
?>

<p>
<label for=”register-name” style=”font-weight: bold;width: 400px;display: inline-block;font-size: 14px;”>Name</label>
<input type=”text” class=”do_input” name=”user_name” id=”user_name” size=”30″ maxlength=”20″ value=”<?php echo esc_attr($a[‘user_name’][0]); ?>” class=”regular-text” />
</p>

<p>
<label for=”register-address” style=”font-weight: bold;width: 400px;display: inline-block;font-size: 14px;”>Address</label>
<input type=”text” class=”do_input” name=”user_address” id=”user_address” size=”30″ maxlength=”20″ value=”<?php echo esc_attr($a[‘user_address’][0]); ?>” class=”regular-text” />
</p>

<p>
<label for=”register-city” style=”font-weight: bold;width: 400px;display: inline-block;font-size: 14px;”>City</label>
<input type=”text” class=”do_input” name=”user_city” id=”user_city” size=”30″ maxlength=”20″ value=”<?php echo esc_attr($a[‘user_city’][0]); ?>” class=”regular-text” />
</p>

<p>
<label for=”register-state” style=”font-weight: bold;width: 400px;display: inline-block;font-size: 14px;”>State</label>
<input type=”text” class=”do_input” name=”user_state” id=”user_state” size=”30″ maxlength=”20″ value=”<?php echo esc_attr($a[‘user_state’][0]); ?>” class=”regular-text” />
</p>

<p>
<label for=”register-zipcode” style=”font-weight: bold;width: 400px;display: inline-block;font-size: 14px;”>Zipcode</label>
<input type=”text” class=”do_input” name=”user_zipcode” id=”user_zipcode” size=”30″ maxlength=”20″ value=”<?php echo esc_attr($a[‘user_zipcode’][0]); ?>” class=”regular-text” />
</p>

<p>
<label for=”register-phone” style=”font-weight: bold;width: 400px;display: inline-block;font-size: 14px;”>Phone</label>
<input type=”text” class=”do_input” name=”user_phone” id=”user_phone” size=”30″ maxlength=”20″ value=”<?php echo esc_attr($a[‘user_phone’][0]); ?>” class=”regular-text” />
</p>

<!– ///////////////////////////////// Additional Field End ///////////////////////////////////// –>
<?php } ?>

Explanation:

add_action()

– this function works when we need to add extra functionality inside a wordpress page

The above code snippets helpful to show an extra fields in profile page and update the user information in Admin Panel and profile page also.

 

Filters

A Filter in WordPress allows you get and modify WordPress data before it is sent to the database or the browser. Some examples of filters would include customizing how excerpts are displayed or adding some custom code to the end of a blog post.

Example:

add_filter( ‘wp_mail_from’, ‘my_mail_from’ );
function my_mail_from( $email )
{
$user_info = get_userdata(1);
return $user_info->user_email;
}
add_filter( ‘wp_mail_from_name’, ‘my_mail_from_name’ );
function my_mail_from_name( $name )
{
$user_info = get_userdata(1);
return $user_info->user_login;
}

Explanation:

add_filter()

– filter is something like replacimg up on an existing function like using !important tag in css which replaces already used css in files

The above filter is used to change the from email address and name in email. This code snippets helpful to change the email address and name as a admin mail address and admin name

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s