6 handige WordPress snippets

 

1. Een custom post type FAQ in WordPress maken

Met de onderstaande snippet van rentageekmom.com kun je een custom post type FAQ maken.

<?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’ ),

));

}

?>

2. Je uploads directory in WordPress beveiligen

Met deze snippet van Jeff Starr kun je alle uploads in je WordPress directory beveiligen.

<Files ~ “.*..*”>

Order Allow,Deny

Deny from all

</Files>

<FilesMatch “.(jpg|jpeg|jpe|gif|png|tif|tiff)$”>

Order Deny,Allow

Allow from all

</FilesMatch>

 

3. E-mail alerts voor 404 pagina’s instellen

Met deze snippet van WP Mix, krijg je een e-mail alert, elke keer dat een bezoeker een 404 melding krijgt.

 

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

?>

4. Het formaat van embedded video’s aanpassen

Door de onderstaande snippet van Shailan naar je functions.php bestand te kopiëren, kun je het formaat van je embedded video’s aanpassen.

 

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

5. Testen of een WordPress plugin actief is

Met deze snippet van CatsWhoCode kun je snel en simpel testen of een plugin actief is of niet.

<?php

if (is_plugin_active(‘plugin-directory/plugin-file.php’)) {

// the plugin is active

}

?>

6. De Post Updated Message In WordPress veranderen

Met de onderstaande snippet van Snipplr kun je de Post Updated Message zelf veranderen. Het enige dat je hoeft te doen is de snippet kopiëren, en je bent klaar!

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

}

Geef een reactie