Loading
Jacob Snyder
This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.
add_filter( $tag, $function_to_add, $priority=10, $accepted_args=1 )
apply_filters( $tag, $value )
$message = "Hello user";
$message = apply_filters( 'message_filter', $message );
echo $message;
add_filter( 'message_filter', 'custom_message' );
function custom_message( $message ) {
$user = wp_get_current_user();
$message = "Hello " . $user->first_name;
return $message;
}
add_action( $tag, $function_to_add, $priority=10, $accepted_args=1 )
do_action( $tag, $arg = '' )
add_action( 'wp_head', 'custom_add_analytics_production' ); function custom_add_analytics_production() { if (! defined('WP_DEBUG') || ! WP_DEBUG ) : ?>
<script> [Analytics code] </script>
<?php endif;
}
function wp_head() { do_action( 'wp_head' ); }
add_filter( 'gettext', 'custom_replace_howdy', 10, 3 );
function custom_replace_howdy( $translated, $text, $domain )
{
if ( false !== strpos($translated, "Howdy") )
{
return str_replace("Howdy", "Welcome back", $translated);
}
return $translated;
}
Before: After:
add_filter( 'the_content', 'custom_share_icons' );
function custom_share_icons( $content ) {
$content = '<h3>Share this post on:</h3>
<ul class="share-icons">
<li class="facebook"><a href="#facebook">Facebook</a></li>
<li class="twitter"><a href="#twitter">Twitter</a></li>
</ul>';
return $content;
}
add_filter( 'the_content', 'custom_share_icons' ); function custom_share_icons( $content ) { if ( in_the_loop() && is_main_query() ) { $content = '<h3>Share this post on:</h3>
<ul class="share-icons">
<li class="facebook"><a href="#facebook">Facebook</a></li>
<li class="twitter"><a href="#twitter">Twitter</a></li>
</ul>';
} return $content; }
add_action( 'wp_enqueue_scripts', 'custom_scripts', 999 );
function custom_scripts()
{
wp_register_script( 'theme-js', get_stylesheet_directory_uri().'/scripts.js' );
wp_enqueue_script( 'theme-js' );
}
add_action( 'plugin_name/page_navi', 'custom_page_navi', 10, 2 );
function custom_page_navi()
{
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 ) return;
echo '<nav class="pagination">';
echo paginate_links( array(
'base' => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
'format' => '',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) );
echo '</nav>';
}
<?php do_action( 'plugin_name/page_navi' ); ?>
add_action( 'init', 'custom_test_init' );
function custom_test_init() {
print_r($GLOBALS['post']);
}
add_action( 'wp', 'custom_test_init' );
function custom_test_init() {
print_r($GLOBALS['post']);
}
add_filter( 'excerpt_more', 'aelc_excerpt_more' );
function aelc_excerpt_more( $more )
{
global $post;
$output = "…";
$output .= '<br />';
$output .= '<a class="excerpt-read-more" data-post_id="'. $post->ID .'" href="'. get_permalink($post->ID) . '" title="Read ' . get_the_title($post->ID).'">';
$output .= "Read more";
$output .= '</a>';
return $output;
}
add_action( 'wp_enqueue_scripts', 'aelc_enqueue_scripts', 999 ); function aelc_enqueue_scripts() { wp_register_script( 'aelc-js', plugins_url( 'assets/js/scripts.js', __FILE__ ), array( 'jquery' ), '', true ); $args = array( 'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'aelc_excerpt_more' ),
); wp_localize_script( 'aelc-js', 'aelc', $args ); wp_enqueue_script( 'aelc-js' ); }
$args = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'aelc_excerpt_more' ),
);
wp_localize_script( 'aelc-js', 'aelc', $args );
add_action( 'wp_ajax_aelc_excerpt_more', 'aelc_load_content' );
add_action( 'wp_ajax_nopriv_aelc_excerpt_more', 'aelc_load_content' );
function aelc_load_content()
{
if ( empty($_POST['post_id']) || empty($_POST['nonce']) ) return;
if (! wp_verify_nonce($_POST['nonce'], 'aelc_excerpt_more')) return;
$post = get_post($_POST['post_id']);
echo apply_filters( 'the_content', $post->post_content );
die;
}
Add the Ajax actions
add_action( 'wp_ajax_aelc_excerpt_more', 'aelc_load_content' );
The action name used in Javascript
add_action( 'wp_ajax_nopriv_aelc_excerpt_more', 'aelc_load_content' );
The action name used in Javascript
The function name
Make sure the expected variables have been supplied
if ( empty($_POST['post_id']) || empty($_POST['nonce']) ) return;
Verify the nonce
if (! wp_verify_nonce($_POST['nonce'], 'aelc_excerpt_more') ) return;
Name of the nonce action
Get the post, apply filters, echo to javascript
$post = get_post($_POST['post_id']);
echo apply_filters( 'the_content', $post->post_content );
die( '0' );
jQuery(document).ready(function($) { $('.excerpt-read-more').on('click', function(e) { e.preventDefault(); var $this = $(this), $content = $this.closest('p'); $.ajax({ url: theme.ajaxurl,
type: 'post',
async: true,
cache: false,
dataType: 'html',
data: {
'action' : 'custom_excerpt_more',
'post_id' : $(this).attr('data-post_id'),
'nonce' : theme.nonce,
}, success: function( response ) { if ( response )
{
$content.fadeOut('400', function(){
$content
.after( response )
.remove();
});
}
} }); }); });
Add a custom onClick event to the link
$('.excerpt-read-more').on('click', function(e) {
And stop the default action
e.preventDefault();
The data we are passing to PHP
PHP handler action name (without the "wp_ajax_" part
'action' : 'aelc_excerpt_more',
The post id from the link html
'post_id' : $this.data('post_id'),
The nonce
'nonce' : aelc.nonce,