Today I’m going to show you 12 code snippets for WordPress that you can insert in your functions.php file or in your functionality plugin, with which you will be able to remove and add different functionalities to your WordPress in a very simple way.
I recommend you to download the code snippets plugin to add the codes.
1. ADD CATEGORIES to the PAGES
Add a new categories section inside the custom post type of pages.
add_action( 'init', 'pages_tax' );function pages_tax() {register_taxonomy('things','page',array('label' => __( 'Categories' ),'rewrite' => array( 'slug' => 'categories' ),'hierarchical' => true,));}
2. REMOVE WEB FIELD FROM COMMENTS
Remove the web field from the comment form of your WordPress.
add_filter ('comment_form_field_url', function ($url) {return;});
3. ENABLE MAINTENANCE MODE
This piece of code simply puts your website in maintenance mode for users in a very simple way.
When you are done simply delete the code or disable the snippet.
function maintenance_mode_on() {if( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {wp_die( 'We are in maintenance. Come back in 1 hour.', 'We are under maintenance. Come back in 1 hour.', array( 'response' => '503') );}}}add_action( 'get_header', 'maintenance_mode_on' );
4. RESTRICT CONTENT FOR UNREGISTERED USERS
This code will create a shortcode with which the content between the two shortcodes will only be shown to registered users. Unregistered users will be shown a message.
[registered] private content [/registered].add_shortcode( 'registered', 'member_check_shortcode' );function member_check_shortcode( $atts, $content = null ){if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )return $content;return 'This content is for registered users only';}
5. WORD COUNTER IN THE FRONT-END
We have to copy the following code in our functionality plugin or in our functions.php file.
function wcount(){ob_start();the_content();$content = ob_get_clean();return sizeof(explode(" ", $content));}
And then we will place the following WordPress function in the template where we want it to be displayed. e.g.(content.php or content-single.php or content-page.php).
echo wcount();
6. ADD BREADCRUMBS
This is a rather more extensive code that we will have to paste into our functionality plugin as well.
function dimox_breadcrumbs() {$delimiter = '»';$name = 'Home'; //text for the 'Home' link$currentBefore = '<span class="current">';$currentAfter = '</span>';if ( !is_home() && !is_front_page() || is_paged() ) {echo '<div id="crumbs">';global $post;$home = get_bloginfo('url');echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';if ( is_category() ) {global $wp_query;$cat_obj = $wp_query->get_queried_object();$thisCat = $cat_obj->term_id;$thisCat = get_category($thisCat);$parentCat = get_category($thisCat->parent);if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' ');echo $currentBefore . 'Archive by category '';single_cat_title();echo ''' . $currentAfter;} elseif ( is_day() ) {echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';echo $currentBefore . get_the_time('d') . $currentAfter;} elseif ( is_month() ) {echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';echo $currentBefore . get_the_time('F') . $currentAfter;} elseif ( is_year() ) {echo $currentBefore . get_the_time('Y') . $currentAfter;} elseif ( is_single() && !is_attachment() ) {$cat = get_the_category(); $cat = $cat[0];echo get_category_parents($cat, TRUE, ' ' ' . $delimiter . ' ');echo $currentBefore;the_title();echo $currentAfter;} elseif ( is_attachment() ) {$parent = get_post($post->post_parent);$cat = get_the_category($parent->ID); $cat = $cat[0];echo get_category_parents($cat, TRUE, ' ' ' . $delimiter . ' ');echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';echo $currentBefore;the_title();echo $currentAfter;} elseif ( is_page() && !$post->post_parent ) {echo $currentBefore;the_title();echo $currentAfter;} elseif ( is_page() && $post->post_parent ) {$parent_id = $post->post_parent;$breadcrumbs = array();while ($parent_id) {$page = get_page($parent_id);$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';$parent_id = $page->post_parent;}$breadcrumbs = array_reverse($breadcrumbs);foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';echo $currentBefore;the_title();echo $currentAfter;} elseif ( is_search() ) {echo $currentBefore . 'Search results for '' . get_search_query() . ''' . $currentAfter;} elseif ( is_tag() ) {echo $currentBefore . 'Posts tagged '';single_tag_title();echo ''' . $currentAfter;} elseif ( is_author() ) {global $author;$userdata = get_userdata($author);echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;} elseif ( is_404() ) {echo $currentBefore . 'Error 404' . $currentAfter;}if ( get_query_var('paged') ) {if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';echo __('Page') . ' ' . get_query_var('paged');if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';}echo '</div>';}}}
Next we need to modify the template where we want the breadcrumbs to be displayed by pasting the following code in the part of the template where we want them to be displayed. (usually at the top just before the content).
<?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs();?>
7. SHOW COPYRIGHT AT THE END OF EACH POST
This code will add a text at the end of each post showing a text and the date (year) that is automatically updated. The text is fully modifiable. Replace ‘this article is copyright‘ with the text you want.
function add_post_content($content) {if(!is_feed() && !is_home()) {$content .= '<p>This article is copyright © '.date('Y').' '.bloginfo('name').'</p>';}return $content;}add_filter('the_content', 'add_post_content');
8. SEARCH ONLY POSTS IN THE SEARCH ENGINE
If you only want your WordPress search engine to show results for posts and not show results such as privacy policy, cookie policy or other pages that you are not interested in showing, simply paste this code in your functionality plugin and the WordPress search engine will only show results for posts.
function search_posts_filter( $query ){if ($query->is_search){$query->set('post_type',array('post','custom_post_type1', 'custom_post_type2'));}return $query;}add_filter('pre_get_posts','search_posts_filter');
9. ADD PAGE BREAK BUTTON IN WORDPRESS EDITOR
This code will add page break button in your WordPress editor. With this button you will be able to create different pages within the same post.
add_filter('mce_buttons','wysiwyg_editor');function wysiwyg_editor($mce_buttons) {$pos = array_search('wp_more',$mce_buttons,true);if ($pos !== false) {$tmp_buttons = array_slice($mce_buttons, 0, $pos+1);$tmp_buttons[] = 'wp_page';$mce_buttons = array_merge($tmp_buttons, array_slice($mce_buttons, $pos+1));}return $mce_buttons;}
10. CREATE MORE TAXONOMIES IN THE ENTRIES
If you need to create more taxonomies for any reason in the entries with this code you will add two more. The default is ‘Actors’ and ‘Genre’, but you can change the name to whatever you want.
If you want to add even more, just copy and paste another line below the last one and replace the name with the one you want.
add_action( 'init', 'create_my_taxonomies', 0 );function create_my_taxonomies() {register_taxonomy( 'genre', 'post', array( 'hierarchical' => false, 'label' => 'Genre', 'query_var' => true, 'rewrite' => true ) );register_taxonomy( 'actors', 'post', array( 'hierarchical' => false, 'label' => 'Actors', 'query_var' => true, 'rewrite' => true ) );}
11. REMOVE METABOX TRACKBACKS FROM WORDPRESS EDITOR
If you don’t use the option to send trackbacks this code allows you to remove it from your WordPress dashboard.
function remove_trackback_metabox() {remove_meta_box( 'trackbacksdiv','post','normal' );}add_action('admin_menu','remove_trackback_metabox');
12. PUT PAYPAL LINK FOR DONATION
This code snippet will create a shortcode that will display a direct link to PayPal where the user can make a donation.
Simply replace the PayPal email with yours, customize the text you want to be displayed and insert the following shortcode where you want the link to be displayed.
[donate]function donate_shortcode( $atts, $content = null) {global $post;extract(shortcode_atts(array('account' => 'pluginswebdudas@gmail.com','for' => $post->post_title,'onHover' => 'Donate',), $atts));if(empty($content)) $content='Make a donation';return '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation for '.$for.'" title="'.$onHover.'">'.$content.'</a>';}add_shortcode('donate', 'donate_shortcode');
If you are still not clear, here you have a video where we test all the codes:
And ready, as I always say if you have any questions, suggestions or just want to tell me something you can send me an email through the contact form or just leave a comment on this post and I will answer you as soon as possible.
Ad Inserter | Add Adsense Advertising to your WordPress website
Leave a Reply