Set Custom Post Type in Widget Settings for use in Widget
I'm trying to dynamically list all of CPT's in a dropdown, set one in the widget settings and then echo that value in the actual widget output. I can't seem to get it to work though. I was trying to use get_post_types which might be the correct way to achieve this, but I couldn't manage to use the value in the actual widget.
My Base Widget code is:
<?php
/**
* Add function to widgets_init that'll load our widget.
* @since 0.1
*/
add_action( 'widgets_init', 'xyz_search_resources_widget' );
/**
* Register our widget.
* 'Example_Widget' is the widget class used below.
*
* @since 0.1
*/
function xyz_search_resources_widget() {
register_widget( 'XYZ_Search_Resources' );
}
/**
* Example Widget class.
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
* @since 0.1
*/
class XYZ_Search_Resources extends WP_Widget {
/**
* Widget setup.
*/
function XYZ_Search_Resources() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'xyz_search_resources', 'description' => __('Displays a Search for Resources.', 'example') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'xyz-search-resources' );
/* Create the widget. */
$this->WP_Widget( 'xyz-search-resources', __('XYZ Search Resources', 'xyz_search_resources'), $widget_ops, $control_ops );
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
extract( $args );
/* Our variables from the widget settings. */
//$number = $instance['number'];
$title = $instance['title'];
// start the wordpress loop!
//if ($query->have_posts()) :
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
//if ( $title )
echo $before_title . $title . $after_title;
?>
<div class="search-box">
<form method="get" id="searchform" action="<?php echo home_url() ; ?>/">
<input type="text" value="<?php echo esc_html($s, 1); ?>" name="s" id="s" maxlength="33" />
<input type="image" src="<?php bloginfo('template_directory'); ?>/images/button_search.png" class="button" value=""/>
<input type="hidden" name="post_type" value="xyz_resources">
</form>
</div>
<?php /* END WHILE AND RESET QUERY */ wp_reset_query();
//else:
//echo 'There are no upcoming articles.';
//endif;
/* After widget (defined by themes). */
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//$instance['number'] = strip_tags( $new_instance['number'] );
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function form( $instance ) {
/* Set up some defa
I'm trying to dynamically list all of CPT's in a dropdown, set one in the widget settings and then echo that value in the actual widget output. I can't seem to get it to work though. I was trying to use get_post_types which might be the correct way to achieve this, but I couldn't manage to use the value in the actual widget.
My Base Widget code is:
<?php
/**
* Add function to widgets_init that'll load our widget.
* @since 0.1
*/
add_action( 'widgets_init', 'xyz_search_resources_widget' );
/**
* Register our widget.
* 'Example_Widget' is the widget class used below.
*
* @since 0.1
*/
function xyz_search_resources_widget() {
register_widget( 'XYZ_Search_Resources' );
}
/**
* Example Widget class.
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
* @since 0.1
*/
class XYZ_Search_Resources extends WP_Widget {
/**
* Widget setup.
*/
function XYZ_Search_Resources() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'xyz_search_resources', 'description' => __('Displays a Search for Resources.', 'example') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'xyz-search-resources' );
/* Create the widget. */
$this->WP_Widget( 'xyz-search-resources', __('XYZ Search Resources', 'xyz_search_resources'), $widget_ops, $control_ops );
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
extract( $args );
/* Our variables from the widget settings. */
//$number = $instance['number'];
$title = $instance['title'];
// start the wordpress loop!
//if ($query->have_posts()) :
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
//if ( $title )
echo $before_title . $title . $after_title;
?>
<div class="search-box">
<form method="get" id="searchform" action="<?php echo home_url() ; ?>/">
<input type="text" value="<?php echo esc_html($s, 1); ?>" name="s" id="s" maxlength="33" />
<input type="image" src="<?php bloginfo('template_directory'); ?>/images/button_search.png" class="button" value=""/>
<input type="hidden" name="post_type" value="xyz_resources">
</form>
</div>
<?php /* END WHILE AND RESET QUERY */ wp_reset_query();
//else:
//echo 'There are no upcoming articles.';
//endif;
/* After widget (defined by themes). */
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//$instance['number'] = strip_tags( $new_instance['number'] );
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function form( $instance ) {
/* Set up some defa
No comments:
Post a Comment