Create a custom archive page for a custom post type in a plugin

What you need is hooking template_include filter and selectively load your template inside plugin. As a good practice, if you plan to distribute your plugin, you should check if archive-my_plugin_lesson.php (or maybe myplugin/archive-lesson.php) exists in theme, if not use the plugin version. In this way is easy for users replace the template via theme (or … Read more

How to get featured image’s width and use elsewhere in template?

Try the following. First, add this piece of code to the template: <?php $image_data = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), “thumbnail” ); ?> $image_data will now return an array containing the url, width, and height (function reference). To get the width, you might do this: <?php $image_width = $image_data[1]; ?> In your specific example, after adding … Read more

Get the current page URL (including pagination)

In addition to Rajeev Vyas’s answer, you don’t need to pass any non-empty parameters to add_query_arg(). The following has always worked well for me: // relative current URI: $current_rel_uri = add_query_arg( NULL, NULL ); // absolute current URI (on single site): $current_uri = home_url( add_query_arg( NULL, NULL ) ); The function falls back on $_SERVER[ … Read more

Preserving theme settings in child theme

Because of the way these theme settings are stored as an array in the database, it can be difficult to copy them over with just copy and paste in phpmyadmin or some similar tactic. The WP CLI option command is your friend here. If you don’t use WP CLI already, check it out! Here’s how … Read more

Can I upgrade a plugin to a specific version?

Using WP-CLI you can specify this as described in the official documentation. $ wp plugin update <plugin> Using either of the following arguments –minor Only perform updates for minor releases (e.g. from 1.3 to 1.4 instead of 2.0) –patch Only perform updates for patch releases (e.g. from 1.3 to 1.3.3 instead of 1.4) –version=<version> If … Read more

Best way to abort plugin in case of insufficient PHP version?

This function and activation hook prevents the plugin from activating and allows you to check for both a minimum PHP and WordPress version. register_activation_hook( __FILE__, array( ‘Your_Plugin_Class_Name’, ‘activate’ ) ); /** * Plugin Activation hook function to check for Minimum PHP and WordPress versions * @param string $wp Minimum version of WordPress required for this … Read more