Small pieces of code, extending the functionality of WordPress. This is how snippets could be characterized in one sentence. That modules also play this role? Sure, they are very similar in nature, but two key factors determine the uniqueness of snippets: they do not require installation and, due to their minimal volume, they do not burden the server.
Table of Contents
How to work with WordPress snippets?
The most common location for storing snippets is the functions.php file in the active theme, and can be just as useful for global use within the wp-config.php file. Just open a text editor and add the appropriate code to these files. A certain disadvantage of manually inserting snippets is their connection to the currently activated theme, with the exception of the above-mentioned exception with wp-config.php.
The solution is the Code Snippets module, which operates systemically and in the comfort of the administrative environment offers, in addition to the obligatory addition, modification, deletion of its own codes, also their backup via an XML file. Individual snippets can be activated and turned off as needed, Code Snippets can also be used for Multisite installation and the Slovak translation of this module will definitely be a bonus for you.
WordPress snippets in practice
With the growing popularity of WordPress, the number of interesting snippets is also increasing. The following selection is an example of a number of useful solutions, a few links to other resources can be found at the end of the article.
This snippet removes the WP version label from the page header
function remove_wp_version() { return ''; } add_filter('the_generator', 'remove_wp_version');
We will insert jQuery from Google Apis into the header
if( !is_admin() ){ wp_deregister_script('jquery'); wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"), false, ''); wp_enqueue_script('jquery'); }
or a font with support for Slovak accents from Google Fonts
function load_fonts() { wp_register_style ('googleFonts', 'http://fonts.googleapis.com/css?family=News+Cycle&subset=latin,latin-ext'); wp_enqueue_style( 'googleFonts'); } add_action('wp_print_styles', 'load_fonts');
We will turn off the admin bar display for all users except administrators
if (!current_user_can('administrator')): show_admin_bar(false); endif;
We will edit the user’s information in his profile
function new_contactmethods( $contactmethods ) { $contactmethods['twitter'] = 'Twitter'; // pridame Twitter $contactmethods['facebook'] = 'Facebook'; // pridame Facebook unset($contactmethods['yim']); // odoberieme Yahoo IM unset($contactmethods['aim']); // odoberieme AIM unset($contactmethods['jabber']); // odoberieme Jabber return $contactmethods; } add_filter('user_contactmethods','new_contactmethods',10,1);
We will remove preset WordPress widgets
function unregister_default_widgets() { unregister_widget('WP_Widget_Pages'); unregister_widget('WP_Widget_Calendar'); unregister_widget('WP_Widget_Archives'); unregister_widget('WP_Widget_Links'); unregister_widget('WP_Widget_Meta'); unregister_widget('WP_Widget_Search'); unregister_widget('WP_Widget_Text'); unregister_widget('WP_Widget_Categories'); unregister_widget('WP_Widget_Recent_Posts'); unregister_widget('WP_Widget_Recent_Comments'); unregister_widget('WP_Widget_RSS'); unregister_widget('WP_Widget_Tag_Cloud'); unregister_widget('WP_Nav_Menu_Widget'); unregister_widget('Twenty_Eleven_Ephemera_Widget'); } add_action('widgets_init', 'unregister_default_widgets', 11);
We will disable the use of the visual editor
add_filter('user_can_richedit' , create_function('' , 'return false;') , 50);
We will choose our own length of the list of contributions
custom_excerpt_length($length) { return 100; } add_filter('excerpt_length', 'custom_excerpt_length');
We will change the redirect after the user logs in
add_action('login_form', 'redirect_after_login'); function redirect_after_login() { global $redirect_to; if (!isset($_GET['redirect_to'])) { $redirect_to = get_option('siteurl'); // presmerujeme na homepage } }
We will allow the author 7 days to edit an already published article
function stop_post_editing_filter( $capauser, $capask, $param){ global $wpdb; $post = get_post( $param[2] ); if( $post->post_status == 'publish' ){ // obmedzenie bude platit pre rolu Autor if( $capauser['author'] == 1 ){ if( ( $param[0] == "edit_post") || ( $param[0] == "delete_post" ) ) { $post_time_unix = strtotime( str_replace('-', ':', $post->post_date ) ); $current_time_unix = time(); $diff = $current_time_unix - $post_time_unix; $hours_after_publication = floor( $diff / 60 / 60 ); // po 168 hodinach od publikovania bude automaticky vypnuta moznost dalsej upravy prispevku if( $hours_after_publication >= 168 ){ foreach( (array) $capask as $capasuppr) { if ( array_key_exists($capasuppr, $capauser) ) { $capauser[$capasuppr] = 0; } } } } } } return $capauser; } add_filter('user_has_cap', 'stop_post_editing_filter', 100, 3 );
We will set the interval of automatic saving of the post to 10 minutes – 60 seconds x 10 (in the file wp-config.php)
define('AUTOSAVE_INTERVAL', 600);
Disable posting revisions (in wp-config.php)
define('WP_POST_REVISIONS', false);
Or we set the post revision limit to 5
define('WP_POST_REVISIONS', 5)
We automatically empty the Recycle Bin every 5 days (in the wp-config.php file)
define('EMPTY_TRASH_DAYS', 5 );
We will insert a link to edit the post (eg in single.php or page.php files)
<?php edit_post_link ('Upraviť príspevok'); ?>
The most famous sources of WordPress snippets
Back up, back up, back up!
Murpy’s law is clear: the bigger the error in the code, the more unexpected it will show. To avoid possible complications, do not forget to make a backup of the edited file and database.
Have you created your own snippets or have you discovered some interesting ones while roaming the internet? Write to us about it in the comments.
Was this article helpful for you? Support me by sharing, please. 👍