Wordpress Widgets Flashcards Preview

Web Technologies > Wordpress Widgets > Flashcards

Flashcards in Wordpress Widgets Deck (26)
Loading flashcards...
1
Q

How do you register a widget?

A

add_action( ‘widgets_init’, function() {
register_widget( ‘Class_Name’ );
});

2
Q

What class do you need to inherit from to make a valid widget?

A

WP_Widget

3
Q

What methods do you have to implement in your class in order to make a valid widget?

A

public function __construct()
public function form( $instance )
public function update( $new_instance, $old_instance )
public function widget( $args, $instance )

4
Q

What do you need to do in the constructor of the widget class?

A

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.
)
);

5
Q

It’s best practice to first enqueue your WHAT, before the javascript?

A

The CSS files.

6
Q

How do you register CSS styles in an OOP manner?

A

Call add_action for the appropriate hooks, and pass it an array containing the context and method to call to get the stylesheet locations.

7
Q

What is the weird thing about the wp_enqueue_scripts hook?

A

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’) );
}

8
Q

What function can you use to specify the URL of the plugin directory (for writing paths to css etc)?

A

plugins_url()

i.e. “plugins_url(‘/css/admin.css)’’

Pass it the path to the file, and it will generate the full URL.

9
Q

Why would you use the function plugins_url() over plugin_dir_path()?

A

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.

10
Q

What are the hooks that we use to register all our widget scripts?

A

admin_enqueue_scripts

wp_enqueue_scripts

11
Q

What are the hooks we use to register all our widget styles?

A

wp_enqueue_scripts

admin_print_styles // This one is questionable - should probably use admin_enqueue_scripts for adding css

12
Q

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?

A

wp_parse_args

13
Q

Why should you use CSS classes when wrapping widget markup, as opposed to ID’s?

A

Because a user can have multiple instances of a widget on the same page, and ID’s have to be unique.

14
Q

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

A

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' => ''
            )
        );
15
Q

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' => ''
            )
        );
A

You can reference them directly.

$instance[‘name’]

16
Q

What is the purpose of the form method in the widget class?

A

It is used to display the markup for the form in the admin area (i.e. all the inputs / labels etc).

17
Q

What is the purpose of the update method in the widget class?

A

It is responsible for serializing the data that the user has entered into the form in the admin area.

18
Q

The update method takes two parameters, what are they?

A

$newinstance, and $oldinstance.

$newinstance contains the data the user just entered, the $oldinstance parameter is the current data.

You should return “$oldinstance” updated for the new data.

19
Q

Why might you use _e() instead of __() in your templates?

A

It makes it simpler to output text, _e echoes out the contents, whereas __() requires you to echo it out. Therefore _e is just simpler, although, otherwise identical.

20
Q

What functions are useful for data sanitization of incoming data to the database?

A

stripslashes()

strip_tags()

21
Q

What function is useful for sanitization of output data from the database?

A

esc_attr();

22
Q

When you output data from the database into an existing input box (i.e. prefilling a name field) - you use esc_attr() to santize the text. What do you do to sanitize a textarea element?

A

esc_textarea()

23
Q

When you are writing ajax code for your widgets admin area - sometimes fields or text are reset when hitting save, or adding the widget for the first time, why is this?

A

There is an AJAX event that is firing on Ajax Success (which is triggered when you add a new widget or hit save). The secret is to update all the text fields when that event fires - and you can do that by hooking into it’s event.

24
Q

When dealing with select boxes, how do you retrieve the current selection so that it doesn’t refresh between loads?

A

Use the wordpress function selected. You would do this inside the “option” tag. The output will be the attribute name and it’s current value, so no need to put

25
Q

You may use the “extract” function in order to get data from an arguments array - what parameter should you pass it to make sure you don’t overwrite existing variables?

A

extract($args, EXTR_SKIP);

26
Q

What function can be used to get the “checked” status of a check box for displaying in HTML?

A

checked (1, $instance[‘nameoffield’], true);