WordPress Development Best Practices

CURL

To fetch data from a remote source, use WP function wp_remote_get()

wp_remote_get( string $url, array $args = array() )

Performs a HTTP request using the GET method and returns its response.

Code reference >>

GPL compatible libraries

Anything stored in the plugin directory hosted on WordPress.org — must comply with the GPL or a GPL-Compatible license. Included third-party libraries, code, images, or otherwise, must be compatible.

WP Detailed Plugin Guidelines

No hard-coding

To include file in your plugin directory use WP function plugins_url().

plugins_url( string $path = '', string $plugin = '' )

Example:
plugins_url('../images/icon-512x512.png', __FILE__);

Retrieves a URL within the plugins or mu-plugins directory.

Code reference >>

Use built in functions for including CSS/JS

Do not use “<style>” or “<link>” to insert CSS/JS. Instead use WP functions for that:

wp_enqueue_script( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

Enqueue a script.

Code reference >>

wp_enqueue_style( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, string $media = 'all' )

Enqueue a CSS stylesheet.

Code reference >>

wp_add_inline_script( string $handle, string $data, string $position = 'after' )

Adds extra code to a registered script.

Code reference >>

For adding styles and scripts in admin use hook:

do_action( 'admin_enqueue_scripts', string $hook_suffix )

Code reference >>

Leave a comment

Your email address will not be published. Required fields are marked *