WordPress, Function

WordPress: Function.php – Snippets

Inhalt

Löscht den Autor aus der Oembed-Vorschau

/**
 * Löscht den Autor aus der Oembed-Vorschau
 */
add_filter( 'oembed_response_data', 'disable_embeds_filter_oembed_response_data_' );
function disable_embeds_filter_oembed_response_data_( $data ) {
    unset($data['author_url']);
    unset($data['author_name']);
    return $data;
}

Ändern des Titels einer Archivseite

Standardmäßig ist der Titel der Archivseite ein­fach der Name der Kategorie oder des Tags.

add_filter( 'get_the_archive_title', function( $title ) {
if ( is_category( 'CATEGORY NAME' ) ) {
$title = 'YOUR CUSTOM TITLE';
}
return $title;
}, 50 );
// Ändern des Titels einer Archivseite 
function new_cpt_archive_title($title){

    if ( is_post_type_archive('posts') ){
        $title = 'Wissensdatenbank - ' . get_bloginfo('name');
        return $title;
    }
    return $title;
} 
add_filter( 'pre_get_document_title', 'new_cpt_archive_title', 9999 );

 

Blockiert Emoji's

/**
 * Disable the emoji's
 */
function disable_emojis() {
 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_action( 'admin_print_styles', 'print_emoji_styles' ); 
 remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
 remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); 
 remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
 add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
 add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
}
add_action( 'init', 'disable_emojis' );
/**
 * Remove emoji CDN hostname from DNS prefetching hints.
 *
 * @param array $urls URLs to print for resource hints.
 * @param string $relation_type The relation type the URLs are printed for.
 * @return array Difference betwen the two arrays.
 */
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
 if ( 'dns-prefetch' == $relation_type ) {
 /** This filter is documented in wp-includes/formatting.php */
 $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );

$urls = array_diff( $urls, array( $emoji_svg_url ) );
 }

return $urls;
}
/**
 * Filter function used to remove the tinymce emoji plugin.
 * 
 * @param array $plugins 
 * @return array Difference betwen the two arrays
 */
function disable_emojis_tinymce( $plugins ) {
 if ( is_array( $plugins ) ) {
 return array_diff( $plugins, array( 'wpemoji' ) );
 } else {
 return array();
 }
}

Disable Gutenberg Editor

// Fully Disable Gutenberg editor.
add_filter('use_block_editor_for_post_type', '__return_false', 10);
// Don't load Gutenberg-related stylesheets.
add_action( 'wp_enqueue_scripts', 'remove_block_css', 100 );
function remove_block_css() {
wp_dequeue_style( 'wp-block-library' ); // WordPress core
wp_dequeue_style( 'wp-block-library-theme' ); // WordPress core
wp_dequeue_style( 'wc-block-style' ); // WooCommerce
wp_dequeue_style( 'storefront-gutenberg-blocks' ); // Storefront theme
}

add_filter( 'wpseo_debug_markers', '__return_false' );

Beiträge im Adminbereich nach dem Autor filterbar machen

/**
* Beiträge im Adminbereich nach dem Autor filterbar.
*/
add_action('restrict_manage_posts', 'hix_filter_by_author');
function hix_filter_by_author() {
        $params = array(
            'name' => 'author',
            'role__in' => array('author','editor','administrator')
        );
        if ( isset($_GET['user']) ) {
            $params['selected'] = $_GET['user'];
    }
    wp_dropdown_users( $params );
}

Yoast SEO-Filter  im Backend entfernen

/*
 * Yoast SEO-Filter entfernen
 */
function custom_remove_yoast_seo_admin_filters() {
    global $wpseo_meta_columns;
    if ($wpseo_meta_columns) {
        remove_action('restrict_manage_posts', array($wpseo_meta_columns, 'posts_filter_dropdown'));
        remove_action('restrict_manage_posts', array($wpseo_meta_columns, 'posts_filter_dropdown_readability'));
    }
}
add_action('admin_init', 'custom_remove_yoast_seo_admin_filters', 20);

Deaktivieren von Kommentaren und Trackbacks in Post Types

// Disable support for comments and trackbacks in post types
function df_disable_comments_post_types_support() {
    $post_types = get_post_types();
    foreach ($post_types as $post_type) {
        if(post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
}
add_action('admin_init', 'df_disable_comments_post_types_support');

Schließen von Kommentaren im Front-End

// Close comments on the front-end
function df_disable_comments_status() {
    return false;
}
add_filter('comments_open', 'df_disable_comments_status', 20, 2);
add_filter('pings_open', 'df_disable_comments_status', 20, 2);

Verstecken von Kommentaren

// Hide existing comments
function df_disable_comments_hide_existing_comments($comments) {
    $comments = array();
    return $comments;
}
add_filter('comments_array', 'df_disable_comments_hide_existing_comments', 10, 2);
// Remove comments page in menu
function df_disable_comments_admin_menu() {
    remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'df_disable_comments_admin_menu');

// Redirect any user trying to access comments page
function df_disable_comments_admin_menu_redirect() {
    global $pagenow;
    if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url()); exit;
    }
}
add_action('admin_init', 'df_disable_comments_admin_menu_redirect');

// Remove comments metabox from dashboard
function df_disable_comments_dashboard() {
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'df_disable_comments_dashboard');

// Remove comments links from admin bar
function df_disable_comments_admin_bar() {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
}
add_action('init', 'df_disable_comments_admin_bar');

function remove_admin_menus() {
        remove_menu_page( 'edit.php' );
    }
add_action( 'admin_menu', 'remove_admin_menus' );

 

Adminbar ausblenden wenn der Nutzer kein Admin oder Super-Admin ist

// hide admin bar if user is not admin or super admin
if (!current_user_can('manage_options')) {
show_admin_bar( false );
}

 jQuery Migrate Script abschalten

jQuery Migrate ist ein Script, das für eine Abwärtskompatibilität älte­rer jQuery-Anwendungen sor­gen soll. Die »nor­ma­le« und moder­ne jQuery Version unter­stützt nicht mehr alle alten Anwendungen. Dieser Fall wird mit Sicherheit nicht mehr als 5% aller WordPress-Websites betref­fen, und doch wird das nicht gera­de klei­ne Script stan­dard­mä­ßig von WordPress gela­den. So schal­test du es ganz ein­fach ab:

<?php
/**
* Dequeue jQuery Migrate Script in WordPress.

*/
if ( ! function_exists( 'evolution_remove_jquery_migrate' ) ) :

function evolution_remove_jquery_migrate( &$scripts) {
    if(!is_admin()) {
        $scripts->remove( 'jquery');
        $scripts->add( 'jquery', false, array( 'jquery-core' ), '1.12.4' );
    }
}
add_filter( 'wp_default_scripts', 'evolution_remove_jquery_migrate' );
endif;

Eigenes Login-Logo mit eigener Hintergrundfarbe

<?php
/**
 * Ein neues Logo für den Adminbereich und eine eigene Hintergrundfarbe
 * @author Andreas Hecht
 */
function ah_login_logo() {
?>
 <style type="text/css">
 #login h1 a, .login h1 a {
 background-image: url(<?php echo get_stylesheet_directory_uri(); ?>/images/dein-logo.png);
 margin-bottom: 0;
 background-size: 180px;
 height: 180px;
 width: 180px;
 margin-left: auto;
 margin-right: auto;
 border-radius: 50%;
 }
 body.login {background-color: #0073bf;} .login #backtoblog a, .login #nav a {color: #fff !important}
 </style>
<?php }
add_action( 'login_enqueue_scripts', 'ah_login_logo' );

BENUTZERANMELDUNG NUR MIT E‑MAIL UND PASSWORT

Seit der WordPress Version 4.5 ist eine Benutzeranmeldung auch mit einer E‑Mail-Adresse und dem Passwort mög­lich. Du Dich mit die­sem Code aus­schließ­lich mit E‑Mail und Passwort anmelden.

<?php
// Ab hier kopieren
//WordPress Authentifikation löschen
remove_filter('authenticate', 'wp_authenticate_username_password', 20);
// Neue Authentifikation setzen - Anmelden nur mit E-Mail und Passwort
add_filter('authenticate', function($user, $email, $password){
    //Check for empty fields
        if(empty($email) || empty ($password)){
            //create new error object and add errors to it.

            $error = new WP_Error();
            if(empty($email)){ //No email
                $error->add('empty_username', __('<strong>ERROR</strong>: Email field is empty.'));
            }
            else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //Invalid Email
                $error->add('invalid_username', __('<strong>ERROR</strong>: Email is invalid.'));
            }
            if(empty($password)){ //No password
                $error->add('empty_password', __('<strong>ERROR</strong>: Password field is empty.'));
            }
            return $error;
        }
        //Check if user exists in WordPress database
        $user = get_user_by('email', $email);
        //bad email
        if(!$user){
            $error = new WP_Error();
            $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
            return $error;
        }
        else{ //check password
            if(!wp_check_password($password, $user->user_pass, $user->ID)){ //bad password
                $error = new WP_Error();
                $error->add('invalid', __('<strong>ERROR</strong>: Either the email or password you entered is invalid.'));
                return $error;
            }else{
                return $user; //passed
            }
        }
}, 20, 3);

Custom Post Type Archiv Title ändern

/** 
 * Archive Title
 */
function cpt_jobs($title){

    if ( is_post_type_archive('jobs') ){
        $title = 'Aktuelle Jobangebote | you Hamburg';
        return $title;
    }
    return $title;
} 
add_filter( 'pre_get_document_title', 'cpt_jobs', 9999 );

Stop Contact Form 7 from loading on all pages

// ==============================================================
// Stop Contact Form 7 from loading on all pages
// ==============================================================

add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );

add_action('wp_enqueue_scripts', 'load_wpcf7_scripts');
function load_wpcf7_scripts() {
  if ( is_page( array( 'kontakt', 'kuechenplanung-zu-hause', 'kuechenplanung-im-kuechenstudio', 'kuechenkonfigurator', 'service-anfragen', 'kundenrezension', 'ausbildung', 'initiativbewerbung', 'service', 'allgemeine-informationen', 'formulare', '28848', 'meda-store', 'falkensee', '28850', 'meda-store/falkensee', '39708', 'meda-store/muelheim-an-der-ruhr', 'muelheim-an-der-ruhr', 'beispiel', 'kuechen/kuechenwelten/outdoorkuechen', '49026', 'rosbach-vor-der-hoehe' ) ) ) {
    if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
      wpcf7_enqueue_scripts();
    }
    if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
      wpcf7_enqueue_styles();
    }
  }
}
add_action('wp_enqueue_scripts', 'load_wpcf_scripts');
function load_wpcf_scripts() {
  if ( is_single() && 'stellenangebote' == get_post_type() ) {
    if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
      wpcf7_enqueue_scripts();
    }
    if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
      wpcf7_enqueue_styles();
    }
  }
}