Category: Functions

  • How to check if a user has a specific role or capability in WordPress

    In many WordPress projects, you will need to check if a user has a specific role or capability. In this tutorial, I will provide custom functions to accomplish that.

    custom function to check if the user has a specific role :

    function wptips_has_user_role($check_role){
        $user = wp_get_current_user();
        if(in_array( $check_role, (array) $user->roles )){
    		return true;
    	}
        return false;
    }

    Once you have defined the above code in functions.php, you can check for a role as given below.

    if(wptips_has_user_role('editor')){
    // write your code here
    }

    WordPress core function to check if the user has a specific capability:

    WordPress core has built-in function current_user_can , which can be used to check if currently logged in user has the given capability. an example is given below.

    if(current_user_can('edit_posts')){
    // write your code here
    }

    WordPress function references :

  • Remove meta generator tags by WordPress and other plugins

    Meta generator tag in WordPress reveals the plugin or WordPress core version which makes WordPress vulnerable. So, here i list snippets to disable meta tag generator by various plugins.

    Remove WordPress & Woocommerce meta tag generator

    This code will disable meta tag generated by WordPress, Woocommerce and many other standard plugins that hook up with the wp_generator action.

    remove_action('wp_head', 'wp_generator');
    add_filter( 'the_generator', '__return_null' );

    Remove meta generator tag of revslider plugin

    // remove revslider meta generator tag
    add_filter( 'revslider_meta_generator', '__return_empty_string' );

    Remove meta generator tag of wpbakery page builder plugin

    // remove wpbakery visual_composer meta generator tag
    add_action('wp_head', 'wpboys_remove_vc_metadata', 1);
    function wpboys_remove_vc_metadata() {
      if ( class_exists( 'Vc_Manager' ) ) {
    	remove_action('wp_head', array(visual_composer(), 'addMetaData'));
      }
    }

    Remove meta generator tag of sitepress wpml plugin

    // remove wpml meta generator tag
    if ( !empty ( $GLOBALS['sitepress'] ) ) {
            function wpboys_remove_wpml_generator() {
                remove_action(
                    current_filter(),
                    array ( $GLOBALS['sitepress'], 'meta_generator_tag' )
                );
            }
            add_action( 'wp_head', 'wpboys_remove_wpml_generator', 0 );
        }

    Complete code Snippet to remove meta generator tags from your WordPress Website

    // Disable meta generator tags
    // author : wpboys
    remove_action('wp_head', 'wp_generator');
    add_filter( 'the_generator', '__return_null' );
    // remove revslider meta generator tag
    add_filter( 'revslider_meta_generator', '__return_empty_string' );
    // remove wpbakery visual_composer meta generator tag
    add_action('wp_head', 'wpboys_remove_vc_metadata', 1);
    function wpboys_remove_vc_metadata() {
      if ( class_exists( 'Vc_Manager' ) ) {
    	remove_action('wp_head', array(visual_composer(), 'addMetaData'));
      }
    }
    // remove wpml meta generator tag
    if ( !empty ( $GLOBALS['sitepress'] ) ) {
            function wpboys_remove_wpml_generator() {
                remove_action(
                    current_filter(),
                    array ( $GLOBALS['sitepress'], 'meta_generator_tag' )
                );
            }
            add_action( 'wp_head', 'wpboys_remove_wpml_generator', 0 );
        }

    Remove generator tags from Feed

    WordPress-based website feeds have a generator tag that reveals the WordPress core version. You can remove the generator tag using the following code :

        remove_action( 'rss2_head', 'the_generator' );
        remove_action( 'rss_head',  'the_generator' );
        remove_action( 'rdf_header', 'the_generator' );
        remove_action( 'atom_head', 'the_generator' );
        remove_action( 'commentsrss2_head', 'the_generator' );
        remove_action( 'opml_head', 'the_generator' );
        remove_action( 'app_head',  'the_generator' );
        remove_action( 'comments_atom_head', 'the_generator' );
  • Remove website field from comment form to reduce spam

    Remove website field from comment form to reduce spam

    Spammers love WordPress comment forms. They use bots that automatically post spam comments with malicious links in hundreds of websites. Their goal is to gain SEO juice out of your site. But what if there is a way to discourage them from using such tools on your website? Its simple, we can disable the website or URL field in the comment form, and it will not let them post any spammy links to your site.

    How to remove website URL field from WordPress comment form without plugin

    By default, WordPress comment form has name, email, website url and comment field. There is a comment_form_default_fields filter in /wp-includes/comment-template.php file in WordPress core, which allows you to filter or change all the default comment form fields. So, we can use comment_form_default_fields filter and unset the url field. Next, we will remove the make_clickable function from comment_text filter to disable auto-linking of urls from comment text. Here is all the code snippet to paste in functions.php file of your website’s active theme.

    <?php
    /* Filter to unset website URL Field from WordPress comment form */
    add_filter('comment_form_default_fields', 'wptips_remove_website_field');
    function wptips_remove_website_field($fields){
        if(isset($fields['url']))
           unset($fields['url']);
           return $fields;
    }
    /* remove make_clickable filter to disable autolinking of urls in WordPress comment text */
    remove_filter( 'comment_text', 'make_clickable', 9 );

    Strip comment author link from comment list

    You can also unlink all the comment author names from the comment list. To achieve this, use get_comment_author_link filter and remove the link via strip_tags function.

    <?php
    // strip comment author links
    function wptips_strip_comment_author_links( $link, $author, $comment_id ){
        return $author;
    }
    add_filter( 'get_comment_author_link', 'wptips_strip_comment_author_links', 10, 3);
    ?>
  • How to create a short code to display recent posts

    You may want to display most recent posts of your website in your WordPress website post or page. You can achieve that creating a custom WordPress shortcode. Let me first explain what is a WordPress shortcode and how it works.

    What is a WordPress shortcode?

    WordPress Shortcode is a feature defined in WordPress core that allows you to define a code programmatically for any dynamic content. You can then use the code inside WordPress post editor, page editor, custom post editor, or WordPress widget. You have to use the defined code wherever you want to display the dynamic content, and you can see the new dynamic content in place of the code in front-end. WordPress shortcode is one of the most important features in WordPress that makes it very easy to use.

    add_shortcode function

    To define a custom WordPress shortcode, you have to use a WordPress core function called add_shortcode. It accepts two parameters, the tag or code name in string format and a callback function name. The call back function should have a definition of the dynamic content that you want to return in place of the shortcode. Remember, the call back function should always return the dynamic content that you want to display.

    Here is an example of creating a Hello World shortcode.

    <?php
    add_shortcode( 'hello_world', 'wptips_helloworld' );
    function wptips_helloworld( $atts ) {
        return "Hello World";
    }
    ?>

    Once you have defined the above shortcode, you can call the shortcode by using the code [hello_world] . It will show as “Hello World” , in frontend.

    Now, Lets create a custom shortcode to display 5 recent posts.

    <?php
    function wptips_recent_posts_shortcode($atts){
    $query = new WP_Query(array( 'orderby' => 'date', 'posts_per_page' => '5') );
    $list = '<ul class="recent-posts">';
      while($query->have_posts()) : $query->the_post();
    		$list .= '<li>' . get_the_date() . '<a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '<br />' . get_the_excerpt() . '</li>';
      endwhile;
    wp_reset_query();
    $list .= '</ul>';
      return $list;
    }
    add_shortcode('recent-posts', 'wptips_recent_posts_shortcode');
    ?>