Difference between the_permalink() and get_permalink() in wordpress

Introduction

WordPress have many helper functions. the_permalink() and get_permalink() functions are one of them . We use this functions frequently through out the WordPress related development. If you are not new in WordPress , this functions are not new to you. Both functions pretty much does the same things however there is a difference, which we are going to talk about.

Getting started

the_permalink() function will echo out the permalink of the current post . We don’t have to echo it out like we do with get_permalink() function. Let’s talk about the differences with the snippet codes from WordPress.

In simple term, <?php the_permalink(); ?> is equivalent to <?php echo esc_url(get_permalink()); ?>. Can you see the difference with the codes ?

In the_permalink() function we didn’t use esc_url() function and also didn’t echo it out. The WordPress will do it for us. Let’s take a look at the WordPress doc about how are the functions working for us .

the_permalink()

function the_permalink( $post = 0 ) {
    echo esc_url( apply_filters( 'the_permalink', get_permalink($post), $post ) );
}

the_permalink() function uses the get_permalink() function under the hood with sanitization and filters.

For more information about the get_permalink() .

Happy coding 🙂

Permalinks are the permanent URLs to your individual weblog posts, as well as categories and other lists of weblog postings. — WordPress

Customize the search form as you want in WordPress

Introduction

Unlike any other default WordPress widgets,WordPress makes it easy to style and format your search form widget. As search form may vary from site to site in context of design, you can change the way your search form looks.

How do we customize the search form ?

The things need to be remember at all times is, like header.php, footer.php and many other conventions , search form also have it’s own convention derived by WordPress.

Let’s create searchform.php in your theme root folder.

The WordPress will know we have our own custom search form template after the  creation of the file searchform.php and will load it instead of WordPress default search form template.

Just an snippet example for our searchform.php file :

Snippet for searchform.php

As we have created our own template file for search form, now we may use it multiple times through out our theme. We just need to call the function in the place where we want the search form.

Easy right 🙂 . Happy coding 🙂

Multiple headers in wordpress

Introduction

In wordpress, we are bound to use header.php naming convention for our header. Then we can call get_header() functions to retrieve the header file. Sometimes we might have multiple header as per our requirements.

How can we use multiple headers ?

WordPress is amazing , using multiple headers is easy. Let’s create a multiple header for our application.

Let’s name our second header as header-v2.php. [ We assume you probably know the idea of using header in wordpress. ]

Now all we have to do to use the 2nd header (header-v2.php) is :

<?php get_header(‘v2’); ?>

Easy right. 🙂 WordPress will search header hyphen v2 and include it in your file. Using this way you can manage multiple headers in your wordpress theme or application. Happy coding 🙂

How to turn on error reporting in wordpress ?

I’ll let you know beforehand , that please do not do this in live . This is meant for only testing and debugging on local environment. To get more detailed error reporting from your wordpress site, please follow along with me.

You need to add following lines of code in wp-config.php file in wordpress.

<?php 

define('WP_DEBUG', true); // update false to true
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors', 0);

You see only by doing this changes, you’ll get more detailed error information. Once you finished debugging or testing please remove the lines of codes and set WP_DEBUG constant to false.

wp_create_user and wp_insert_user

These two functions are available to create a new user in wordpress.

These both functions does the exact thing except wp_create_user is wrapper as in more simplified version of wp_insert_user.

wp_create_user( string $username, string $password, string $email = ” )

wp_create_user() function creates a new user with just the username, password, and email, for more complex user creation or to specify more information about user we use wp_insert_user() function.

wp_insert_user( array|object|WP_User $userdata )

As you can see wp_create_user() function accept three parameters username , password and email of the new user, wp_inser_user accept array/object of new user.

Let’s see how these functions work :

When creating a new user using wp_create_user() function, we need to pass three parameters :

function create_user( $user_name, $user_email ) {
     $user_id = username_exists( $user_name ); 
    if ( ! $user_id && false == email_exists( $user_email ) ) { 
        $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false ); 
        $user_id = wp_create_user( $user_name, $random_password, $user_email ); 
    } else { 
        $random_password = __( 'User already exists.', 'textdomain' ); 
    }
}

For the sake of simplicity, we copied this code from the official document of wordpress. I just wrapped this code with in a function named create_user.

As you can see , there is two parameters only passed to the function. Why ? We’ll get there soon. Line of code, we checked does the $user_name and $user_email exists in our database , if false than we generated random password with a length of 12 and excluding standard special characters in our password like double quote (“), Number sign (hash),Dollar sign ($) . If $user_name and $user_email already exist in our database ,  Message will be shown “User already exists. Password inherited”.

After generating the $random_password, now we have three parameters with us . wp_create_user( $user_name, $random_password, $user_email )

After passing the parameters, the function will create a new users. If user is not created due to problems , WP_Error come’s in handy . we’ll talk about WP_Error in up coming days.

As WordPress documentation says ” wp_insert_user() is a more complete way to create a new user.”

Absolutely with above statement, cause wp_create_user() function uses the wp_insert_user() function under the hood. Let’s look at the wp_create_user() function :

function wp_create_user($username, $password, $email = '') {
    $user_login = wp_slash( $username );
    $user_email = wp_slash( $email    );
    $user_pass = $password;
    $userdata = compact('user_login', 'user_email', 'user_pass');
    return wp_insert_user($userdata);
}

We can see that the parameters that we passed will be checked using wp_slash() function, which add slashes to a string or array of strings and then all the parameters are passed down to the compact function.

compact( ) function in php creates an array from variables and their values.

You see the array contains our username, email and password and finally passed down to the wp_insert_user() function.

let’s dive into wp_insert_user() function for a bit. As you can see wp_insert_user() function accepts (array|object|WP_User) $userdata, $userdata may contain ID, user_pass, user_login, user_nicename, user_email and many other attributes for the user. You can see all the attributes from this link .

The wp_insert_user() is a complete function for creating a user so it contains little more lines of codes compared to the wp_create_user() function.

https://developer.wordpress.org/reference/functions/wp_insert_user/#source

In above link , you can see the whole function for wp_insert_user() . In the function , $wpdb is called which is a class that contains a set of functions used to interact with a database. After checking if the $userdata is instanceof  stdClass or WP_User, The function will check if its about to create new instance of user or the update of the user.

// Are we updating or creating?
    if ( ! empty( $userdata['ID'] ) ) {}

After that , you can see there is whole lot of checking if username exists, email exists , generating hash password for the user. This is how wp_insert_user() functions works in a brief introduction.

Why wordpress prefer array() instead of short array syntax [] ?

Short array syntax [] was introduced a while back when PHP 5.4 was released. Basically it is ability to define arrays using [] instead of array().

<?php 

$arr1 = array(
	'motto' => 'learning is fun.',
	'url'   => 'raisachin.com.np'
);

$arr2 = [
	'motto' => 'learning is fun.',
	'url'   => 'raisachin.com.np'
];

echo '<pre>'; 
print_r($arr1); 
echo '</pre>';

Above both will result the same.

WordPress prefer array() instead of short array syntax , because wordpress can run on PHP 5.2 , and [] was not introduced back then. Using short array syntax can breaks some sites running on wordpress.