WordPress Development Best Practices

0

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 >>

0

Plugin review: Site Kit by Google

Site kit by Google

0

Google released its Site Kit plugin almost half a year ago (October 31, 2019). In this review, I would like to present Site Kit’s main features and show you why you definitely need to have this plugin running on your site as well.

Effortless integration to Google services

One-click integration with most popular Google services

Before Site Kit was released, connecting your site to Google services was a complicated process. Often you needed to install several plugins, spend time with configuring them and after all of this, you still didn’t get the value you expected. No need for that anymore. With Site Kit 1.4.0 you can smoothly integrate your site to:

With just a few clicks you are able to set up your new Google Analytics account and connect it to your site. Already have a Tag Manager? No need to hassle either, just press connect and after allowing Google to access your data, you are done.

Most important stats at one place

Google Site Kit dashboard widget

Every time you login to your site administration, you are presented with daily updated and most important statistics. Based on your integrated services, you can view in the Site Kit’s dashboard following stats:

To access more in-depth statistics you can visit respected Google service through the link in the Site Kit dashboard.

No real-time analytics

While Site Kit can show you the most important stats, it is missing real-time analytics. For that, you still need to access Google Analytics.

Some users also reported Site Kit as buggy. I have experienced some errors as well, when I installed it on a site that already had other plugin, that was using Google charts library for example.

Conclusion

While WordPress users gave Site Kit average rating of 3.5 stars. I think it is definitely worth more. Plugin is well supported and almost every two weeks receives an update with bug-fixes and functionality enhancements.

Thanks to its ease of use and great value for the little effort, I ‘ll make sure that Site Kit is installed on every project I work on.

0

How to setup WordPress error log

Wordpress error log

0

By adding a few lines of code into the wp-confing file, you can enable WordPress log and see what is going on in the background.

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true ); 
define('WP_DEBUG_DISPLAY', false);

debug.log file will be then created in the WP-Content directory after there is an error or warning generated on your page.

Change debug.log file location

By default, it is set by the wp-includes/load.php to be stored inside WP-Content directory:

ini_set( 'error_log', $log_path );

You can easily overwrite it by using the same approach:

ini_set( 'error_log', WP_CONTENT_DIR . 'YOUR_DIRECTORY/YOUR_FILE_NAME' );

Write custom message to WordPress log

By calling the error_log() function, you can write to the debug.log file any message you want. You can also put this snippet into functions.php, that will make it easier for you to handle arrays:

/*
*
* Write Log Debugging
*
*/

if ( ! function_exists('write_log')) {
	function write_log ( $log )  {
	   if ( is_array( $log ) || is_object( $log ) ) {
		  error_log( print_r( $log, true ) );
	   } else {
		  error_log( $log );
	   }
	}
 }

 write_log('Debug log test');

WordPress error log still not working

After making sure that wp-config file is set, you still see no debug.log file. Of course, problem could be with one of the installed plugins and you can try deactivating all of them. However, far more probable cause is your php.ini settings.

There is a variable called error_log. Usually it is empty, but it could be your case that it has value like ” /home/html/xxx.xom/logs/php.log ” set, which causes that error_log() function sends all the messages directly there.

Therefore check your php.ini by simply calling php_info() or go to your web administration panel and check if you can see this setting here as well.

Solution is then to either change it yourself (if possible) or ask your webhosting support.

0

Maco

1+

Our adopted & beloved dog Maco.

Maco, Dolný Kubín, Október 2018

Maco, Dolný Kubín, October 2018

1+

Google add to calendar button bug

Add to Google Calendar bug

0

Imagine situation when you are hosting an event or you have a website that regularly organizes events. You create a page, add time and place of event and then you decide to add a Google Calendar button to make things look fancy.

So you google it and you find a script like this:

<a href="http://www.google.com/calendar/event?action=TEMPLATE&text=Example%20Event
&dates=20131124T010000Z/20131124T020000Z
&details=Event%20Details%20Here
&location=123%20Main%20St%2C%20Example%2C%20NY">Add to gCal</a>

Looks simple, you just need to edit some url parameters, most importantly dates parameter.

You type in your event data and here comes trouble.

Google Calendar adds couple of hours to your specified time. Why is that?

To be short, this is the reason:

Did you notice that “Z” at the end? That is the reason why your Google Calendar is adding some time to the official event time.

Lesson learned

0