Integrating in Wordpress Flashcards

1
Q

How do you add a submenu to the ‘Settings’ page?

A

<?php add_options_page( page_title, menu_title, capability, menu_slug, function);?>

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

How do you create a top-level menu page?

A

<?php add_menu_page( page_title, menu_title, capability, menu_slug, function, icon_url, position ); ?>

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

How are all widgets created in Wordpress?

A
  1. register your widget on ‘widgets_init’ action hook.
  2. extend WP_Widget using the register_widget function name from step 1.
  3. process new widget with function - insert your function name.
  4. build settings form with function form()
  5. save widget settings with function update()
  6. 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

}

}

?>

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

Which WP_Widget method should you create widget options in?

A

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

}

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

Write code to register a widget called ‘bs_widget’.

A

<?php </p>

add_action( ‘widgets_init’, ‘bs_widget’ );

function bs_widget() {

register_widget( ‘bs_widget’ );

}

?>

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

Write code to set up widget class for ‘bs_widget’ and create options using the appropriate WP_Widget method.

A

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

}

?>

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

Set up widget settings form for ‘bs_widget’ to accept three values: Title, Favorite Movie and Favorite Song.

A

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

}

?>

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

Save widget settings(title, movie, song) for bs_widget using the appropriate WP_Widget method.

A

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

}

?>

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

Display the title, fav movie, fav song of ‘bs_widget’ in

using the appropriate method of WP_Widget.

A

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

}

?>

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