Plugin: To create a plugin we should create a directory in the wp folder and create a .php file in that dir. The plugin detail needs to be written inside a comment block to make this visible to wp.
Filter Hooks: We use add_filter() & remove_filter() to modify content i.e. text. Filter is a way to modify the post content. Two simple filter hook, first one makes first letter of each words of title to uppercase; second one adds a timestamp at the end of content of posts.
<?php
/*
* Plugin Name: sajib filter
* Plugin URI: net.sajibbiswas.com
* Description: just demo
* Author: sajib
* Author URI: sajib.com
* Version: 1.0
*/
add_filter('the_title', ucwords);
add_filter('the_content', function($content){
return $content . '<br>' .time();
});
Action Hooks: We use actions, add_actions() & remove_actions() to hook into specific events that occurs in the lifecycle of wp. The first function of the following codeblock echoes a sentence in the footer area and the second one sends an email each time a new comment is left on the blog.
add_action('wp_footer', function(){
echo "hello, from the footer.";
});
add_action('comment_post', function(){
$email = get_bloginfo("admin_email");
wp_mail(
$email,
'new comment posted',
'A new comment has been left on your blog'
);
});
A simple plugin that adds 3 recent post’s link at the end of the content.
add_filter('the_content', function($content){
if( !is_singular('post') ){
return $content;
}
$terms = get_the_terms( the_ID(), "category");
$cats = array();
foreach( $terms as $term){
$cats[] = $term->term_id;
}
$loop = new WP_Query( array(
'posts_per_page' => 3,
'category__in' => $cats,
'orderby' => 'rand',
'post__not_in' => array( get_the_ID() ),
));
if( $loop->have_posts() ){
$content .= '<h2>You might also like....</h2>'
. '<ul class="related-cat-posts">';
while( $loop->have_posts() ): $loop->the_post();
$content .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
endwhile;
$content .= '</ul>';
wp_reset_query();
}
return $content;
});
Leave a Reply