functions.php snippets

functions.php snippets

functions.php snippets !

Questa è una raccolta di frammenti di codice che potete inserire nel vostro file functions.php, per aggiungere funzioni personalizzate e modificare quelle già esistenti nel vostro tema.

Ricordatevi, però, di eseguire prima il backup del file functions.php, per poterlo ripristinare in caso di problemi. Inoltre, tutte le funzioni e le modifiche personalizzate al codice PHP dovrebbero essere fatte su un child theme, in modo che tutte le modifiche apportate non vengano cancellate dagli aggiornamenti futuri del tema originale.

Aggiungere i meta tag Open Graph

function meta_og() {
global $post;
if ( is_single() ) {
if( has_post_thumbnail( $post->ID ) ) {
$img_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
}
$excerpt = strip_tags($post->post_content);
$excerpt_more = '';
if ( strlen($excerpt ) > 155) {
$excerpt = substr($excerpt,0,155);
$excerpt_more = ' ...';
}
$excerpt = str_replace( '"', '', $excerpt );
$excerpt = str_replace( "'", '', $excerpt );
$excerptwords = preg_split( '/[\n\r\t ]+/', $excerpt, -1, PREG_SPLIT_NO_EMPTY );
array_pop( $excerptwords );
$excerpt = implode( ' ', $excerptwords ) . $excerpt_more;
?>
<meta name="author" content="Il vostro nome">
<meta name="description" content="<?php echo $excerpt; ?>">
<meta property="og:title" content="<?php echo the_title(); ?>">
<meta property="og:description" content="<?php echo $excerpt; ?>">
<meta property="og:type" content="article">
<meta property="og:url" content="<?php echo the_permalink(); ?>">
<meta property="og:site_name" content="Il vostro sito web">
<meta property="og:image" content="<?php echo $img_src[0]; ?>">
<?php
} else {
return;
}
}
add_action('wp_head', 'meta_og', 5);

Cambiare il logo nella pagina login

function custom_login_logo() {
echo '
<style>
.login h1 a { background-image: url(la vostra immagine.jpg) !important; background-size: 234px 67px; width:234px; height:67px; display:block; }
</style>
';
}
add_action( 'login_head', 'custom_login_logo' );

Inserire del contenuto sotto ogni articolo

// add custom post content
function add_post_content($content) {
if(!is_feed() && !is_home()) {
$content .= '<p>This article is copyright &copy; '.date('Y').'&nbsp;'.bloginfo('name').'</p>';
}
return $content;
}
add_filter('the_content', 'add_post_content');

Lunghezza personalizzata per l’excerpt (il riassunto)

function custom_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Sostituire il testo del link "Read More"

function custom_read_more_link() {
return 'Read More';
}
add_filter( 'the_content_more_link', 'custom_read_more_link' );

Modificare l’excerpt

function custom_more_excerpt( $more ) {
return '...';
}
add_filter( 'excerpt_more', 'custom_more_excerpt' );

Disabilitare Emoji

function disable_wp_emojicons() {
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );
add_filter( 'emoji_svg_url', '__return_false' );
}
add_action( 'init', 'disable_wp_emojicons' );
function disable_emojicons_tinymce( $plugins ) {
return is_array( $plugins ) ? array_diff( $plugins, array( 'wpemoji' ) ) : array();
}

Per vedere altri frammenti di codice dello stesso autore cliccate qui.

Rimuovere il numero di versione di WordPress (head)

remove_action(‘wp_head’, ‘wp_generator’);
Fonte

Rimuovere il generator sia dal documento HTML che dai feed

function html_remove_generator() {
return ”;
}
add_filter(‘the_generator’, ‘html_remove_generator’);

Fonte

Rimuovere il numero di versione da stili e script

//Removes version number from scripts and styles
function remove_src_version ( $src ) {global $wp_version;$version_str = ‘?ver=’.$wp_version;
$version_str_offset = strlen( $src ) – strlen( $version_str );if( substr( $src, $version_str_offset ) == $version_str ){
return substr( $src, 0, $version_str_offset );
} else{
return $src;
}
}
add_filter( ‘script_loader_src’, ‘remove_src_version’ );
add_filter( ‘style_loader_src’, ‘remove_src_version’ );

Fonte

boilerplate functions.php

/** ******** ******** ******** ******** ******** ******** ******** ********
* Faster than @import
*
* http://codex.wordpress.org/Customizing_the_Login_Form
*
*/function my_child_theme_scripts() {
wp_enqueue_style( 'parent-theme-css', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'my_child_theme_scripts' );/** ******** ******** ******** ******** ******** ******** ******** ********
* Customize Favicon
*
* http://codex.wordpress.org/Customizing_the_Login_Form
*
*/// Custom Function to Include
function favicon_link() {
echo '<link rel="shortcut icon" type="image/x-icon" href="' . get_stylesheet_directory() .'/favicons/favicon.ico" />' . "\n";
}
add_action( 'wp_head', 'favicon_link' );/** ******** ******** ******** ******** ******** ******** ******** ********
* Title: Remove Black Baar on mobile
*
* http://codex.wordpress.org/Roles_and_Capabilities#Subscriber
* http://docs.appthemes.com/tutorials/wordpress-check-user-role-function/
*
* @param string $role Role name.
* @param int $user_id (Optional) The ID of a user. Defaults to the current user.
* @return bool
*/
if (!current_user_can('edit_posts') || wp_is_mobile() ) {
show_admin_bar(false);
}/** ******** ******** ******** ******** ******** ******** ******** ********
* TITLE: Change admin e-mail to new users
* DESCRIPTION: auto-detect the server so you only have to enter the front/from
* half of the email address, including the @ sign
*
* http://snipplr.com/view/77687/wordpress-change-admin-email-to-new-users/
* http://miloguide.com/filter-hooks/wp_mail_from_name/
* http://premium.wpmudev.org/blog/wordpress-email-settings/
*
*/
function admin_email_filter_wp_mail_from($email){
/* start of code lifted from wordpress core, at
http://svn.automattic.com/wordpress/tags/3.4/wp-includes/pluggable.php */
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
if ( substr( $sitename, 0, 4 ) == 'www.' ) {
$sitename = substr( $sitename, 4 );
}
/* end of code lifted from wordpress core */
$myfront = "[email protected]";
$myback = $sitename;
$myfrom = $myfront . $myback;
return $myfrom;
}
add_filter("wp_mail_from", "admin_email_filter_wp_mail_from");/**
* TITLE: Change admin name to new users
* DESCRIPTION: enter the full name you want displayed alongside the email address
*
* http://miloguide.com/filter-hooks/wp_mail_from_name/
*/function admin_name_filter_wp_mail_from_name($from_name){
return "GuitarPick";
}
add_filter("wp_mail_from_name", "admin_name_filter_wp_mail_from_name");/** ******** ******** ******** ******** ******** ******** ******** ********
* TITLE: Beautify the Log-in Page
* DESCRIPTION: Make the Log-in Page PRetty
* 1: Change out Logo
* 2: Change out URL
* 3: Change out Title
*
* http://codex.wordpress.org/Customizing_the_Login_Form
*/
function my_login_logo() { ?>
<style type="text/css">
body.login div#login h1 a {
background-image: none;
}
</style>
<?php }
add_action( 'login_enqueue_scripts', 'my_login_logo' );function my_login_logo_url() {
return home_url();
}
add_filter( 'login_headerurl', 'my_login_logo_url' );function my_login_logo_url_title() {
return 'GuitarPick';
}
add_filter( 'login_headertitle', 'my_login_logo_url_title' );function no_errors_please(){
return 'Please Try Again';
}
add_filter( 'login_errors', 'no_errors_please' );

/** ******** ******** ******** ******** ******** ******** ******** ********
* TITLE: Remove unnecessary meta-data from your WordPress site
* DESCRIPTION: Over the years, wordpress has placed a lot of junk in their system.
*/
function remove_header_extra(){
//Are you editing your WordPress blog using your browser? Then you are not using a blog client
remove_action('wp_head', 'rsd_link');
// Windows Live Writer is (it�s another blog editing client
remove_action('wp_head', 'wlwmanifest_link');
//This announces that you are running WordPress and what version you are using.
remove_action('wp_head', 'wp_generator');
//URL shortening is sometimes useful, but this automatic ugly url in your header is useless.
remove_action('wp_head', 'wp_shortlink_wp_head');
// Display the links to the general feeds: Post and Comment Feed
remove_action('wp_head', 'feed_links', 2);
//// Display the links to the extra feeds such as category feeds
remove_action('wp_head', 'feed_links_extra', 3);
//Deprecated
remove_action('wp_head', 'index_rel_link');
// start link
remove_action('wp_head', 'start_post_rel_link', 10, 0);
// prev link
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
// Display relational links for the posts adjacent to the current post.
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
}
add_action('init', 'remove_header_extra');

/** ******** ******** ******** ******** ******** ******** ******** ********
* Title: WordPress Dashboard
* Description: Remove all those boxes on the WordPress Dashboard
*
* http://codex.wordpress.org/Function_Reference/remove_meta_box
*/
function remove_dashboard_meta() {
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_welcome_panel', 'dashboard', 'normal' );
}
add_action( 'admin_init', 'remove_dashboard_meta' );

/** ******** ******** ******** ******** ******** ******** ******** ********
* Title: Limit The Excerpt�s Word Count
*
* http://www.smashingmagazine.com/2011/12/07/10-tips-optimize-wordpress-theme/
*
*
*/
function ilc_excerpt_length( $length ){
return 10;
}
add_filter('excerpt_length', 'ilc_excerpt_length');

/** ******** ******** ******** ******** ******** ******** ******** ********
* Title: Remove Black Baar on mobile
*
* http://codex.wordpress.org/Roles_and_Capabilities#Subscriber
* http://docs.appthemes.com/tutorials/wordpress-check-user-role-function/
*
* @param string $role Role name.
* @param int $user_id (Optional) The ID of a user. Defaults to the current user.
* @return bool
*/
if (!current_user_can('edit_posts') || wp_is_mobile() ) {
show_admin_bar(false);
}

/** ******** ******** ******** ******** ******** ******** ******** ********
* Title: Redirect WordPress Feeds to Feedburner
*
* http://www.smashingmagazine.com/2011/12/07/10-tips-optimize-wordpress-theme/
*/
function feed_to_feedburner_redirect() {
if ( is_feed() && !preg_match('/feedburner|feedvalidator/i', $_SERVER['HTTP_USER_AGENT'])){
header('Location: http://feeds.feedburner.com/[THE_NAME_OF_YOUR_FEED]');
header('HTTP/1.1 302 Temporary Redirect');
}
}
//add_action('template_redirect', 'feed_to_feedburner_redirect');

/** ******** ******** ******** ******** ******** ******** ******** ********
* Title: Remove fields from woocommerce checkout
*
* [Override Billing](https://gist.github.com/mikejolley/1860056)
* [WooCommerce Plug-ins and hacks](http://wordpress.org/support/topic/woocommerce-remove-fields-on-edit-address)
*
*/

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
unset($fields['order']['order_comments']);
unset($fields['billing']['billing_phone']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_company']);
unset($fields['shipping']['shipping_company']);
unset($fields['shipping']['shipping_address_2']);

return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

/** ******** ******** ******** ******** ******** ******** ******** ********
* Title: Make Woocommerce Phone Field Not Required
*
* [Override Billing](https://gist.github.com/mikejolley/1860056)
* [WooCommerce Plug-ins and hacks](http://wordpress.org/support/topic/woocommerce-remove-fields-on-edit-address)
*
*/

function wc_npr_filter_phone( $address_fields ) {
$address_fields['billing_phone']['required'] = false;
return $address_fields;
}
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );

/** ******** ******** ******** ******** ******** ******** ******** ********
* Title: Show Featured Images In Feed
*
* http://www.smashingmagazine.com/2011/12/07/10-tips-optimize-wordpress-theme/
*
*
*/

function rss_post_thumbnail($content) {
global $post;
if( has_post_thumbnail($post->ID) )
$content = '<p>' . get_the_post_thumbnail($post->ID, 'thumbnail') . '</p>' . $content;
return $content;
}
add_filter('the_content_feed', 'rss_post_thumbnail');

/** ******** ******** ******** ******** ******** ******** ******** ********
*
*/

Utilizziamo cookies, anche di terze parti. Maggiori informazioni. Cookies Policy. Cookies Setting.