Last active
December 29, 2015 04:49
-
-
Save asvechkar/7617171 to your computer and use it in GitHub Desktop.
Powerful WordPress Tips And Tricks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Replace Built-In Scripts By Deregistering Them */ | |
function my_scripts_method() { | |
wp_deregister_script( 'jquery' ); | |
wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery-new.js'); | |
wp_enqueue_script( 'jquery' ); | |
} | |
add_action('wp_enqueue_scripts', 'my_scripts_method'); | |
/* Force Perfect JPG Images */ | |
add_filter( 'jpeg_quality', 'smashing_jpeg_quality' ); | |
function smashing_jpeg_quality() { | |
return 100; | |
} | |
/* FeedBurner Redirection */ | |
add_action( 'template_redirect' , 'smashing_rss_redirect'); | |
function smashing_rss_redirect() { | |
if ( is_feed() AND !preg_match( '/feedburner|feedvalidator/i', $_SERVER['HTTP_USER_AGENT'] ) ){ | |
header( 'Location: http://feeds.feedburner.com/my_smashing_feed' ); | |
header( 'HTTP/1.1 302 Temporary Redirect' ); | |
} | |
} | |
/* Setting Up Sessions In WordPress */ | |
add_action( 'init', 'smashing_session_start' ); | |
function smashing_session_start() { | |
if ( !session_id() ) { | |
session_start(); | |
} | |
} | |
/* List All Hooked Functions */ | |
function list_hooked_functions($tag=false){ | |
global $wp_filter; | |
if ($tag) { | |
$hook[$tag]=$wp_filter[$tag]; | |
if (!is_array($hook[$tag])) { | |
trigger_error("Nothing found for '$tag' hook", E_USER_WARNING); | |
return; | |
} | |
} | |
else { | |
$hook=$wp_filter; | |
ksort($hook); | |
} | |
echo '<pre>'; | |
foreach($hook as $tag => $priority){ | |
echo "<br />>>>>>\t<strong>$tag</strong><br />"; | |
ksort($priority); | |
foreach($priority as $priority => $function){ | |
echo $priority; | |
foreach($function as $name => $properties) { | |
echo "\t$name<br />"; | |
} | |
} | |
} | |
echo '</pre>'; | |
return; | |
} | |
/* Automatically Add Paragraph Tags To Anything */ | |
remove_filter( 'the_content', 'wpautop' ); | |
remove_filter( 'the_excerpt', 'wpautop' ); | |
/* Send Emails Using WordPress */ | |
$message = 'Hello, thanks for reading my post! I hope to see you back soon.'; | |
wp_mail( '[email protected]', 'Thanks for reading my post!', $message); | |
add_filter ("wp_mail_content_type", "smashing_mail_content_type"); | |
function smashing_mail_content_type() { | |
return "text/html"; | |
} | |
/* Native Pagination Links */ | |
// Pagination for a WordPress loop | |
$list = new WP_Query( $query_args ); | |
$pagination = array( | |
'base' => str_replace( 99999, '%#%', get_pagenum_link( 99999 ) ), | |
'format' => '?paged=%#%', | |
'current' => max( 1, get_query_var( 'paged' ) ), | |
'total' => $list->max_num_pages, | |
'next_text' => 'next', | |
'prev_text' => 'previous' | |
); | |
echo '<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>'; | |
// Pagination for anything | |
$list = range(1, 100); | |
$items_per_page = 12; | |
$pagination = array( | |
'base' => get_bloginfo( 'url' ) . '/mypage/%_%', | |
'format' => '?paged=%#%', | |
'current' => $_GET['current_page'], | |
'total' => ceil( max($list) / $items_per_page ), | |
'next_text' => 'go forth', | |
'prev_text' => 'go back' | |
); | |
echo '<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>'; | |
/* Upload Files With Ease */ | |
$upload = wp_upload_bits( $_FILES['myfile']['name'], null, file_get_contents( $_FILES['myfile']['tmp_name'] ) ); | |
echo 'Well uploaded! The path to this file is ' . $upload['file'] . ' and the url to this file is ' . $upload['url']; | |
/* Twitter-Like Time Display */ | |
$diff = human_time_diff( '2012-05-05 12:05:00', '2012-05-05 12:10:00' ); | |
echo 'This comment was submitted ' . $diff . 'ago'; | |
/* Log In As Any User */ | |
$user_id = 4; | |
wp_set_auth_cookie( $user_id ); | |
/* Add Custom Profile Fields In The Admin Area */ | |
add_action( 'show_user_profile', 'smashing_profile_fields' ); | |
add_action( 'edit_user_profile', 'smashing_profile_fields' ); | |
function smashing_profile_fields( $user ) { | |
?> | |
<h3>Social Sites</h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="twitter">Twitter</label></th> | |
<td> | |
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" /><br /> | |
<span class="description">Your Twitter Username</span> | |
</td> | |
</tr> | |
<tr> | |
<th><label for="twitter">Facebook</label></th> | |
<td> | |
<input type="text" name="facebook" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" /><br /> | |
<span class="description">Your Facebook Profile URL</span> | |
</td> | |
</tr> | |
<tr> | |
<th><label for="twitter">Linkedin</label></th> | |
<td> | |
<input type="text" name="linkedin" id="linkedin" value="<?php echo esc_attr( get_the_author_meta( 'linkedin', $user->ID ) ); ?>" /><br /> | |
<span class="description">Your Linkedin Profile URL</span> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
add_action( 'personal_options_update', 'smashing_save_profile_fields' ); | |
add_action( 'edit_user_profile_update', 'smashing_save_profile_fields' ); | |
function smashing_save_profile_fields( $user_id ) { | |
if ( !current_user_can( 'edit_user', $user_id ) ) | |
return false; | |
update_user_meta( $user_id, 'twitter', $_POST['twitter'] ); | |
update_user_meta( $user_id, 'facebook', $_POST['facebook'] ); | |
update_user_meta( $user_id, 'linkedin', $_POST['linkedin'] ); | |
} | |
/* Sanitize URLs With Ease */ | |
$my_url = 'http://mypage.com/?awesome=true'; | |
$url = esc_url( $my_url ); | |
/* Empower Text Widgets */ | |
add_filter( 'widget_text', 'do_shortcode' ); | |
/* Add Custom Post Types To The RSS Feed */ | |
add_filter('request', 'smashing_custom_feed'); | |
function smashing_custom_feed( $vars ) { | |
if ( isset( $vars['feed'] ) ) { | |
$vars['post_type'] = get_post_types(); | |
} | |
return $vars; | |
} | |
add_filter('request', 'smashing_custom_feed'); | |
$post_type_list = array( 'post', 'products' ); | |
function smashing_custom_feed( $vars ) { | |
if ( isset( $vars['feed'] ) AND !isset( $vars['post_type'] ) ) { | |
$vars['post_type'] = $post_type_list; | |
} | |
return $vars; | |
} | |
/* Don’t Break WordPress Loops */ | |
$tmp_query = $wp_query; | |
query_posts('cat=5&order=ASC'); | |
while( have_posts() ) : the_post() | |
?> | |
<a href="<?php the_permalink() ?>'><?php the_title() ?></a><br /> | |
<?php | |
$wp_query = $tmp_query; | |
/* Custom Database Queries */ | |
$recent_users = $wpdb->get_results( "SELECT display_name, user_registered FROM $wpdb->users ORDER BY user_registered DESC LIMIT 0,10" ); | |
/* Customize WordPress Post Revisions */ | |
// To remove revisions | |
define( 'WP_POST_REVISIONS', FALSE ); | |
// To limit them | |
define( 'WP_POST_REVISIONS', 5 ); | |
/* Storing Your Whole Page In A Variable */ | |
add_action('wp_head', 'smashing_buffer_start'); | |
add_action('wp_footer', 'smashing_buffer_end'); | |
function smashing_buffer_start() { | |
ob_start( 'smashing_callback' ); | |
} | |
function buffer_end() { | |
ob_end_flush(); | |
} | |
function smashing_callback( $content ) { | |
// Feel free to do things to the content here | |
$content = str_replace( 'great', 'awesome', $content ); | |
echo $content; | |
} | |
/* List all hooked WordPress functions */ | |
function list_hooked_functions($tag=false){ | |
global $wp_filter; | |
if ($tag) { | |
$hook[$tag]=$wp_filter[$tag]; | |
if (!is_array($hook[$tag])) { | |
trigger_error("Nothing found for '$tag' hook", E_USER_WARNING); | |
return; | |
} | |
} | |
else { | |
$hook=$wp_filter; | |
ksort($hook); | |
} | |
echo '<pre>'; | |
foreach($hook as $tag => $priority){ | |
echo "<br />>>>>>\t<strong>$tag</strong><br />"; | |
ksort($priority); | |
foreach($priority as $priority => $function){ | |
echo $priority; | |
foreach($function as $name => $properties) echo "\t$name<br />"; | |
} | |
} | |
echo '</pre>'; | |
return; | |
} | |
/* SHOW THE ADMIN BAR FOR ADMINISTRATORS */ | |
if (!current_user_can( 'manage_options' )) { | |
add_filter('show_admin_bar', '__return_false'); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment