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.