Advertisement
  1. Code
  2. WordPress
  3. Plugin Development

How to Create a WordPress Avatar Management Plugin from Scratch: Getting Started

Scroll to top
Read Time: 21 min
This post is part of a series called How to Create a WordPress Avatar Management Plugin from Scratch.
How to Create a WordPress Avatar Management Plugin: Finishing Touches

Avatar Manager for WordPress is a sweet and simple plugin for storing avatars locally and more. Easily.

Enhance your WordPress website by letting your users choose between using Gravatar or a self-hosted avatar image right from their profile screen. Improved workflow, on-demand image generation and custom user permissions under a native interface. Say hello to the Avatar Manager plugin.


Introduction

A WordPress plugin is a PHP application that adds a specific set of features or services to WordPress, which can be seamlessly integrated with WordPress using access points and methods provided by the WordPress Plugin API.

This article will guide you through the process of creating your own WordPress plugin from scratch.

Note: This article assumes that you are already familiar with the basic functionality of WordPress, and PHP programming.


Step 1. Setting Up the Workspace

To get started navigate to wp-content/plugins/ under your WordPress install. In order to set up the workspace start by creating the following structure of directories and empty files as exemplified in the image below:

Workspace structure of the Avatar Manager plugin
Workspace structure for the Avatar Manager plugin

Make sure to pick a unique name for the plugin directory and for the main PHP file, such as avatar-manager and avatar-manager.php in this example, and put all the plugin's files into that directory.

Silence Is Golden

Before starting to write our plugin, open avatar-manager/index.php and add the following code:

1
2
<?php
3
// Silence is golden.

4
?>

You can see this file in many places of WordPress. It's a simple trick used to prevent directory browsing.


Step 2. Writing a Basic WordPress Plugin

Now, it's time to put some information into our main plugin PHP file.

Standard Plugin Information

The top of the plugin's main PHP file must contain a standard plugin information header. This header lets WordPress recognize that the plugin exists, add it to the plugin management screen so it can be activated, load it, and run its functions; without the header, the plugin will never be activated and will never run.

Open avatar-manager/avatar-manager.php and add the following lines:

1
2
<?php
3
/**

4
 * @package Avatar_Manager

5
 */
6
/*

7
Plugin Name: Avatar Manager

8
Plugin URI: http://wordpress.org/extend/plugins/avatar-manager/

9
Description: Avatar Manager for WordPress is a sweet and simple plugin for storing avatars locally and more. Easily.

10
Version: 1.0.0

11
Author: Cătălin Dogaru

12
Author URI: http://profiles.wordpress.org/cdog/

13
License: GPLv2 or later

14
*/
15
?>

The minimum information WordPress needs to recognize our plugin is the Plugin Name line. The rest of the information (if present) will be used to create the table of plugins on the plugin management screen. The order of the lines is not important.

So that the upgrade mechanism can correctly read the version of our plugin it is recommended to pick a format for the version number and stick to it between the different releases.

The License slug should be a short common identifier for the license the plugin is under and is meant to be a simple way of being explicit about the license of the code.

Versioning

For transparency and insight into our release cycle, and for striving to maintain backward compatibility, Avatar Manager will be maintained under the Semantic Versioning guidelines as much as possible.

Releases will be numbered with the following format:

<major>.<minor>.<patch>

And constructed with the following guidelines:

  • Breaking backward compatibility bumps the major (and resets the minor and patch).
  • New additions without breaking backward compatibility bumps the minor (and resets the patch).
  • Bug fixes and misc changes bumps the patch.

For more information on SemVer, please visit semver.org.

License

It is customary to follow the standard header with information about licensing for the plugin. Most plugins use the same license used by WordPress, which is the GPLv2 license or a license compatible with the GPLv2. To indicate a GPLv2 license, include the following lines in our plugin:

1
2
/*

3
Copyright © 2013 Cătălin Dogaru

4


5
This program is free software; you can redistribute it and/or modify it under

6
the terms of the GNU General Public License as published by the Free Software

7
Foundation; either version 2 of the License, or (at your option) any later

8
version.

9


10
This program is distributed in the hope that it will be useful, but WITHOUT ANY

11
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A

12
PARTICULAR PURPOSE. See the GNU General Public License for more details.

13


14
You should have received a copy of the GNU General Public License along with

15
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin

16
Street, Fifth Floor, Boston, MA 02110-1301, USA.

17
*/

Next, open avatar-manager/LICENSE and paste the plain text version of GPLv2 to it.


Step 3. Programming the Avatar Manager Plugin

After completing the previous step you should be able to locate the Avatar Manager plugin under the Plugins Screen.

The Avatar Manager plugin under Plugins ScreenThe Avatar Manager plugin under Plugins ScreenThe Avatar Manager plugin under Plugins Screen
The Avatar Manager plugin under Plugins Screen

Now, it's time to make our plugin actually do something. Activate it and add the following lines of code to the main plugin PHP file:

1
2
define( 'AVATAR_MANAGER_VERSION', '1.0.0' );
3
define( 'AVATAR_MANAGER_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
4
define( 'AVATAR_MANAGER_AVATAR_UPLOADS', 0 );
5
define( 'AVATAR_MANAGER_DEFAULT_SIZE', 96 );

The define() function defines a named constant at runtime. The plugin_dir_url() function gets the URL (with trailing slash) for the plugin __FILE__ passed in. The value of __FILE__ is the full path and filename of the current file and it's one of the eight magical constants that PHP provides.

Let's go further and initialize our plugin:

1
2
/**

3
 * Sets up plugin defaults and makes Avatar Manager available for translation.

4
 *

5
 * @uses load_theme_textdomain() For translation/localization support.

6
 * @uses plugin_basename() For retrieving the basename of the plugin.

7
 *

8
 * @since Avatar Manager 1.0.0

9
 */
10
function avatar_manager_init() {
11
	// Makes Avatar Manager available for translation.

12
	load_plugin_textdomain( 'avatar-manager', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
13
}
14
15
add_action( 'init', 'avatar_manager_init' );

The add_action() call hooks a function on to a specific action. The init action runs after WordPress has finished loading but before any headers are sent. Also the load_plugin_textdomain() call should be made during init, otherwise users can't hook into it. But more about this later, when I'll cover the internationalization of our plugin. The dirname() function returns the parent directory's path, while plugin_basename() function gets the basename of the plugin.

Hooks, Actions and Filters

Hooks are provided by WordPress to allow a plugin to hook into the rest of WordPress; that is, to call functions in the plugin at specific times, and thereby set the plugin in motion. There are two types of hooks:

  • Actions – Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur.
  • Filters – Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen.

Step 4. Adding Plugin Options

Next, we're going to add the plugin options. Allowing for customization makes a plugin far more flexible for the user.

1
2
/**

3
 * Registers sanitization callback and plugin setting fields.

4
 *

5
 * @uses register_setting() For registering a setting and its sanitization

6
 * callback.

7
 * @uses add_settings_field() For registering a settings field to a settings

8
 * page and section.

9
 * @uses __() For retrieving the translated string from the translate().

10
 *

11
 * @since Avatar Manager 1.0.0

12
 */
13
function avatar_manager_admin_init() {
14
	// Registers plugin setting and its sanitization callback.

15
	register_setting( 'discussion', 'avatar_manager', 'avatar_manager_sanitize_options' );
16
17
	// Registers Avatar Uploads settings field under the Settings Discussion

18
	// Screen.

19
	add_settings_field( 'avatar-manager-avatar_uploads', __( 'Avatar Uploads', 'avatar-manager' ), 'avatar_manager_avatar_uploads_settings_field', 'discussion', 'avatars' );
20
21
	// Registers Default Size settings field under the Settings Discussion

22
	// Screen.

23
	add_settings_field( 'avatar-manager-default-size', __( 'Default Size', 'avatar-manager' ), 'avatar_manager_default_size_settings_field', 'discussion', 'avatars' );
24
}
25
26
add_action( 'admin_init', 'avatar_manager_admin_init' );

The admin_init action is triggered before any other hook when a user accesses the admin area. The register_setting() function registers a setting and its sanitization callback. The add_settings_field() function registers a settings field to a settings page and section. We used them to add our plugin options under the Settings Discussion Screen. The __() function will be explained later, when I'll cover the internationalization process.

Step 5. Adding the Sanitization Callback

Before writing the sanitization callback, we need to define two more functions, avatar_manager_get_default_options() and avatar_manager_get_options().

1
2
/**

3
 * Returns plugin default options.

4
 *

5
 * @since Avatar Manager 1.0.0

6
 *

7
 * @return array Plugin default options.

8
 */
9
function avatar_manager_get_default_options() {
10
	$options = array(
11
		'avatar_uploads' => AVATAR_MANAGER_AVATAR_UPLOADS,
12
		'default_size'   => AVATAR_MANAGER_DEFAULT_SIZE
13
	);
14
15
	return $options;
16
}

The avatar_manager_get_default_options() function returns plugin default options.

1
2
/**

3
 * Returns plugin options.

4
 *

5
 * @uses get_option() For getting values for a named option.

6
 * @uses avatar_manager_get_default_options() For retrieving plugin default

7
 * options.

8
 *

9
 * @since Avatar Manager 1.0.0

10
 *

11
 * @return array Plugin options.

12
 */
13
function avatar_manager_get_options() {
14
	return get_option( 'avatar_manager', avatar_manager_get_default_options() );
15
}

The avatar_manager_get_options() function retrieves current plugin options. The get_otpion() function returns the value of the specified option or the default value if the option isn't in the database.

1
2
/**

3
 * Sanitizes and validates plugin options.

4
 *

5
 * @uses avatar_manager_get_default_options() For retrieving plugin default

6
 * options.

7
 * @uses absint() For converting a value to a non-negative integer.

8
 *

9
 * @since Avatar Manager 1.0.0

10
 *

11
 * @return array Sanitized plugin options.

12
 */
13
function avatar_manager_sanitize_options( $input ) {
14
	$options = avatar_manager_get_default_options();
15
16
	if ( isset( $input['avatar_uploads'] ) && trim( $input['avatar_uploads'] ) )
17
		$options['avatar_uploads'] = trim( $input['avatar_uploads'] ) ? 1 : 0;
18
19
	if ( isset( $input['default_size'] ) && is_numeric( trim( $input['default_size'] ) ) ) {
20
		$options['default_size'] = absint( trim( $input['default_size'] ) );
21
22
		if ( $options['default_size'] < 1 )
23
			$options['default_size'] = 1;
24
		elseif ( $options['default_size'] > 512 )
25
			$options['default_size'] = 512;
26
	}
27
28
	return $options;
29
}

The avatar_manager_sanitize_options() function sanitizes and validates plugin options. The isset() call determines if a variable is set and not NULL. The trim() function strips whitespaces from the beginning and end of a string. The is_numeric() function finds whether a variable is a number or a numeric string. The absint() function converts a value to a non-negative integer.


Step 6. Adding the Setting Fields

Now, it's time to add the setting fields.

1
2
/**

3
 * Prints Avatar Uploads settings field.

4
 *

5
 * @uses avatar_manager_get_options() For retrieving plugin options.

6
 * @uses _e() For displaying the translated string from the translate().

7
 * @uses checked() For comparing two given values.

8
 *

9
 * @since Avatar Manager 1.0.0

10
 */
11
function avatar_manager_avatar_uploads_settings_field() {
12
	// Retrieves plugin options.

13
	$options = avatar_manager_get_options();
14
	?>
15
	<fieldset>
16
		<legend class="screen-reader-text">
17
			<span>
18
				<?php _e( 'Avatar Uploads', 'avatar-manager' ); ?>
19
			</span>
20
		</legend><!-- .screen-reader-text -->
21
		<label>
22
			<input <?php checked( $options['avatar_uploads'], 1, true ); ?> name="avatar_manager[avatar_uploads]" type="checkbox" value="1">
23
			<?php _e( 'Anyone can upload', 'avatar-manager' ); ?>
24
		</label>
25
	</fieldset>
26
	<?php
27
}

The avatar_manager_avatar_uploads_settings_field() callback prints Avatar Uploads settings field. The checked() function compares two given values and, if the values are the same, adds the checked attribute to the current checkbox. The _e() function will be described later, when I'll explain the internationalization process.

1
2
/**

3
 * Prints Default Size settings field.

4
 *

5
 * @uses avatar_manager_get_options() For retrieving plugin options.

6
 * @uses _e() For displaying the translated string from the translate().

7
 *

8
 * @since Avatar Manager 1.0.0

9
 */
10
function avatar_manager_default_size_settings_field() {
11
	// Retrieves plugin options.

12
	$options = avatar_manager_get_options();
13
	?>
14
	<fieldset>
15
		<legend class="screen-reader-text">
16
			<span>
17
				<?php _e( 'Default Size', 'avatar-manager' ); ?>
18
			</span>
19
		</legend><!-- .screen-reader-text -->
20
		<label>
21
			<?php _e( 'Default size of the avatar image', 'avatar-manager' ); ?>
22
			<input class="small-text" min="1" name="avatar_manager[default_size]" step="1" type="number" value="<?php echo $options['default_size']; ?>">
23
		</label>
24
	</fieldset>
25
	<?php
26
}

The avatar_manager_default_size_settings_field() callback prints Default Size settings field.

After adding the setting fields you should be able to locate the plugin options under the Settings Discussion Screen.

The Avatar Manager plugin options under the Settings Discussion ScreenThe Avatar Manager plugin options under the Settings Discussion ScreenThe Avatar Manager plugin options under the Settings Discussion Screen
The Avatar Manager plugin options under the Settings Discussion Screen

The first option controls whether low privileged users can upload an avatar image or not, while the second option represents the default size for an avatar image.


Step 7. Adding the Avatar Section

To allow users manage their avatar, we need to add a new section to their profile page. Let's go furher and add the Avatar section under the User Your Profile Screen:

1
2
/**

3
 * Prints Avatar section.

4
 *

5
 * @uses avatar_manager_get_options() For retrieving plugin options.

6
 * @uses get_post_meta() For retrieving attachment meta fields.

7
 * @uses remove_filter() For removing a function attached to a specified action

8
 * hook.

9
 * @uses _e() For displaying the translated string from the translate().

10
 * @uses checked() For comparing two given values.

11
 * @uses get_avatar() For retrieving the avatar for a user.

12
 * @uses esc_attr() For escaping HTML attributes.

13
 * @uses add_query_arg() For retrieving a modified URL (with) query string.

14
 * @uses self_admin_url() For retrieving an admin url link with optional path

15
 * appended.

16
 * @uses current_user_can() For checking whether the current user has a certain

17
 * capability.

18
 * @uses submit_button() For echoing a submit button, with provided text and

19
 * appropriate class.

20
 * @uses __() For retrieving the translated string from the translate().

21
 *

22
 * @since Avatar Manager 1.0.0

23
 *

24
 * @param array $profileuser User to edit.

25
 */
26
function avatar_manager_edit_user_profile( $profileuser ) {
27
	// Retrieves plugin options.

28
	$options = avatar_manager_get_options();
29
30
	$avatar_type = isset( $profileuser->avatar_manager_avatar_type ) ? $profileuser->avatar_manager_avatar_type : 'gravatar';
31
32
	if ( isset( $profileuser->avatar_manager_custom_avatar ) ) {
33
		// Retrieves attachment meta fields based on attachment ID.

34
		$custom_avatar_rating   = get_post_meta( $profileuser->avatar_manager_custom_avatar, '_avatar_manager_custom_avatar_rating', true );
35
		$user_has_custom_avatar = get_post_meta( $profileuser->avatar_manager_custom_avatar, '_avatar_manager_is_custom_avatar', true );
36
	}
37
38
	if ( ! isset( $custom_avatar_rating ) || empty( $custom_avatar_rating ) )
39
		$custom_avatar_rating = 'G';
40
41
	if ( ! isset( $user_has_custom_avatar ) || empty( $user_has_custom_avatar ) )
42
		$user_has_custom_avatar = false;
43
44
	if ( $user_has_custom_avatar )
45
		// Removes the function attached to the specified action hook.

46
		remove_filter( 'get_avatar', 'avatar_manager_get_avatar' );
47
	?>
48
	<h3>
49
		<?php _e( 'Avatar', 'avatar-manager' ); ?>
50
	</h3>
51
	<table class="form-table">
52
		...
53
	</table><!-- .form-table -->
54
	<?php
55
}
56
57
add_action( 'show_user_profile', 'avatar_manager_edit_user_profile' );
58
add_action( 'edit_user_profile', 'avatar_manager_edit_user_profile' );

The show_user_profile and edit_user_profile actions help customization of the user profile page. The $profileuser parameter is a WP_User object of the user being edited. The get_post_meta() function returns the values of the custom fields with the specified key from the specified post. The empty() function determines whether a variable is empty. The remove_filter() function removes a function attached to a specified filter hook; it's required to remove our custom function for retrieving an avatar image.

Next, we're going to add an avatar picker, an upload form and a rating chooser for the custom avatar image of each user.

The Avatar Picker

The avatar picker lets a user choose between using Gravatar or a custom avatar image. To add it, write the following code:

1
2
<tr>
3
	<th>
4
		<?php _e( 'Display this avatar', 'avatar-manager' ); ?>
5
	</th>
6
	<td class="avatar-manager">
7
		<fieldset>
8
			<legend class="screen-reader-text">
9
				<span>
10
					<?php _e( 'Display this avatar', 'avatar-manager' ); ?>
11
				</span><!-- .screen-reader-text -->
12
			</legend>
13
			<label>
14
				<input <?php checked( $avatar_type, 'gravatar', true ); ?> name="avatar_manager_avatar_type" type="radio" value="gravatar">
15
				<?php echo get_avatar( $profileuser->ID, 32, '', false ); ?>
16
				<?php _e( 'Gravatar', 'avatar-manager' ); ?>
17
				<span class="description">
18
					<?php _e( '<a href="http://codex.wordpress.org/How_to_Use_Gravatars_in_WordPress" target="_blank">More information</a>', 'avatar-manager' ); ?>
19
				</span><!-- .description -->
20
			</label>
21
			<?php if ( $user_has_custom_avatar ) : ?>
22
				<br>
23
				<label>
24
					<input <?php checked( $avatar_type, 'custom', true ); ?> name="avatar_manager_avatar_type" type="radio" value="custom">
25
					<?php echo avatar_manager_get_custom_avatar( $profileuser->ID, 32, '', false ); ?>
26
					<?php _e( 'Custom', 'avatar-manager' ); ?>
27
				</label>
28
			<?php endif; ?>
29
			<?php
30
			if ( $user_has_custom_avatar && ( current_user_can( 'upload_files' ) || $options['avatar_uploads'] ) ) {
31
				$href = esc_attr( add_query_arg( array(
32
					'action'                       => 'update',
33
					'avatar_manager_action'        => 'remove-avatar',
34
					'avatar_manager_custom_avatar' => $profileuser->avatar_manager_custom_avatar,
35
					'user_id'                      => $profileuser->ID
36
				),
37
				self_admin_url( IS_PROFILE_PAGE ? 'profile.php' : 'user-edit.php' ) ) );
38
				?>
39
				<a class="delete" href="<?php echo wp_nonce_url( $href, 'update-user_' . $profileuser->ID ); ?>" onclick="return showNotice.warn();">
40
					<?php _e( 'Delete', 'avatar-manager' ); ?>
41
				</a><!-- .delete -->
42
				<?php
43
			}
44
			?>
45
		</fieldset>
46
	</td><!-- .avatar-manager -->
47
</tr>

The get_avatar() function retrieves the avatar for a user who provided a user ID or email address. To retrievie a custom avatar image by a user ID, we used the avatar_manager_get_custom_avatar() function, which we'll write later. The current_user_can() function checks whether the current user has a certain capability. Normally, low priviledged users like Subscribers aren't allowed to upload files; this is why we use the $options['avatar_uploads'] variable. The esc_attr() function escapes HTML attributes. The self_admin_url() function retrieves an admin URL link with optional path appended. The IS_PROFILE_PAGE constant tells us if we're editing our profile or another user's profile. The wp_nonce_url() function retrieves URL with nonce added to URL query. Before deleting an avatar, we ask the user to confirm by calling the showNotice.warn() method at the onclick event of the Delete link which displays a warning notice.

The Upload Form

The upload form allows a user to browse and upload a custom avatar image:

1
2
<?php if ( current_user_can( 'upload_files' ) || $options['avatar_uploads'] ) : ?>
3
	<tr>
4
		<th>
5
			<?php _e( 'Select Image', 'avatar-manager' ); ?>
6
		</th>
7
		<td>
8
			<fieldset>
9
				<legend class="screen-reader-text">
10
					<span>
11
						<?php _e( 'Select Image', 'avatar-manager' ); ?>
12
					</span>
13
				</legend><!-- .screen-reader-text -->
14
				<label class="description" for="avatar-manager-upload-avatar">
15
					<?php _e( 'Choose an image from your computer:', 'avatar-manager' ); ?>
16
				</label><!-- .description -->
17
				<br>
18
				<input name="avatar_manager_import" type="file">
19
				<?php submit_button( __( 'Upload', 'avatar-manager' ), 'button', 'avatar-manager-upload-avatar', false ); ?>
20
			</fieldset>
21
		</td>
22
	</tr>
23
<?php endif; ?>

The submit_button() function echoes a submit button, with provided text and appropriate class.

The Rating Chooser

The rating chooser shows up only when a custom avatar is available. To add it, write the following lines:

1
2
<?php if ( $user_has_custom_avatar ) : ?>
3
	<tr>
4
		<th>
5
			<?php _e( 'Avatar Rating', 'avatar-manager' ); ?>
6
		</th>
7
		<td>
8
			<fieldset>
9
				<legend class="screen-reader-text">
10
					<span>
11
						<?php _e( 'Avatar Rating', 'avatar-manager' ); ?>
12
					</span>
13
				</legend><!-- .screen-reader-text -->
14
				<?php
15
				$ratings = array(
16
					// Translators: Content suitability rating:

17
					// http://bit.ly/89QxZA

18
					'G'  => __( 'G &#8212; Suitable for all audiences', 'avatar-manager' ),
19
					// Translators: Content suitability rating:

20
					// http://bit.ly/89QxZA

21
					'PG' => __( 'PG &#8212; Possibly offensive, usually for audiences 13 and above', 'avatar-manager' ),
22
					// Translators: Content suitability rating:

23
					// http://bit.ly/89QxZA

24
					'R'  => __( 'R &#8212; Intended for adult audiences above 17', 'avatar-manager' ),
25
					// Translators: Content suitability rating:

26
					// http://bit.ly/89QxZA

27
					'X'  => __( 'X &#8212; Even more mature than above', 'avatar-manager' )
28
				);
29
30
				foreach ( $ratings as $key => $rating ) {
31
					?>
32
					<label>
33
						<input <?php checked( $custom_avatar_rating, $key, true ); ?> name="avatar_manager_custom_avatar_rating" type="radio" value="<?php echo esc_attr( $key ); ?>">
34
						<?php echo $rating; ?>
35
					</label>
36
					<br>
37
					<?php
38
				}
39
				?>
40
				<span class="description">
41
					<?php _e( 'Choose a rating for your custom avatar.', 'avatar-manager' ); ?>
42
				</span><!-- .description -->
43
			</fieldset>
44
		</td>
45
	</tr>
46
<?php endif; ?>

It lets the user choose an appropriate rating for the custom avatar image being used.


Step 8. Adding Scripts and Styles

Now that we've added the Avatar section, it's time to style it. Also, we'll write some JS to change the form encoding; this step is required because we've added a file upload control to it.

The CSS Style

To style our plugin, open avatar-manager/avatar-manager.css and write the following lines:

1
2
#profile-page .avatar-manager img {
3
	margin: 2px 0;
4
	vertical-align: middle;
5
}
6
7
#profile-page .avatar-manager .delete {
8
	color: red;
9
	padding: 2px;
10
}
11
12
#profile-page .avatar-manager .delete:hover {
13
	background: red;
14
	color: #fff;
15
	text-decoration: none;
16
}

This aligns an avatar image vertically with a radiobox and styles the Delete link accordingly for a seamless integration with WordPress' native interface.

The JS Script

Next, open avatar-manager/avatar-manager.js and add the following code:

1
2
jQuery( document ).ready( function() {
3
	jQuery( '#your-profile' ).attr( 'enctype', 'multipart/form-data' );
4
} );

The .attr() function sets the value of one or more attributes for every matched element. The enctype attribute specifies how the form-data should be encoded when submitting it to the server. We need to change its value to multipart/form-data to allow file uploads.


Step 9. Enqueuing the Scripts and Styles

The safe and recommended method of adding scripts and styles to a WordPress generated page is by using wp_enqueue_script() and wp_enqueue_style(). These functions include the scripts and styles if they haven't already been included, and safely handle dependencies.

1
2
/**

3
 * Enqueues plugin scripts and styles for Users Your Profile Screen.

4
 *

5
 * @uses wp_register_style() For registering a CSS style file.

6
 * @uses wp_enqueue_style() For enqueuing a CSS style file.

7
 * @uses wp_register_script() For registering a JS script file.

8
 * @uses wp_enqueue_script() For enqueuing a JS script file.

9
 *

10
 * @since Avatar Manager 1.0.0

11
 */
12
function avatar_manager_admin_enqueue_scripts() {
13
	global $hook_suffix;
14
15
	if ( $hook_suffix == 'profile.php' || $hook_suffix == 'user-edit.php' ) {
16
		// Registers plugin CSS style file.

17
		wp_register_style( 'avatar-manager.css', AVATAR_MANAGER_PLUGIN_URL . 'avatar-manager.css', array(), '1.0.0' );
18
19
		// Enqueues plugin CSS style file.

20
		wp_enqueue_style( 'avatar-manager.css' );
21
22
		// Registers plugin JS script file.

23
		wp_register_script( 'avatar-manager.js', AVATAR_MANAGER_PLUGIN_URL . 'avatar-manager.js', array( 'jquery' ), '1.0.0' );
24
25
		// Enqueues plugin JS script file.

26
		wp_enqueue_script( 'avatar-manager.js' );
27
	}
28
}
29
30
add_action( 'admin_enqueue_scripts', 'avatar_manager_admin_enqueue_scripts' );

The admin_enqueue_scripts action is the first action hooked into the admin scripts actions. Then, the global variable $hook_suffix is used to add our scripts and styles only on the needed pages. Before enqueuing a script or style, we need to register it first. The wp_register_style() function is a safe way to register a CSS style file for later use; the wp_enqueue_style() function is used to enqueue it. Similarly, the wp_register_script() and wp_enqueue_script() functions are used to register and enqueue our plugin JS script file.

After this step you should be able to locate the plugin options under the User Your Profile Screen.

The Avatar Manager plugin options under the User Your Profile ScreenThe Avatar Manager plugin options under the User Your Profile ScreenThe Avatar Manager plugin options under the User Your Profile Screen
The Avatar Manager plugin options under the User Your Profile Screen

The first option lets you choose between using Gravatar or a self-hosted avatar image. The second field lets you browse and upload a custom avatar image. The Avatar Rating option shows up only when a custom avatar is available. But more about this later, when we'll handle avatar uploads.


What's Next

This completes the first part of our tutorial. I hope you've enjoyed the time we've spent together and found it to be useful. In the next part we're going to finish the Avatar Manager plugin; we'll handle avatar uploads and on-demand image generation, internationalize our plugin and much more. Thanks for reading!


References

  • WordPress Coding Standards - General information about coding standards for WordPress development.
  • Writing a Plugin - Best starting place for learning about how to develop WordPress plugins.
  • Plugin API - Description of how to use action and filter hooks in your WordPress plugin, and core functions that plugins can override.
  • Settings API - A reference with examples for adding new settings to existing settings screens.
  • Function Reference - An article with many of the core WordPress functions useful to plugin and theme developers; lists most of the core functions, excluding Template Tags.
  • SemVer - Semantic Versioning (SemVer) specification.
  • GPLv2 - GNU General Public License, version 2.

External Links

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.