Funzioni personalizzate WordPress
Versione del 30 mar 2023 alle 07:35 di Andrea (discussione | contributi)
Indice
Vedi anche ...
Plugin
Plugin WordPress#Code Snippets permette di inserire funzioni personalizzate di codice php, js, css, e altro.
Email utente facoltativa
Ecco il codice per rendere l'email facoltativa nella form di inserimento dell'utente nell'are adi amministrazione
// This will suppress empty email errors when submitting the user form
add_action('user_profile_update_errors', 'my_user_profile_update_errors', 10, 3 );
function my_user_profile_update_errors($errors, $update, $user) {
$errors->remove('empty_email');
}
// This will remove javascript required validation for email input
// It will also remove the '(required)' text in the label
// Works for new user, user profile and edit user forms
add_action('user_new_form', 'my_user_new_form', 10, 1);
add_action('show_user_profile', 'my_user_new_form', 10, 1);
add_action('edit_user_profile', 'my_user_new_form', 10, 1);
function my_user_new_form($form_type) {
?>
<script type="text/javascript">
jQuery('#email').closest('tr').removeClass('form-required').find('.description').remove();
// Uncheck send new user email option by default
<?php if (isset($form_type) && $form_type === 'add-new-user') : ?>
jQuery('#send_user_notification').removeAttr('checked');
<?php endif; ?>
</script>
<?php
}
Target _blank
Per pre-selezionare il checkbox per impostare il target _blank per i link inseriti negli articoli e pagine
/**
* Select target _blank by default.
*
* Outputs javascript that hooks into the WordPress link dialog
* and sets the target _blank checkbox to be selected by default.
*
* @return null
*/
function default_target_blank() {
?>
<script>
jQuery(document).on( 'wplink-open', function( wrap ) {
if ( jQuery( 'input#wp-link-url' ).val() <= 0 )
jQuery( 'input#wp-link-target' ).prop('checked', true );
});
</script>
<?php
}
add_action( 'admin_footer-post-new.php', 'default_target_blank', 10, 0 );
add_action( 'admin_footer-post.php', 'default_target_blank', 10, 0 );
Leggi l'intero articolo ...
Nella lista degli articoli, viene inserita solo l'anteprima del contenuto.
Questo codice permette di personalizzare il pulsante per leggere tutto l'articolo:
/** Leggi articolo completo */
function new_excerpt_more($more) {
global $post;
return '...<p><a class="moretag" href="'. get_permalink($post->ID) . '">Leggi d\'articolo completo ...</a></p>';
}
add_filter('excerpt_more', 'new_excerpt_more', 99);