Seo is Dead

  • Seo is Dead
  • SEO Beginners
  • SEO Intermediate
  • SEO Advance
  • Black Hat
  • Link Building
  • Security
  • WordPress
  • Ecommerce
  • Monetize
Home » Blog » 12 Code SNIPPETS for WORDPRESS

by seoisdead Leave a Comment

12 Code SNIPPETS for WORDPRESS

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 = '&raquo;';$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 &#39;';single_cat_title();echo '&#39;' . $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 &#39;' . get_search_query() . '&#39;' . $currentAfter;} elseif ( is_tag() ) {echo $currentBefore . 'Posts tagged &#39;';single_tag_title();echo '&#39;' . $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.

Keep Reading this POST:  SEO Experiment, use of rel="nofollow", "ugc" and "Sponsored" in Links.

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.

Web Analytics and CONVERSION FUNNELS

Ad Inserter | Add Adsense Advertising to your WordPress website

10 Tips to get FEATURED SNIPPET

Keep Reading this POST:  Web Analytics and CONVERSION FUNNELS

Related posts:

SEO Experiment, use of rel="nofollow", "ugc" and "Sponsored" in Links.
10 Tips to get FEATURED SNIPPET
Create and DISPLAY CUSTOM FIELDS
Web Analytics and CONVERSION FUNNELS

Filed Under: SEO Advance

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Blog

About

Privacity Polity

Cookie Policy

Site Map

    Copyright © 2023

    We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
    Cookie settingsACCEPT
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
    CookieDurationDescription
    cookielawinfo-checbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
    cookielawinfo-checbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
    cookielawinfo-checbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
    cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
    cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
    viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
    Functional
    Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
    Performance
    Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
    Analytics
    Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
    Advertisement
    Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
    Others
    Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
    SAVE & ACCEPT