Wordpress Plugins Flashcards
All plugins belong in..
wp-content/plugins
What is required for Wordpress to be able to ‘see’ your plugin?
Meta data in the main PHP file.
Simply add comments with the following fields:
Plugin Name: Plugin URI: Description: Version: Author: Author URI: License:
What are the two kinds of hooks?
Filters / Actions
What are actions related to?
Wordpress core events
What are Filters used for?
The ability to modify posts / variables etc before they are rendered to the screen
What class is used to run a query in a plugin?
WP_Query
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)?
‘post_not_in’ => get_the_ID()
When you run $loop->the_post(), what ‘type’ of functions are available to access the data for the current post?
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.
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?
The .htaccess file has not been written correctly.
What is the difference in the way you access the posts, between running a query (WP_Query) and just being on the index page?
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.
When creating a shortcode, which action do you hook into?
‘init’
add_shortcode(‘sc_name’, ‘shortcodefunction’);
There are two parameters that are passed to a shortcode function, what are they?
$args (a list of arguments passed into the shortcode)
$content (the content that can be passed to the shortcode)
How do you write the shortcode in a post?
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.
Why might you want to use wp_remote_get rather than cURL to obtain data from the web?
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).
What action/hook would you use to add a metabox to a post?
add_action(‘add_meta_boxes’, ‘functionname’);
What function do you use to get the data from a metabox?
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;
What action do you use to save data?
add_action(‘save_post’, ‘functionhandler’);
Should you use “autosave” when filling in meta data fields?
No. You can prevent this by doing the following check:
if (defined( ‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) {
return;
}
What must you check before committing a save for metadata on a post?
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;
}
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?
Make sure the input has both an ID and a NAME attribute. Remember, PHP works with NAME attributes.
What method is used to actually save the post meta data?
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.
What filter would you use to modify the contents of the titles on a post?
add_filter(‘the_title’, ‘myfunc’);
What filter would you use to modify the contents of the content on a post?
add_filter(‘the_content’, ‘myfunc’);
What filter would you use to modify the list of categories?
add_filter(‘list_cats’, ‘myfunc’);
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?
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 do you add a new menu to the “settings” tab in wordpress admin?
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.
In order for settings fields to show up, you have to do what first?
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).
In your admin screen, what should you take into account when laying out the form?
You should use the same classes / layout as wordpress, so that styling is consistent, even when they update versions.
What function prints a submit button for a form in PHP?
submit_button();
In the settings API, where is the data stored in the database?
It is stored in a table called: $prefix_options, where $prefix is the value set in wp-config.php in the variable $table_prefix.
What function is responsible for embedding a nonce value (and other meta data for the admin page forms) on the admin pages?
settings_fields( ‘page-slug’ );
This function will output a number of values that are used by the form for security etc.
When using AJAX with plugins, what is the purpose of using the wp_localize_script with AJAX?
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.
When using AJAX with plugins, why do we potentially need to add ‘two’ actions for the server side processing function?
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).
What is unusual about the action name that you use when declaring an AJAX script?
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.
Once you’ve declared the action for an AJAX script, where is that used?
You use it on the client side when specifying the post action.
What script processes your AJAX requests on the server side?
admin-ajax.php
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?
ajaxurl
What function can you use to determine whether the user is logged in?
is_user_logged_in()
How do you obtain an object containing the current user?
$user = wp_get_current_user();
What function do you use to add meta data to the user?
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.
How do you retrieve the meta data stored against a user?
$values = get_user_meta( $user_id, ‘wanted_posts’ );
It’s best practice to add a what? to the beginning of each meta field name?
Unique prefix.
Why do you need to check if a user is logged in before trying to save meta data?
Because you can’t save user meta data for someone that is not logged in.
What event do you use to create a dashboard widget?
wp_dashboard_setup
Why should you use wp_get_current_user instead of get_current_user?
get_current_user gives you your apache user, not the logged in user.
What function do you use to actually add the dashboard widget to the screen?
wp_add_dashboard_widget( ‘css_id’, $title, ‘cain_show_dashboard_widget’ );
What is the gotcha with using wordpress cron jobs?
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.
What action would you use to create a cron job?
The ‘init’ action.
In your cronjob initialisation function, what must you do before initialising the cronjob itself?
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’ );
}
What does the parameters of ‘wp_shedule_event’ do?
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.
What are the three parts of a plugins lifecycle?
Activation
Deactivation
Deletion/Removal
What should you do on plugin deletion?
Remove all traces of the plugin - all tables, data etc. It’s a pretty final step (i.e. the user really wants this).
It’s common practice to put the uninstall code where?
In an uninstall file, in the plugin directory
What function do you call in the plugin activation part of the lifecycle, to perform actions like creating database tables?
register_activation_hook( __FILE__, ‘name_of_function’ );
What function do you call in the plugin deactivation cycle to (for instance) record the fact it has been deactivated to a log file?
register_deactivation_hook ( __FILE__, ‘name_of_function’ );
How do you access the WP database so you can add new tables?
Use the global variable.
global $wpdb;
How do you ‘name’ a new table, such that it uses the correct prefix (remember, users can specify their own prefix)?
global $wpdb;
$table_name = $wpdb->prefix . ‘my_table’;
How can you check to make sure you haven’t already created this table?
if ( $wpdb->get_var(“SHOW TABLES LIKE $table_name
”) != $table_name) { // Do stuff }
How do you create a table, once you have an SQL statement?
$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.
Database table creation should be done using…
register_activation_hook ( __FILE__, ‘my_func_name’ );
What wp function can be used to get the current time?
current_time()
What parameter can be passed to current_time to get the time in a MySQL format?
current_time( ‘mysql’ );
How do you insert data into a wordpress database?
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.
What is one way of implementing a page counter?
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.
How would you drop a table when your plugin is deleted?
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).
What must you do before testing the delete plugin functionality?
Backup your code!!
it will delete it