Blocking user registration by country level domain in WordPress

Spammers are everywhere and in weDevs, every single day a lots of spammers are registering. Among of them “.pl” domains are mostly used. Enough is enough, so why not block them? Thats why wrote a function that will check the users email address and will find the country level domain (only the last part of the email address). If it finds in the blocked list, it will not allow the user to register in the site. Simple enough.

/**
 * Block selected top level domains from registering in your site
 *
 * @param object $errors WP_Error class
 * @return object errors
 */
function wedevs_block_user_registration_by_email( $errors ) {
    $email = $_POST['user_email'];
    $ext = substr( strrchr( $email, '.' ), 1 ); // get last part of dot in email
    $blocked_domains = array( 'pl' );

    if ( in_array( $ext, $blocked_domains ) ) {
        $errors->add( 'email_blocked', __( 'You are blocked from registration.', 'dlp' ) );
    }

    return $errors;
}

add_filter( 'registration_errors', 'wedevs_block_user_registration_by_email' );

Contribute code for WP Project Manager

Are you a developer?

I’ve released WP Project Manager plugin few months back. It’s an awesome piece of plugin IMHO. Working single in a project like this along with client project and other personal project is hard. It takes time to improve it a lot by working alone. There are many areas where it can be improved.

So, are you a developer and got some spare time? Why not tinker with WP Project Manager and make it more awesome?

WP User Frontend PRO released

WP User Frontend PRO

A long awaited plugin!! Today I’ve released the PRO version of my plugin WP User Frontend. It comes with a bunch of really cool features.

  • Unlimited post type form creation
  • Drag-n-drop form builder
  • Custom taxonomy support
  • 13 variations of custom fields
  • Guest post support
  • Custom Redirection
  • Image upload on post content area
  • Post status selection on new post and edited post separately
  • New or edit post notification
  • Custom fields are generated also in admin area
  • Profile form builder
  • Different profile edit forms for different user roles
  • Drag-n-Drop profile form builder
  • Profile fields are generated on backend too
  • Avatar Upload
  • Frontend profile edit
  • Registration form builder
  • Captcha Support
  • Theme My Login compatible
  • Much more…

Why don’t you check out the features?

A file based logging utility

We all do debugging. I frequently use a file based logging function that I wrote, it was very basic. Thought it could be more improved, so here’s the improved version.

It has a small dependency, WordPress’s WP_CONTENT_DIR constant, because I do WordPress a lot. But you can change the logging file path as you want.

Usage:

Drop the function anywhere your theme/plugin. The preferred way would be using it in your MU plugin.

$simple_var = 'dynamic variable';
$test_array = array('one' => 'One', 2, '3');

wp_log( 'test', 'a simple string for testing');
wp_log( 'test', 'A simple variable: ', $simple_var );
wp_log( 'sms', 'It\'s an array: ', $test_array );
wp_log( 'truth', 'A boolean True value: ', true );
wp_log( 'test', 'It should be false: ', false );
wp_log( 'group', 'PHP object: ', $post );

PHP file based Logging screenshot

Get attachment ID from “Insert into Post|Page” button

We always use this nifty “Insert into Post” button when uploading image in any types of field, including uploading a image for some meta field or anything else. Then we attach a JavaScript listener on window.send_to_editor and insert the attachment/image URL to the input field.

Today I needed a way to get the attachment ID instead of the URL. So I added an HTML5 data attribute on the link and grabbed the value.

/**
 * Add an HTML5 data attribute to the link
 * 
 * @param string $html
 * @param int $id
 * @return string
 */
function wedevs_send_to_editor( $html, $id ) {
    return str_replace( '<a href', '<a data-id="' . $id . '" href', $html );
}

add_filter( 'image_send_to_editor', 'wedevs_send_to_editor', 10, 2 );

So now you’ve the HTML5 data attribute and it’s super easy to grab the value.

window.send_to_editor = function(html) {
    var attachment_id = $(html).data('id');

    tb_remove();
};

Easy peasy :-)

IMDB like post rating plugin for WordPress

I was looking for a plugin that will work like IMDB movie rating does, but didn’t found like what I wanted. And they were bloated with features like GD Star Rating. So spend some hours and wrote one myself.

Features

  1. Creates a table {wpprefix}_imdb_rating
  2. Using IMDB_Post_Ratings::init()->rating_input(); inside a post, provides a 10 star rating. Change the number using the filter ip_base_rating. Cool huh?
  3. You can give a rating, also then can remove the rating, give again, remove again, give again…

Functions

  1. IMDB_Post_Ratings::init()->rating_input( $post_id ) Inserts the rating star bars.
  2. IMDB_Post_Ratings::init()->get_top_rated() – get top rated posts. Supports 3 parameters, post_type, limit, offset
  3. IMDB_Post_Ratings::init()->get_rating( $post_id ) – returns rating for single post.

Screenshot

screenshot

Github

Frontend for WP Project Manager released

From the first day of WP Project Manager plugin release, frontend functionality was the first demand. Today, I’ve released an extra plugin to add the frontend functionality. Managing both the backend and frontend is a daunting task, so I separated the frontend functionality to an another plugin.

  • PM - 2
  • PM - 3

However, this is a premium one. It needs WP Project Manager plugin to work, installing the frontend plugin just brings the project management functionality to the site frontend using a shortcode. Using the shortcode, either you can display all the projects in a page or a single project in a page. Version 0.4 (current) or higher version of the Project Manager plugin is needed to work on frontend.

Features:

  • Show all projects via [cpm]
  • Show a single project id [cpm id="project ID"]
  • It doesn’t now supports project creation feature from frontend

Buy Now ($15)

Sticky posts in custom post type archives

WordPress has post sticky option only for posts post type. So if you are going to create a new custom post type, there is no native way for adding support for sticky posts. This feature was considered to bring to the core, but didn’t happen sadly.

In my case, I’m using a plugin Sticky Custom Post Types to add sticky post support for my custom post type. But the problem is, the sticky posts doesn’t stick in the post type archive. If you are registering custom post type and want to display all posts from that custom post type in a page without using any page template, custom post type archive comes handy in this case. So, if you have a post type named place, WordPress will search for a page archive-place.php to display all the posts from place post type (if it supports archive. e.g: 'has_archive' => true).

But the problem arises here, you won’t see your sticky posts at the top of the post list. Because WordPress doesn’t process sticky posts on other pages except home (e.g: is_home()). So here’s the handy hack to move all the sticky posts to the top of the archive post list.

/**
 * Put sticky posts to top at custom post archives
 * 
 * WordPress doesn't do any processing for sticky posts in custom post type archives.
 * It process sticky posts in homepage only (is_home()). This function processes
 * sticky posts at custom post archive page and puts them to the top of list.
 * 
 * @author Tareq Hasan (http://tareq.weDevs.com)
 *
 * @param array $posts array of queried posts
 * @return array
 */
function wedevs_cpt_sticky_at_top( $posts ) {

    // apply the magic on post archive only
    if ( is_main_query() && is_post_type_archive() ) {
		global $wp_query;

        $sticky_posts = get_option( 'sticky_posts' );
        $num_posts = count( $posts );
        $sticky_offset = 0;

        // loop through the post array and find the sticky post
        for ($i = 0; $i < $num_posts; $i++) {

            // Put sticky posts at the top of the posts array
            if ( in_array( $posts[$i]->ID, $sticky_posts ) ) {
                $sticky_post = $posts[$i];

                // Remove sticky from current position
                array_splice( $posts, $i, 1 );

                // Move to front, after other stickies
                array_splice( $posts, $sticky_offset, 0, array($sticky_post) );
                $sticky_offset++;

				// Remove post from sticky posts array
                $offset = array_search($sticky_post->ID, $sticky_posts);
                unset( $sticky_posts[$offset] );
            }
        }

        // Fetch sticky posts that weren't in the query results
        if ( !empty( $sticky_posts) ) {

            $stickies = get_posts( array(
                'post__in' => $sticky_posts,
                'post_type' => $wp_query->query_vars['post_type'],
                'post_status' => 'publish',
                'nopaging' => true
            ) );

            foreach ( $stickies as $sticky_post ) {
                array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
                $sticky_offset++;
            }
        }

    }

    return $posts;
}

add_filter( 'the_posts', 'wedevs_cpt_sticky_at_top' );

So, grab the plugin mentioned earlier for adding sticky support to custom post type and use this code snippet to bring the sticky posts at the top.

Force JavaScript/CSS reload in WordPress themes

If you are developing with WordPress, specially creating theme, browser needs to load your latest changes. Here is a trick to force your browser to reload the script or style:

A helper function:

/**
 * Generates dynamic JS/CSS file version number for forcefully asset loading
 * 
 * @param string $file file name relative to theme directory
 * @return bool|int last file modification time
 */
function wedevs_asset_version( $file ) {
    //file path will be relative to the current theme directory
    $file_path = is_child_theme() ? STYLESHEETPATH : TEMPLATEPATH;
    $file_path = $file_path . '/' . $file;

    if ( !is_readable( $file_path ) ) {
        return;
    }

    return filemtime( $file_path );
}

Here, we are getting the last file modification time of a file (js|css) from your current theme directory and returning the value. To use this helper function, you need to call the function with relative file path to your theme.

Usage:

wp_enqueue_script( 'theme-script', get_template_directory_uri() . '/js/scripts.js', array('jquery'), wedevs_asset_version( 'js/scripts.js' ), true );
wp_enqueue_style( 'custom-css', get_template_directory_uri() . '/css/custom.css', array('style'), wedevs_asset_version( 'css/custom.css' ) );