We'll create fresh WordPress site with Picker installed. You have 20 minutes to test the plugin after that site we'll be deleted.
Picker is a simple and flexible plugin which allow users to choose a specific post inside admin widgets page and display it in their site frontend.
If you need to display a specific post, not a generic list of last posts, top ranked posts, category posts, etc.. but only the one that you configure in the backend. Picker plugin makes it possible in a very quick and easy way. It adds a widget on the admin widgets page that you can use to select and show a post on your site’s frontend. You can work with standard types (like posts) and custom types simply by changing the settings page of the plugin.
Picker plugin is extensible in many of its features, such as, applying filters and action, managing layout template, etc.. moreover, plugin is based on the WordPress Transient API for caching issue.
As mentioned before, the plugin provides a way to override the default layout template. With a few lines of code, you can create your layout template (copying the default one) and completely override the plugin look&feel thanks to a complete access to all widget item data throw Picker classes.
You can use Picker plugin like all the other WordPress plugins, dragging the widget to a sidebar you can enable a Picker widget. Initially, the widget is not published on the frontend. In this way you can safely configure widget’s data and then publish/display the widget.
Picker widget is composed by the following fields:
For some field there is a jQuery validation to check right formats. Picker plugin checks all numbers, url and datetime fields showing you an alert if data are wrong.
Picker plugin provide a default widget template that show a linked title (using post title), the featured image of the post and the post excerpt. Alternatively, you can manage widget template overriding default template. Picker plugin has a tool to detect the template path used for widget layout inclusion.
This is the load order of the template files:
Picker plugin provides many filters to extend default behavior of the plugin core functions. This is a list of the most important available filters:
To better understand how you can interact with plugin filters, here are some examples.
If you want, you can modify the default (usually “picker/” folder), with a folder in your theme root. In the following example, adding the function to your “functions.php” theme file, we are telling to Picker plugin to search template files inside a “templates/” folder in your theme root.
function modify_picker_template_path() {
return 'templates';
}
add_filter( 'picker_template_path', 'modify_picker_template_path' );
Also, you can modify the default post title (usually “post_title” field), adding for example a prefix/suffix. In the following example, adding the function to your “functions.php” theme file, we are telling to Picker plugin to call your function before return to template the item post title value.
function modify_picker_post_title( $value ) {
return 'my_prefix ' . $value;
}
add_filter( 'picker_item_title', 'modify_picker_post_title' );
As described in the previous paragraph, you can customize widget template according to you site specs. Inside the template file you can use a $picker_item
global variable to access the picker class methods:
thumbnail
, medium
, large
, full
default sizes or your registered new image size)__get
magic method you can read a custom field (eg: $picker_item->my_custom_field)In addition to $picker_item
global variable, you can use custom variables values, such as:
In the template file are available special variables, for now:
Very important is the widget_sidebar
variable. With this value you can display a different widget layout according to which sidebar contains the widget.
To increase performance and reduce database queries Picker plugin use a persistent caching management. To get a persistent cache without using external plugins Picker plugin uses WordPress Transient API.
Anytime WordPress display Picker plugin on a site frontend page a calls Picker plugin widget management. First of all, Picker plugin look for a cached object into Transient cache, if found it, gets data, prepares HTML and provides it to frontend.
If data isn’t in Transient cache, Picker plugin go on with widget date and status validation, then, after looked up post into database, create Picker item object that contains all the widget post data, and in the end like a cached object prepares HTML and provides it to frontend.
Before concluding by Picker plugin widget management, the Picker item is saved to Transient cache. Only the Picker item is saved to cache, no HTML neither frontend logic are saved to cache. The goal to Picker caching management is only to reduce database usage.
Picker plugin cache has many expiration rules, the most important are:
Links: Author’s Site