Wordpress Plugins Flashcards

1
Q

All plugins belong in..

A

wp-content/plugins

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is required for Wordpress to be able to ‘see’ your plugin?

A

Meta data in the main PHP file.

Simply add comments with the following fields:

Plugin Name:
Plugin URI:
Description:
Version:
Author:
Author URI:
License:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the two kinds of hooks?

A

Filters / Actions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are actions related to?

A

Wordpress core events

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Filters used for?

A

The ability to modify posts / variables etc before they are rendered to the screen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What class is used to run a query in a plugin?

A

WP_Query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What parameter to WP_Query would ensure the current post is not included as part of the query (i.e. if you wanted to get a list of related posts, but you wanted to exclude the current post from that list)?

A

‘post_not_in’ => get_the_ID()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When you run $loop->the_post(), what ‘type’ of functions are available to access the data for the current post?

A

template tags. These may have been available already, but by calling the_post, you are updating the values for the current post being processed. This is useful in a loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If you are able to see a post you have created through the edit screen in the admin edit area, but you can’t access it from the front page. What is the likely problem?

A

The .htaccess file has not been written correctly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference in the way you access the posts, between running a query (WP_Query) and just being on the index page?

A

WHen you run a specific query (i.e. $query = WP_Query()), you will access the posts via the query, i.e. $query->the_post().

If you are just on a page and are iterating through available posts, i.e. while (have_posts()) : the_post(), you can simply use the global versions of these functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When creating a shortcode, which action do you hook into?

A

‘init’

add_shortcode(‘sc_name’, ‘shortcodefunction’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

There are two parameters that are passed to a shortcode function, what are they?

A

$args (a list of arguments passed into the shortcode)

$content (the content that can be passed to the shortcode)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you write the shortcode in a post?

A

Add this into your editor (it doesn’t have to be in the text pane).

[sc_name param1=’foobar’]My Content[/sc_name]

Where sc_name is the name registered with add_shortcode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why might you want to use wp_remote_get rather than cURL to obtain data from the web?

A

Because there is such a wide variety of installations of PHP around the world (with 30% of the internet on WP) - you may find external libraries are not always installed (like cURL).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What action/hook would you use to add a metabox to a post?

A

add_action(‘add_meta_boxes’, ‘functionname’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What function do you use to get the data from a metabox?

A

get_custom_post ($post->ID);

This will return an array that has indexes corresponding to the ID’s in the form’s HTML.

NOTE: You probably need to import the $post variable from the global scope before using it.

global $post;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What action do you use to save data?

A

add_action(‘save_post’, ‘functionhandler’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Should you use “autosave” when filling in meta data fields?

A

No. You can prevent this by doing the following check:

if (defined( ‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) {
return;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What must you check before committing a save for metadata on a post?

A

You must check that the user has permission to save a post. You can do that by using the following code:

if ( !current_user_can( ‘edit_posts’) ) {
return;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

If you find you are not getting the data in the $_POST variable in the ‘save_post’ action, what is one thing you can check for?

A

Make sure the input has both an ID and a NAME attribute. Remember, PHP works with NAME attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What method is used to actually save the post meta data?

A

update_post_meta( $post_id, ‘cain_youtube’, esc_url( $_POST[ ‘cain_youtube’ ] ) );

$post_id comes as a parameter from the function (the one that is hooked to the save_post action).

NOTE: In the documentation for this function, it says some of this data should be raw instead of sanatised for database queries. Look into that.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What filter would you use to modify the contents of the titles on a post?

A

add_filter(‘the_title’, ‘myfunc’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What filter would you use to modify the contents of the content on a post?

A

add_filter(‘the_content’, ‘myfunc’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What filter would you use to modify the list of categories?

A

add_filter(‘list_cats’, ‘myfunc’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

When you are writing a widget, it’s important to remember that it’s not working in isolation. Someone else may write a plugin that puts all the widget titles in uppercase for instance. How would you make sure your widget / plugin supports that?

A

You can call the function:

apply_filters( ‘widget_titles’, $instance[ ‘title’ ] );

‘widget_titles’ refers to the filter_hook, so this is how you specify which filters are relevant (obviously, if it’s a widget title, you should only use the widget_titles hook).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How do you add a new menu to the “settings” tab in wordpress admin?

A

add_action( ‘admin_menu’, ‘cain_plugin_menu’ );

function cain_plugin_menu() {
add_options_page( ‘Zenva Wishlist Options’,
‘Zenva Wishlist’,
‘manage_options’,
‘cainmenu’,
‘cain_plugin_options’ );
}

NOTE: manage_options is a flag to describe the priv. level required to make changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

In order for settings fields to show up, you have to do what first?

A

Register the settings.

add_action( 'admin_init', 'cain_admin_init' );
function cain_admin_init()
{
    register_setting( 'cain-group', 'cain_dashboard_title' );
    register_setting( 'cain-group', 'cain_number_of_items' );
}

NOTE: This does not actually show the form, it simply allows for the forms to be shown. The first parameter to register_setting is a “group” of settings (an internal thing).

28
Q

In your admin screen, what should you take into account when laying out the form?

A

You should use the same classes / layout as wordpress, so that styling is consistent, even when they update versions.

29
Q

What function prints a submit button for a form in PHP?

A

submit_button();

30
Q

In the settings API, where is the data stored in the database?

A

It is stored in a table called: $prefix_options, where $prefix is the value set in wp-config.php in the variable $table_prefix.

31
Q

What function is responsible for embedding a nonce value (and other meta data for the admin page forms) on the admin pages?

A

settings_fields( ‘page-slug’ );

This function will output a number of values that are used by the form for security etc.

32
Q

When using AJAX with plugins, what is the purpose of using the wp_localize_script with AJAX?

A

The wp_localize_script function is used to inject server side data to the client side, which can then be used, or passed back to the server.

33
Q

When using AJAX with plugins, why do we potentially need to add ‘two’ actions for the server side processing function?

A

AJAX has a concept of privileged and non-privileged use. (i.e, are you logged in or not). You have to add one for each case (if you want to support each case).

34
Q

What is unusual about the action name that you use when declaring an AJAX script?

A

Instead of using a prebuilt action, you create your own action - you have to add the prefixes

wp_ajax_
wp_ajax_nopriv_

But remember, when you use the actual action names, you drop those prefixes.

35
Q

Once you’ve declared the action for an AJAX script, where is that used?

A

You use it on the client side when specifying the post action.

36
Q

What script processes your AJAX requests on the server side?

A

admin-ajax.php

37
Q

Rather than give jQuery.post the full url to admin-ajax.php, you can use a predefined variable provided by wordpress. What is that variable?

A

ajaxurl

38
Q

What function can you use to determine whether the user is logged in?

A

is_user_logged_in()

39
Q

How do you obtain an object containing the current user?

A

$user = wp_get_current_user();

40
Q

What function do you use to add meta data to the user?

A

add_user_meta( $user->ID, ‘name_of_field’, $data);

NOTE: name_of_field is the name of the meta data field that will be stored against the user.

41
Q

How do you retrieve the meta data stored against a user?

A

$values = get_user_meta( $user_id, ‘wanted_posts’ );

42
Q

It’s best practice to add a what? to the beginning of each meta field name?

A

Unique prefix.

43
Q

Why do you need to check if a user is logged in before trying to save meta data?

A

Because you can’t save user meta data for someone that is not logged in.

44
Q

What event do you use to create a dashboard widget?

A

wp_dashboard_setup

45
Q

Why should you use wp_get_current_user instead of get_current_user?

A

get_current_user gives you your apache user, not the logged in user.

46
Q

What function do you use to actually add the dashboard widget to the screen?

A

wp_add_dashboard_widget( ‘css_id’, $title, ‘cain_show_dashboard_widget’ );

47
Q

What is the gotcha with using wordpress cron jobs?

A

They are not actually the same as server side cron jobs. When you setup a server side cron job, it’s guaranteed to run when requested. But because wordpress/php doesn’t run all the time, it will only execute when a request (of any type) is sent to the server.

48
Q

What action would you use to create a cron job?

A

The ‘init’ action.

49
Q

In your cronjob initialisation function, what must you do before initialising the cronjob itself?

A

Make sure it hasn’t already been initialised (as we run this code path everytime the server is touched).

if ( !wp_next_scheduled( ‘cm_sendmail_hook’ ) ) {
wp_schedule_event( time(), ‘hourly’, ‘cm_sendmail_hook’ );
}

50
Q

What does the parameters of ‘wp_shedule_event’ do?

A

wp_schedule_event($firsttime, $frequency, $hook, $args)

$firsttime - must be unix time stamp, this is the first time it will run - can just pass ‘time()’ to make it run the very first time

$frequency - string specifying frequency ‘hourly’, ‘daily’, ‘twicedaily’

$hook - the function that is to be run. This function must have been registered using add_action, and given a custom hook name.

51
Q

What are the three parts of a plugins lifecycle?

A

Activation
Deactivation
Deletion/Removal

52
Q

What should you do on plugin deletion?

A

Remove all traces of the plugin - all tables, data etc. It’s a pretty final step (i.e. the user really wants this).

53
Q

It’s common practice to put the uninstall code where?

A

In an uninstall file, in the plugin directory

54
Q

What function do you call in the plugin activation part of the lifecycle, to perform actions like creating database tables?

A

register_activation_hook( __FILE__, ‘name_of_function’ );

55
Q

What function do you call in the plugin deactivation cycle to (for instance) record the fact it has been deactivated to a log file?

A

register_deactivation_hook ( __FILE__, ‘name_of_function’ );

56
Q

How do you access the WP database so you can add new tables?

A

Use the global variable.

global $wpdb;

57
Q

How do you ‘name’ a new table, such that it uses the correct prefix (remember, users can specify their own prefix)?

A

global $wpdb;

$table_name = $wpdb->prefix . ‘my_table’;

58
Q

How can you check to make sure you haven’t already created this table?

A

if ( $wpdb->get_var(“SHOW TABLES LIKE $table_name”) != $table_name) { // Do stuff }

59
Q

How do you create a table, once you have an SQL statement?

A

$sql = // sql statement - standard stuff

require_once( ABSPATH, ‘wp-admin/includes/upgrade.php’);

dbDelta( $sql );

NOTE: the WP upgrade.php file is responsible for adding new tables to the database.

60
Q

Database table creation should be done using…

A

register_activation_hook ( __FILE__, ‘my_func_name’ );

61
Q

What wp function can be used to get the current time?

A

current_time()

62
Q

What parameter can be passed to current_time to get the time in a MySQL format?

A

current_time( ‘mysql’ );

63
Q

How do you insert data into a wordpress database?

A

global $wpdb;

$tablename = $wpdb->prefix . ‘hits’;

$newdata = array(
    'hit_ip' => $ip,
    'hit_date' => current_time('mysql'),
    'hit_post_id' => $post_id
);

$wpdb->insert( $tablename, $newdata );

NOTE: The column names must match the entries in the array.

64
Q

What is one way of implementing a page counter?

A

You can check that the page is single (is_single()) and then capture the users IP address and record a page hit in the database. This will give you a total page view count. You can then use the IP address to filter for unique page views.

65
Q

How would you drop a table when your plugin is deleted?

A

if ( $wpdb->get_var( “SHOW TABLES LIKE ‘$tablename’” ) == $tablename ) {
$sql = “DROP TABLE ‘$tablename’;”;
$wpdb->query( $sql );
}

NOTE: This will delete your code too (not the sql, the act of deleting a plugin).

66
Q

What must you do before testing the delete plugin functionality?

A

Backup your code!!

it will delete it