Integrating in Wordpress Flashcards
How do you add a submenu to the ‘Settings’ page?
<?php add_options_page( page_title, menu_title, capability, menu_slug, function);?>
How do you create a top-level menu page?
<?php add_menu_page( page_title, menu_title, capability, menu_slug, function, icon_url, position ); ?>
How are all widgets created in Wordpress?
- register your widget on ‘widgets_init’ action hook.
- extend WP_Widget using the register_widget function name from step 1.
- process new widget with function - insert your function name.
- build settings form with function form()
- save widget settings with function update()
- display the widget with function widget()
<?php </p>
class My_Widget extends WP_Widget {
function My_Widget() {
// processes the widget
}
function form($instance) {
// displays the widget form in the admin dashboard
}
function update($new_instance, $old_instance) {
// process widget options to save
}
function widget($args, $instance) {
// displays the widget
}
}
?>
Which WP_Widget method should you create widget options in?
The processing function:
<?php </p>
function boj_widgetexample_widget_my_info() {
$widget_ops = array(
‘classname’ => ‘boj_widgetexample_widget_class’,
‘description’ => ‘Display a user's favorite movie and song.’ );
$this->WP_Widget( ‘boj_widgetexample_widget_my_info’, ‘My Info Widget’, $widget_ops );
}
Write code to register a widget called ‘bs_widget’.
<?php </p>
add_action( ‘widgets_init’, ‘bs_widget’ );
function bs_widget() {
register_widget( ‘bs_widget’ );
}
?>
Write code to set up widget class for ‘bs_widget’ and create options using the appropriate WP_Widget method.
<?php </p>
function bs_widget() {
$widget_ops = array(
‘classname’ => ‘bs_widget_class’,
‘description’ => ‘Display a user's favorite movie and song.’
);
$this->WP_Widget( ‘bs_widget’, ‘My Info Widget’, $widget_ops );
}
?>
Set up widget settings form for ‘bs_widget’ to accept three values: Title, Favorite Movie and Favorite Song.
<?php </p>
function form($instance) {
$defaults = array( ‘title’ => ‘My Info’, ‘movie’ => ‘’, ‘song’ => ‘’ );
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance[‘title’];
$movie = $instance[‘movie’];
$song = $instance[‘song’];
?>
Title:
Favorite Movie:
Favorite Song:
}
?>
Save widget settings(title, movie, song) for bs_widget using the appropriate WP_Widget method.
<?php </p>
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance[‘title’] = strip_tags( $new_instance[‘title’] );
$instance[‘movie’] = strip_tags( $new_instance[‘movie’] );
$instance[‘song’] = strip_tags( $new_instance[‘song’] );
return $instance;
}
?>
Display the title, fav movie, fav song of ‘bs_widget’ in
using the appropriate method of WP_Widget.
<?php </p>
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( ‘widget_title’, $instance[‘title’] );
$movie = empty( $instance[‘movie’] ) ? ‘ ‘ : $instance[‘movie’];
$song = empty( $instance[‘song’] ) ? ‘ ‘ : $instance[‘song’];
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo ‘
Fav Movie: ‘ . $movie . ‘
’;
echo ‘
Fav Song: ‘ . $song . ‘
’;
echo $after_widget;
}
?>