Creating Custom Post Type FAQ In WordPress
The following snippet will let you create custom post type FAQ.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
<?php add_action('init', 'custom_faqs'); function custom_faqs() { $labels = array( 'name' => _x('faqs', 'post type general name'), 'singular_name' => _x('faq', 'post type singular name'), 'add_new' => _x('Add New', 'FAQ'), 'add_new_item' => __('Add New FAQ'), 'edit_item' => __('Edit FAQ'), 'new_item' => __('New FAQ'), 'view_item' => __('View FAQ'), 'search_items' => __('Search FAQs'), 'not_found' => __('No FAQs found'), 'not_found_in_trash' => __('No FAQs found in Trash'), 'parent_item_colon' => '', 'menu_name' => 'FAQ' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 5, 'supports' => array('title','editor','thumbnail','excerpt','custom-fields') ); register_post_type('faqs',$args); } //add filter to ensure the text faq, or faq, is displayed when user updates a faq add_filter('post_updated_messages', 'faq_updated_messages'); function faq_updated_messages( $messages ) { global $post, $post_ID; $messages['faqs'] = array( 0 => '', // Unused. Messages start at index 1. 1 => sprintf( __('FAQ updated. <a href="%s">View FAQ</a>'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('FAQ updated.'), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf( __('FAQ restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('FAQ published. <a href="%s">View FAQ</a>'), esc_url( get_permalink($post_ID) ) ), 7 => __('FAQ saved.'), 8 => sprintf( __('FAQ submitted. <a target="_blank" href="%s">Preview FAQ</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('FAQ scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview FAQ</a>'), // translators: Publish box date format, see http://php.net/date date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('FAQ draft updated. <a target="_blank" href="%s">Preview FAQ</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; } //display contextual help for faqs add_action( 'contextual_help', 'faq_add_help_text', 10, 3 ); function faq_add_help_text($contextual_help, $screen_id, $screen) { //$contextual_help .= var_dump($screen); // use this to help determine $screen->id if ('faqs' == $screen->id ) { $contextual_help = '<p>' . __('Things to remember when adding or editing a FAQ:') . '</p>' . '<ul>' . '<li>' . __('Specify the correct topic such as Orders. Create a new topic if necessary.') . '</li>' . '<li>' . __('Specify a custom excerpt - a short overview of the FAQ') . '</li>' . '</ul>' . '<p>' . __('Choose related posts using the MicroKids Related Posts Plugin below the FAQ') . '</p>' . '<ul>' . '<li>' . __('Click the Save button to save an FAQ as a draft') . '</li>' . '<li>' . __('Click publish to publish an FAQ. ') . '</li>' . '</ul></p>'; } elseif ( 'edit-faqs' == $screen->id ) { $contextual_help = '<p>' . __('This is the list of all current FAQs in published or draft form. Hover over an FAQ and click Edit to edit, or click on the title of the FAQ to edit.') . '</p>' ; } return $contextual_help; } function my_rewrite_flush() { custom_faqs(); flush_rewrite_rules(); } register_activation_hook(__FILE__, 'my_rewrite_flush'); //hook into the init action and call create_FAQ_taxonomies when it fires add_action( 'init', 'create_faq_taxonomies', 0 ); //create two taxonomies, genres and writers for the post type "FAQ" function create_faq_taxonomies() { // Add new taxonomy, make it hierarchical (like categories) $labels = array( 'name' => _x( 'topics', 'taxonomy general name' ), 'singular_name' => _x( 'topic', 'taxonomy singular name' ), 'search_items' => __( 'Search Topics' ), 'all_items' => __( 'All Topics' ), 'parent_item' => __( 'Parent Topic' ), 'parent_item_colon' => __( 'Parent Topic:' ), 'edit_item' => __( 'Edit Topic' ), 'update_item' => __( 'Update Topic' ), 'add_new_item' => __( 'Add New Topic' ), 'new_item_name' => __( 'New Topic Name' ), 'menu_name' => __( 'Topics' ), ); register_taxonomy('topic',array('faqs'), array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'topic' ), )); } ?> |
Snippet Source/Credit: rentageekmom.com
How To Secure Your Uploads Directory In WordPress
Using the following snippet, you will be able to secure uploads directory in WordPress.
1 2 3 4 5 6 7 8 |
<Files ~ ".*..*"> Order Allow,Deny Deny from all </Files> <FilesMatch ".(jpg|jpeg|jpe|gif|png|tif|tiff)$"> Order Deny,Allow Allow from all </FilesMatch> |
Snippet Source/Credit: Jeff Starr
How To Get Email Alerts For 404 Pages In Website
Using the following snippet, you will be able to get email alerts for 404 pages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<?php // WP 404 ALERTS @ http://wp-mix.com/wordpress-404-email-alerts/ // set status header("HTTP/1.1 404 Not Found"); header("Status: 404 Not Found"); // site info $blog = get_bloginfo('name'); $site = get_bloginfo('url') . '/'; $email = get_bloginfo('admin_email'); // theme info if (!empty($_COOKIE["nkthemeswitch" . COOKIEHASH])) { $theme = clean($_COOKIE["nkthemeswitch" . COOKIEHASH]); } else { $theme_data = wp_get_theme(); $theme = clean($theme_data->Name); } // referrer if (isset($_SERVER['HTTP_REFERER'])) { $referer = clean($_SERVER['HTTP_REFERER']); } else { $referer = "undefined"; } // request URI if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) { $request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); } else { $request = "undefined"; } // query string if (isset($_SERVER['QUERY_STRING'])) { $string = clean($_SERVER['QUERY_STRING']); } else { $string = "undefined"; } // IP address if (isset($_SERVER['REMOTE_ADDR'])) { $address = clean($_SERVER['REMOTE_ADDR']); } else { $address = "undefined"; } // user agent if (isset($_SERVER['HTTP_USER_AGENT'])) { $agent = clean($_SERVER['HTTP_USER_AGENT']); } else { $agent = "undefined"; } // identity if (isset($_SERVER['REMOTE_IDENT'])) { $remote = clean($_SERVER['REMOTE_IDENT']); } else { $remote = "undefined"; } // log time $time = clean(date("F jS Y, h:ia", time())); // sanitize function clean($string) { $string = rtrim($string); $string = ltrim($string); $string = htmlentities($string, ENT_QUOTES); $string = str_replace("\n", "<br>", $string); if (get_magic_quotes_gpc()) { $string = stripslashes($string); } return $string; } $message = "TIME: " . $time . "\n" . "*404: " . $request . "\n" . "SITE: " . $site . "\n" . "THEME: " . $theme . "\n" . "REFERRER: " . $referer . "\n" . "QUERY STRING: " . $string . "\n" . "REMOTE ADDRESS: " . $address . "\n" . "REMOTE IDENTITY: " . $remote . "\n" . "USER AGENT: " . $agent . "\n\n\n"; mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email"); ?> |
Snippet Source/Credit: WP Mix
How To Modify Size Of Embedded Videos
Simply paste the following snippet on your functions.php file and you will be able to modify the size of embedded videos as per your requirements.
1 2 3 4 5 6 |
function mycustom_embed_defaults($embed_size){ $embed_size['width'] = 586; // Adjust values to your needs $embed_size['height'] = 500; return $embed_size; } add_filter('embed_defaults', 'mycustom_embed_defaults'); |
Snippet Source/Credit: Shailan
How To Test Whether A Plugin Is Active In WordPress
Using the following snippet, you will be able to test whether a plugin is active or not.
1 2 3 4 5 |
<?php if (is_plugin_active('plugin-directory/plugin-file.php')) { // the plugin is active } ?> |
Snippet Credit/Source: CatsWhoCode
How To Change The Post Updated Message Itself In WordPress
The use of the following snippet will let you change the post updated message itself. All you have to do is to simply insert the snippet and you are done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//add a custom message to the post message function add_filter('post_updated_messages', 'listing_updated_messages'); function listing_updated_messages( $messages ) { $messages['listing'] = array( 0 => '', // Unused. Messages start at index 1. 1 => sprintf( __('Listing updated. <a href="%s">View Listing</a>'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Listing updated.'), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf( __('Listing restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('Listing published. <a href="%s">View Listing</a>'), esc_url( get_permalink($post_ID) ) ), 7 => __('Listing saved.'), 8 => sprintf( __('Listing submitted. <a target="_blank" href="%s">Preview Listing</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Listing scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Listing</a>'), // translators: Publish box date format, see http://php.net/date date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('Listing draft updated. <a target="_blank" href="%s">Preview Listing</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; } |
Snippet Source/Credit: Snipplr