The Complete Apache Spark Collection [Tutorials and Articles]

In this edition of "Best of DZone," we've compiled our best tutorials and articles on one of the most popular analytics engines for data processing, Apache Spark. Whether you're a beginner or are a long-time user, but have run into inevitable bottlenecks, we've got your back!

Before we begin, we'd like need to thank those who were a part of this article. DZone has and continues to be a community powered by contributors like you who are eager and passionate to share what they know with the rest of the world. 

How To Ease Your Team’s Development Workflow With Git Hooks

How To Ease Your Team’s Development Workflow With Git Hooks

How To Ease Your Team’s Development Workflow With Git Hooks

Konstantinos Leimonis

One of the major requirements working for a team or an open-source project is using a version control system (VCS). Git is a free and open-source distributed version control system for tracking source code changes during software development. It’s been created by Linus Torvalds in 2005 for the development of the Linux kernel. It is easy to learn and has a tiny footprint with lightning fast performance.

There is a big chance you have already used Git (since it is one of the most popular and well-adopted VCS tools available in the development community), and you most probably already have some knowledge about staging and committing your code by pushing and pulling it from a remote repository. This article won’t address the basics of git workflows but will mostly focus on git hooks and how to utilize them in order to achieve better collaboration in your team. With teams growing in size, it’s becoming even more important to keep contributors in line and maintain different rules about the code.

What Are Git Hooks?

Git hooks are scripts that are triggered when specific actions or events are performed in a git repository. These actions are about parts of the version control workflow such as committing and pushing. Hooks can be really useful by automating tasks on your git workflow. For example, they can help us validate the syntax of our codebase based on some specific rules, or run some tests before committing our changes.

How To Get Them Set?

Git hooks are a built-in feature, meaning that we are able to access them and start using them as long as a git repository is initialized. Let’s have a look in more detail what that means by trying to get them set.

Using your favorite terminal, create a new git repository.

mkdir my-new-repository && cd my-new-repository
git init
ls -la

You will notice that a new hidden directory has just been created. This folder .git is used from git for storing repository related information, such as version control hashes, information about commits, remote repository addresses, and so on. This is also the folder where hooks actually live for git .git/hooks. You can find some pre-populated sample scripts automatically created during the initialisation. These are actually the scripts that will get triggered after specific actions.

ls .git/hooks

Some of the samples you can find are:

  • pre-commit.sample: invoked just before making the commit.
  • commit-msg.sample: edit the message file in place.
  • post-receive.sample: invoked after the remote repository has updated.

Under The Hood

Now that we know where we can find hooks, let’s take a step back in order to understand how do they actually work.

Git hooks are event-based, so as long as we execute a git command in the development flow, git will check hooks folders in order to find if there is an associated script to run. Some of these scripts will run before or after these development flow actions.

A good example for us to go through and more specifically understand the flow under which hooks get triggered is the committing workflow which is a quite familiar use case.

Whenever we commit any changes to our codebase, some of these related hooks are triggered in the following order:

  1. pre-commit: inspects the snapshot that’s about to be committed and verify what is to be committed.
  2. prepare-commit-msg: lets you edit the default message before the commit author sees it.
  3. commit-msg: sets commit message to a template.
  4. post-commit: runs an action just after the commit has been completed, and sends a notification for instance.
Hooks executing during the commit creation process
Hooks executing during the commit creation process (Image credits: Atlassian Bitbucket) (Large preview)

In the above repository, let’s now try and add some custom pre- and post-commit scripts in order to further visualize how git hooks actually work.

nano .git/hooks/pre-commit

Add the following snippet:

#!/bin/sh
echo Changes are about to be committed

Make sure our scripts are executable:

chmod +x .git/hooks/pre-commit

Repeat the above process for the post-commit script:

nano .git/hooks/post-commit
#!/bin/sh
echo Changes have been committed
chmod +x .git/hooks/post-commit

Now we can add a new file nano index.html with a small HTML snippet just for demonstration purposes (no need to let HTML validators know about this).

<h1>Hello world from our new repository!</h1>

We’ll be adding the changes in our codebase through staging and then committing this:

git add .
git commit

After committing has been successfully processed, we can see the following output of the two above-added scripts:

Changes are about to be committed
Changes have been committed

As expected, git triggered hooks in the committing flow. The pre-commit and post-commit scripts that have been added are running and will get executed in the correct sequence (based on the order we mentioned earlier).

This was a simple demonstration in order to understand how the committing workflow scripts work and how they are executed. For more details on this workflow, you can read more in the documentation.

In the above example, we chose to write these two scripts in bash but the truth is that git supports hooks that can be written in any scripting language we want. Ruby, Python or JavaScript are great alternatives, as long as we set the correct shebang at the first line of our executable script.

For example, we can rewrite the pre-commit hook as a Node.js script like below:

#!/usr/bin/env node
console.log("Changes are about to be commited")

Local And Remote Hooks

Hooks are separated between local and remote (or client and server). While local hooks run before or after specific actions on the local repository, remote hooks run before or after pushes to the server. Local ones cannot be used for enforcing policies since their nature lets developers easily alter them. They are mostly used for sticking to some specific guidelines that we want to apply within a team. In case we want to get stricter and enforce some policies for our repository, we reside in remote hooks.

Local Hooks

  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit
  • applypatch-msg
  • pre-applypatch
  • post-applypatch
  • pre-rebase
  • post-rewrite
  • post-checkout
  • post-merge
  • pre-push

Remote Hooks

  • pre-receive
  • update
  • post-receive

Sharing Hooks

Git hooks are all about sharing them within the team. This is the main reason they exist: promoting better team collaboration, automating detrimental processes, and letting us focus only on the important parts of the codebase.

As stated before, .git/hooks is the folder that hosts our customized hooks, but this is not really helpful when we need to share these scripts within the team since this folder is not tracked by git.

A good approach for solving this is adding all of our custom hooks on a separate folder inside our repository. For example, we can add a .githooks folder and save the executable scripts over there. Then, on project initialization, we can either explicitly copy or symlink these scripts to the original folder for keeping our hooks .git/hooks.

find .git/hooks -type l -exec rm {} \\;
find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \\;

Alternatively, if you’re using the latest git version (speaking of 2.9 and above) we are able to directly configure the git hooks path to our custom folder:

git config core.hooksPath .githooks

Git Hooks Made Easy (A JavaScript Codebase Use Case)

There are tools that help us further integrate git hooks to the needs of our codebase. Specifically for JavaScript codebases, there is Husky with which we can easily customize actions on git events through configuration.

For example, we can easily lint our code or run some tests in the pre-commit event and proceed to commit based on whether the linting, testing or both are successful or not.

This can be accomplished by extending the package.json configuration simply as:

{
    "scripts": {
        "test": "echo Running tests"
    },
    "devDependencies": {
        "eslint": "5.16.0",
    },
    "husky": {
        "hooks": {
            "pre-commit": "eslint . && npm test",
        }
    }
}

Conclusion

In this article, we’ve found out that different actions taken on a git repository can optionally trigger custom scripts to run. Those scripts can be under the control of the developer locally or managed more centrally for a team or project on a remote. We’ve also learned that scripts are often written in a shell script like bash, but can actually use almost any scripting language, even JavaScript.

Git hooks can be a really powerful part of a well-designed workflow, and I’d encourage you to give them a try and see what you can do for your own projects.

Smashing Editorial (dm, il)

How to Add an Author Info Box in WordPress Posts

Do you want to add an author bio box in your WordPress posts? The author bio box is a small section where you can display information about the blog’s author, show their social media profiles, and more.

Many WordPress themes allow you to easily display author bio using the default WordPress functionality. However, some themes may not have this feature built-in, or you may want to change how they display the author bio section.

In this article, we will show you multiple ways to easily show an author info box in WordPress posts. We will also show you how to customize author bio and make it more useful.

Easily add an author info section to WordPress posts

Why and When You Need an Author Info Box in WordPress

Seeing an actual person behind the content helps build credibility and strengthens your site’s authority among users.

For a single-author WordPress blog, you can just add an about me page, but for a multi-author WordPress sites, you’ll need to add an author info box below each post.

This helps your users learn more about individual authors on your website. It also provides authors an additional incentive to contribute more often and interact with readers.

If you want more users to submit content to your website, then the author info box is a great way to attracts writers who are looking for exposure and new audiences.

Having said that, let’s take a look at how to easily add an author info box in WordPress posts.

Adding Author Info Box in WordPress Posts

WordPress is the best website builder in the world because of the flexibility and customization options it offers.

There are many different author bio plugins that you can use to add an author info section. We will show you the default WordPress method, two different plugins as well as the code method. This way you can choose a method that works best for your site.

Method 1: Adding Author Bio Using Your WordPress Theme

If your WordPress theme comes with an author information box below each article, then you can simply use that to display your author bio.

In order to make use of it, you will simply need to visit Users » All Users page. From here you need to edit the user you want to change.

Editing a user profile

On the profile edit screen, scroll down to the ‘Biographical info’ section to add the author’s bio. You can also use HTML in this field to manually add links to the author’s social media profiles.

Add author bio in user profile

The author profile image is fetched using Gravatar. If the author has not set up a gravatar photo, then you can ask them to follow our guide for setting up a gravatar photo in WordPress.

Alternatively, you can also allow users on your website to upload a custom author profile photo by editing their profile.

Don’t forget to click on the ‘Update user’ button to save your changes.

You can now visit any article on your website to see your WordPress theme display author bio box below the content.

Preview default author info box

Method 2. Adding Author Info Box in WordPress Using a Plugin

If your theme does not show an author info box, or you want to customize it, then this method is for you.

For this method, we’ll be using a WordPress plugin to add author info box to your WordPress posts.

First, thing you need to do is install and activate the Author Bio Box plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit Settings » Author Bio Box page to configure plugin settings.

Author Bio Box settings

From here, you can choose colors, gravatar size for the author photo, and location to display author bio box.

Once you are done, don’t forget to click on the ‘Save changes’ button to store your changes.

Next, you need to visit Users » All Users page and click on the ‘Edit’ link below the user you want to change.

Editing a user profile

On the profile edit screen, you need to scroll down to ‘Contact Info’ section. From here you can add links to the author’s social media profiles.

Add social media links to user profile

The plugin will only show icons for social networks where you enter a URL.

After that, you can scroll down to the ‘Biographical Info’ section to add the author’s bio. You can also use HTML in this field to manually add links or use basic HTML formatting options.

Add author biographical information

Once you are finished, click on the Update user button to save your changes.

You can now visit any article written by that user to see the author info box in action.

Author info box plugin preview

Method 3: Display Author Info in a Sidebar Widget

Do you want to show the author info in the sidebar instead of below the article? If yes, then this method is for you because it allows you to show author info box in a sidebar widget.

For this method, you’ll need to install and activate the Meks Smart Author Widget plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit Appearance » Widgets page. There you will find Meks Smart Author under the list of available widgets. You need to add this widget to the sidebar where you want to display the author information.

Adding author info widget

The widget comes with a number of options. The most important option that you need to check is the checkbox next to ‘Automatically detect author’ option.

Click on the Save button to store your widget settings. You can now visit your website to see the author’s information widget in action.

Author bio box widget

This plugin fetches user information from their WordPress profile. You or your authors will need to fill their biographical information by editing their profiles.

Method 4. Add Author Info Box Manually (Coding Required)

This method requires you to add code to your WordPress website. If you have not done this before, then please take a look at our guide on how to add code snippets in WordPress.

First, you need to add the following code to your theme’s functions.php file or a site-specific plugin.

function wpb_author_info_box( $content ) {
 
global $post;
 
// Detect if it is a single post with a post author
if ( is_single() && isset( $post->post_author ) ) {
 
// Get author's display name 
$display_name = get_the_author_meta( 'display_name', $post->post_author );
 
// If display name is not available then use nickname as display name
if ( empty( $display_name ) )
$display_name = get_the_author_meta( 'nickname', $post->post_author );
 
// Get author's biographical information or description
$user_description = get_the_author_meta( 'user_description', $post->post_author );
 
// Get author's website URL 
$user_website = get_the_author_meta('url', $post->post_author);
 
// Get link to the author archive page
$user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
  
if ( ! empty( $display_name ) )
 
$author_details = '<p class="author_name">About ' . $display_name . '</p>';
 
if ( ! empty( $user_description ) )
// Author avatar and bio
 
$author_details .= '<p class="author_details">' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '</p>';
 
$author_details .= '<p class="author_links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>';  
 
// Check if author has a website in their profile
if ( ! empty( $user_website ) ) {
 
// Display author website link
$author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">Website</a></p>';
 
} else { 
// if there is no author website then just close the paragraph
$author_details .= '</p>';
}
 
// Pass all this info to post content  
$content = $content . '<footer class="author_bio_section" >' . $author_details . '</footer>';
}
return $content;
}
 
// Add our function to the post content filter 
add_action( 'the_content', 'wpb_author_info_box' );
 
// Allow HTML in author bio section 
remove_filter('pre_user_description', 'wp_filter_kses');

This code simply fetches the author information and displays it below WordPress posts. You need to style this author info box so that it looks nice and matches your WordPress theme.

You can add the following custom CSS to style your author box. Feel free to modify it to meet your needs

.author_bio_section{
background-color: #F5F5F5;
padding: 15px;
border: 1px solid #ccc;
}
 
.author_name{
font-size:16px;
font-weight: bold;
}
 
.author_details img {
border: 1px solid #D8D8D8;
border-radius: 50%;
float: left;
margin: 0 10px 10px 0;
}

This is how the author info box looked on our demo site.

Custom author info box

We hope this article helped you learn how to add an author info box to WordPress posts. You may also want to see our tips on how to increase your blog traffic, or our step by step guide on how to create an email newsletter.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post How to Add an Author Info Box in WordPress Posts appeared first on WPBeginner.

how can improve ecommerce website ranking?

Hello evryone currenlty i have wordpress site with woo commerce plugin. I run online shopping site i don't know how can i improve traffic more. now i have small amount of traffic how can increase high traffic. suggest me good information.

Free Vs. Paid WordPress Themes: Which Option Should You Go For?

One of the most crucial decisions you probably have to make before launching a new website is which theme you should go for. Though there are countless fantastic WordPress themes, the real confusion that builds up here is whether to choose a free or paid theme. Premium or paid WordPress themes tend to lure visitors […]

The post Free Vs. Paid WordPress Themes: Which Option Should You Go For? appeared first on WPArena.

Things to look at if you have children with asthma or allergies

Asthma is becoming a significant issue amongst our youth. There are many statistics that reflect the fact that children are much more likely to be asthma sufferers than adults. Just check the Asthma and Allergy Foundation of America’s fact sheet. To summarise: Asthma is more common in children than adults Asthma is the leading chronic […]

The post Things to look at if you have children with asthma or allergies appeared first on WPArena.

Top 7 APIs for Beer

The global beer industry is worth an estimated 500 billion dollars, so it's no wonder developers like to create applications that "tap" into data about beer.

PDF search or bookmark parameter to a URL by Today()’s date

I'm trying to setup the links on this page: http://limitlesshoops.com/levels.html to lead to specific pages within this PDF: https://fiftyallstars.com/a2z.pdf --- I've found I can lead people to whatever page like this: https://fiftyallstars.com/a2z.pdf#page=7 but I'd like to lead them to a page based on Today's date. I've added a few example pages to the end of the PDF that have 12/01/19 etc. in the header section. I figure if the browser's "find" functionality can scroll to that page, then there should be some javascript that can do the same.

Any help would be awesome. Thanks!

Here are some helpful links I've found so far:

Appending the URL link with today's date: https://stackoverflow.com/questions/27728219/how-to-insert-todays-date-into-a-url

Leading to a specific destination within the PDF: https://helpx.adobe.com/acrobat/kb/link-html-pdf-page-acrobat.html ---

Setting markers within the document that have the date instead: https://stackoverflow.com/questions/125632/is-it-possible-to-link-to-a-bookmark-within-a-pdf-using-url-parameters