Plugin-Widgets

=================== FAQs Plugin with Widgets ===============

<?php

/*
Plugin Name: RC Faq
Plugin URL: http://remicorson.com/rc-faq
Description: A simple FAQ plugin
Version: 1.0
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
*/

/*
* Register CPT rc_faq
*
*/

////////////////////////————- This for FAQs Plugins.
function rc_faq_setup_post_types() {

$faq_labels = apply_filters( ‘rc_faq_labels’, array(
‘name’ => ‘FAQs’,
‘singular_name’ => ‘FAQ’,
‘add_new’ => __(‘Add New’, ‘rc_faq’),
‘add_new_item’ => __(‘Add New FAQ’, ‘rc_faq’),
‘edit_item’ => __(‘Edit FAQ’, ‘rc_faq’),
‘new_item’ => __(‘New FAQ’, ‘rc_faq’),
‘all_items’ => __(‘All FAQs’, ‘rc_faq’),
‘view_item’ => __(‘View FAQ’, ‘rc_faq’),
‘search_items’ => __(‘Search FAQs’, ‘rc_faq’),
‘not_found’ => __(‘No FAQs found’, ‘rc_faq’),
‘not_found_in_trash’ => __(‘No FAQs found in Trash’, ‘rc_faq’),
‘parent_item_colon’ => ”,
‘menu_name’ => __(‘FAQs’, ‘rc_faq’),
‘exclude_from_search’ => true
) );
$faq_args = array(
‘labels’ => $faq_labels,
‘public’ => true,
‘publicly_queryable’=> true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘query_var’ => true,
‘capability_type’ => ‘post’,
‘has_archive’ => false,
‘hierarchical’ => false,
‘supports’ => apply_filters(‘rc_faq_supports’, array( ‘title’, ‘editor’ ) ),
);
register_post_type( ‘rc_faq’, apply_filters( ‘rc_faq_post_type_args’, $faq_args ) );

}

add_action(‘init’, ‘rc_faq_setup_post_types’);
/*
* Add [rc_faq limit=”-1″] shortcode
*
*/
function rc_faq_shortcode( $atts, $content = null ) {

extract(shortcode_atts(array(
“limit” => ”
), $atts));

// Define limit
if( $limit ) {
$posts_per_page = $limit;
} else {
$posts_per_page = ‘-1’;
}

ob_start();

// Create the Query
$post_type = ‘rc_faq’;
$orderby = ‘menu_order’;
$order = ‘ASC’;

$query = new WP_Query( array (
‘post_type’ => $post_type,
‘posts_per_page’ => $posts_per_page,
‘orderby’ => $orderby,
‘order’ => $order,
‘no_found_rows’ => 1
)
);

//Get post type count
$post_count = $query->post_count;
$i = 1;

// Displays FAQ info
if( $post_count > 0) :

// Loop
while ($query->have_posts()) : $query->the_post();
?>

<h3 class=”rc_faq_title”><a href=”#” onclick=”rc_faq_toggle(‘rc_faq_<?php echo get_the_ID(); ?>’);”><?php the_title(); ?></a></h3>
<p id=”rc_faq_<?php echo get_the_ID(); ?>” style=”display: none;”><?php echo get_the_content(); ?></p>

<?php
$i++;
endwhile;

endif;

// Reset query to prevent conflicts
wp_reset_query();

?>
<script type=”text/javascript”>
<!–
function rc_faq_toggle(id) {
var e = document.getElementById(id);
e.style.display = ((e.style.display!=’none’) ? ‘none’ : ‘block’);
}
//–>
</script>
<?php

return ob_get_clean();

}

add_shortcode(“rc_faq”, “rc_faq_shortcode”);
/////////////////////////////////———— This for FAQs Widgets
class wpb_widget extends WP_Widget {

function __construct() {
parent::__construct(
// Base ID of your widget
‘wpb_widget’,

// Widget name will appear in UI
__(‘WPBeginner Widget’, ‘wpb_widget_domain’),

// Widget description
array( ‘description’ => __( ‘Sample widget based on WPBeginner Tutorial’, ‘wpb_widget_domain’ ), )
);
}

// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( ‘widget_title’, $instance[‘title’] );
// before and after widget arguments are defined by themes
echo $args[‘before_widget’];
if ( ! empty( $title ) )
echo $args[‘before_title’] . $title . $args[‘after_title’];

// This is where you run the code and display the output
$faqs = ”;
// Create the Query
$post_type = ‘rc_faq’;
$orderby = ‘menu_order’;
$order = ‘ASC’;

$query = new WP_Query( array (
‘post_type’ => $post_type,
‘posts_per_page’ => $posts_per_page,
‘orderby’ => $orderby,
‘order’ => $order,
‘no_found_rows’ => 1
)
);

//Get post type count
$post_count = $query->post_count;
$i = 1;

// Displays FAQ info
if( $post_count > 0) :

// Loop
$faqs .= ‘<ul>’;
while ($query->have_posts()) : $query->the_post();
$faqs .= ‘<li>’.the_title().'</li>’;
$i++;
endwhile;
$faqs .= ‘</ul>’;

endif;

echo __( $faqs, ‘wpb_widget_domain’ );
echo $args[‘after_widget’];
}

// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ ‘title’ ] ) ) {
$title = $instance[ ‘title’ ];
}
else {
$title = __( ‘New title 1’, ‘wpb_widget_domain’ );
}

// Widget admin form
?>
<p>
<label for=”<?php echo $this->get_field_id( ‘title’ ); ?>”><?php _e( ‘Title:’ ); ?></label>
<input class=”widefat” id=”<?php echo $this->get_field_id( ‘title’ ); ?>” name=”<?php echo $this->get_field_name( ‘title’ ); ?>” type=”text” value=”<?php echo esc_attr( $title ); ?>” />
</p>
<?php
}

// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[‘title’] = ( ! empty( $new_instance[‘title’] ) ) ? strip_tags( $new_instance[‘title’] ) : ”;
return $instance;
}
} // Class wpb_widget ends here

// Register and load the widget
function wpb_load_widget() {
register_widget( ‘wpb_widget’ );
}
add_action( ‘widgets_init’, ‘wpb_load_widget’ );

 

https://www.dropbox.com/s/e8r84if50ugnqcy/rc-faq_widget.docx

https://www.dropbox.com/s/erekst7fgce433n/rc-faq_widget.php

=================== FAQs Plugin with Widgets ===============

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: