Wordpress Widgets Flashcards
How do you register a widget?
add_action( ‘widgets_init’, function() {
register_widget( ‘Class_Name’ );
});
What class do you need to inherit from to make a valid widget?
WP_Widget
What methods do you have to implement in your class in order to make a valid widget?
public function __construct()
public function form( $instance )
public function update( $new_instance, $old_instance )
public function widget( $args, $instance )
What do you need to do in the constructor of the widget class?
You have to make a call to the parent concstructor.
parent::__construct(
‘my_widget_id’, // A string id, you make it up here
‘My Widget Name’, // A human readable name
array (
‘classname’ => ‘my_widget_class’, // css class
‘description’ => ‘a long string’ // Just a desc.
)
);
It’s best practice to first enqueue your WHAT, before the javascript?
The CSS files.
How do you register CSS styles in an OOP manner?
Call add_action for the appropriate hooks, and pass it an array containing the context and method to call to get the stylesheet locations.
What is the weird thing about the wp_enqueue_scripts hook?
It’s used for both scripts and styles. It’s a hook that allows you to enqueue things that are meant to go in the header (useful for OOP).
You would typically use add action to register the hook, and when the hook is fired, it calls the method you registered, and in that method, you enqueue the required files.
add_action(‘wp_enqueue_scripts’, array($this, ‘method_name’));
public function method_name() {
wp_enqueue_style(‘front-to-back-admin’, plugins_url( ‘front-to-back/css/admin.css’) );
}
What function can you use to specify the URL of the plugin directory (for writing paths to css etc)?
plugins_url()
i.e. “plugins_url(‘/css/admin.css)’’
Pass it the path to the file, and it will generate the full URL.
Why would you use the function plugins_url() over plugin_dir_path()?
plugins_url provides the URL which is important when doing things like enqueueing scripts. If you provided the file system path (plugin_dir_path()), it would not load in the HTML.
What are the hooks that we use to register all our widget scripts?
admin_enqueue_scripts
wp_enqueue_scripts
What are the hooks we use to register all our widget styles?
wp_enqueue_scripts
admin_print_styles // This one is questionable - should probably use admin_enqueue_scripts for adding css
As part of the interface for WP_Widget, the function “form” takes a parameter called $instance. This is a URL string - what WP function can we use to parse it?
wp_parse_args
Why should you use CSS classes when wrapping widget markup, as opposed to ID’s?
Because a user can have multiple instances of a widget on the same page, and ID’s have to be unique.
When creating views, you may have to specify an id / name for an input element. How do you get around the fact the widget may be placed several times within the page, therefore using the same ID more than once (a violation of the HTML standard)?
You use the functions
$this->get_field_id(‘name_of_id’);
This uses the context of calling class (i.e. inside your “form” method, you might “include” an external view/php file. In that file is the HTML markup for the form. Inside that markup, you call $this->get_field_id(‘name_of_id’).
The name_of_id is defined as an argument in the “form” method.
$instance = wp_parse_args( (array)$instance, array( 'name' => '' ) );
If we have defined the “name” variable in the “form” method of the plugin class, how do we reference the values of the input fields?
$instance = wp_parse_args( (array)$instance, array( 'name' => '' ) );
You can reference them directly.
$instance[‘name’]