How to make an FAQ plugin for your WordPress™ website

Howdy! folks, I recently received an email from a follower of my blog who wanted an FAQ plug-in for his WordPress website, since they are unavailable for free I guess… So here’s the deal.

Note: If you are new to this I highly recommend  you should install XAMPP and work on localserver.

#1. Lets begin

Goto the folder you installed Xampp and wordpress and under the /xampp/htdocs/wordpress/wp-content/plugins make a new folder called FAQs

 

Example

Example of Path to xampp->wordpress->wp-content->plugin

in that folder create a new file namely faqs.php

#2. Adding Header

Adding header to your file

//Code Starts

<?php
/*
Plugin Name: FAQs //Your Plugin name, in my case it’s FAQs since it’s requested
Plugin URL: http://tyrant.co.nf/metro  //the url for your website, in my case it’s mone of my websites
Description: A simple FAQ Plug-in that lets u add FAQs Tab to your wordpress theme //Description of your plugin
Version: 1.0(Beta) //version history to track updates
Author: voltainc™ //author name of the plugin
Author URI: http://www.awaissoftnews.wordpress.com //plugin’s website, in my case it’s my blog
Contributors: Awais Shaikh  //the people who helped you in the making of the plugin, in my case it’s my colleague.
*/

/*
* Register CPT faqs
*
*/

and that’s all for the header part, remember what we coding here must be contained within a single file i.e “faqs.php”

after doing this open up your WordPress dashboard and goto the plugins area

example

Plugin’s Page

It should look something like above picture. At this point our plugin wont do anything except showing the credentials about the plugin, now to get our plugin working we must add some code that executes when we require it to, so here goes..

#3. Adding functionality

note: if you are unfamiliar with the hook programming  i suggest u read about it first on
https://codex.wordpress.org/Writing_a_Plugin

and for a bit of extra info : http://wordpress.org/plugins/

//remaining part of code

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

?>

//Code Ends

Explanation : What this code do is that when you activates the plugin it allows you to add a separate plugin tab in your dashboard amongst others options, the code also contains the short codes that enable you a form with title and content area that lets you post as many FAQs as you want and view them later you done adding.

this should look something like this :

plugin features

the plugin is provided with 2 features that’s “add new” & “all faqs”, both of them are self explanatory.

#4. Adding an FAQ

You can add an FAQ by simply clicking add new under FAQ tab in the dashboard:

add new faq

and view them when done  by clicking All FAQs under the FAQs tab:

#5. Viewing FAQS

view all faq

And there you have it, we are done creating a WP – plugin in 5 simple steps that Adds FAQs and views the stored ones…

Advertisements

One thought on “How to make an FAQ plugin for your WordPress™ website

  1. It’s awesome to pay a quick visit this web page and reading the views of all mates about this article, while I am also eager of getting knowledge.

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