hooking into wordpress craig ralston [email protected]

17
Hooking into WordPress Craig Ralston [email protected]

Upload: nicolette-barrick

Post on 22-Dec-2015

236 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Hooking into WordPress Craig Ralston craig@websolutions.com

Hooking into WordPress

Craig Ralston

[email protected]

Page 2: Hooking into WordPress Craig Ralston craig@websolutions.com

Why use hooks?

• Extend core functionality

• Allow others to alter your work

[email protected]

Page 3: Hooking into WordPress Craig Ralston craig@websolutions.com

What are hooks?

ActionsDO something new

• do_action()• add_action()• has_action()• remove_action()

FiltersMODIFY existing data

• apply_filters()• add_filter()• has_filter()• remove_filter()

[email protected]

Page 4: Hooking into WordPress Craig Ralston craig@websolutions.com

Actions – How are they used?

Add an alert to the admin area using a core hook

function cor_admin_notice() { ?>

<div class="cor_alert">

<h3>Hello World!</h3>

</div>

<?php }

add_action( 'admin_notices', 'cor_admin_notice' );

[email protected]

Core hook Our function

Page 5: Hooking into WordPress Craig Ralston craig@websolutions.com

add_action( $hook, $function, $priority, $args );

$hook – (required) Name of the hook you are attaching to

$function – (required) Name of the function you wish to call

$priority – (optional) Prioritize multiple functions on the same hook

$args – (optional) Number of arguments $function takes

add_action( 'admin_notices', 'cor_admin_notice' );

[email protected]

Page 6: Hooking into WordPress Craig Ralston craig@websolutions.com

Creating Actions

function cor_admin_notice() { ?>

<div class="cor_alert">

<h3>Hello World!</h3>

</div>

<?php }

[email protected]

Page 7: Hooking into WordPress Craig Ralston craig@websolutions.com

function cor_admin_notice() {

echo '<div class="cor_alert">';

do_action( 'before_alert_text' );

echo apply_filters( 'filter_alert_text', '<h3>Hello World</h3>');

do_action( 'after_alert_text' );

echo '</div>';

}

Action

Filter

Action

[email protected]

Page 8: Hooking into WordPress Craig Ralston craig@websolutions.com

Let’s add in our own stuff now

Add our image before the alert message

function add_image_above_alert() {

$image_src = '/images/alert.png';

echo '<img src="'. $image_src . '" alt="alert image" />';

}

add_action( 'before_alert_text', 'add_image_above_alert');

Add a paragraph after the alert message

function add_text_below_alert() {

echo '<p>Some really awesome text</p>';

}

add_action( 'after_alert_text', 'add_text_below_alert');

[email protected]

Page 9: Hooking into WordPress Craig Ralston craig@websolutions.com

function cor_admin_notice() {

echo '<div class="cor_alert">';

do_action( 'before_alert_text' );

echo apply_filters( 'filter_alert_text', '<h3>Hello World</h3>');

do_action( 'after_alert_text' );

echo '</div>';

}

Action

Filter

Action

[email protected]

Page 10: Hooking into WordPress Craig Ralston craig@websolutions.com

echo apply_filters( 'filter_alert_text', '<h3>Hello World</h3>');

apply_filters( $hook, $value, $var );

$hook – (required) Name of the filter hook

$value – (required) Value to be modified

$var – (optional) One or more additional variables passed to the filter functions

[email protected]

Page 11: Hooking into WordPress Craig Ralston craig@websolutions.com

Let’s change the original alert message

function change_alert_text( $text ) {

$text = '<h3>Our new custom alert message</h3>';

return $text;

}

add_filter( 'filter_alert_text', 'change_alert_text', 10, 1 );

[email protected]

Page 12: Hooking into WordPress Craig Ralston craig@websolutions.com

function cor_admin_notice() {

echo '<div class="cor_alert">';

do_action( 'before_alert_text' );

echo apply_filters( 'filter_alert_text', '<h3>Hello World</h3>');

do_action( 'after_alert_text' );

echo '</div>';

}

Action

Filter

Action

[email protected]

Page 13: Hooking into WordPress Craig Ralston craig@websolutions.com

Filter profanity in the content area

function cor_filter_profanity( $content ) {

// Store an array of what we want to consider profanity

$profanities = array( 'badword','alsobad','reallybad' );

// Check the content for profanities before output

// Replace them with {censored}

$content = str_ireplace($profanities, '{censored}', $content);

return $content;

}

add_filter ( 'the_content', 'cor_filter_profanity', 10, 1 );

[email protected]

Page 14: Hooking into WordPress Craig Ralston craig@websolutions.com

Action or Filter?

• When Sarah arrives home, tell her to call me.

• When Sarah makes her grocery list, make sure she adds beer to the list

[email protected]

Page 15: Hooking into WordPress Craig Ralston craig@websolutions.com

When Sarah gets home, have her call me.

// When Sarah gets home, have her call me.

add_action( 'after_sarah_arrives' , 'sarah_call_craig', 10 , 1 );

function sarah_call_craig( $sarah_has_phone ) {

// if $sarah_has_phone is true

if ( $sarah_has_phone ) {

echo 'Sarah, would you please call Craig? Thanks.';

}

}

[email protected]

Page 16: Hooking into WordPress Craig Ralston craig@websolutions.com

When Sarah makes her grocery list, add beer.

// Modify Sarah's grocery list

add_filter( 'sarahs_grocery_list' , 'cor_add_beer', 10, 1 );

function cor_add_beer( $grocery_list ) {

// Append another item to the list

$grocery_list = $grocery_list . ' Beer.';

return $grocery_list;

}

[email protected]

Page 17: Hooking into WordPress Craig Ralston craig@websolutions.com

Helpful Resources

http://codex.wordpress.org/Plugin_API

http://adambrown.info/p/wp_hooks

[email protected]

Questions?