jacodelucia
1/26/2015 - 9:21 AM

WP - Filters

WP - Filters

<?php
/* => http://codex.wordpress.org/Plugin_API/Action_Reference */

// FACEBOOK OPEN GRAPH IN HEAD

add_action( 'wp_head', 'wp_head_example' );
 
function wp_head_example() {
    global $post;
     
    // default image
    $site_logo = get_stylesheet_directory_uri() . '/images/logo.png';
     
    // homepage
    if ( is_home() ) {
        echo '<meta property="og:type" content="website" />';
        echo '<meta property="og:url" content="' . get_bloginfo( 'url' ) . '" />';
        echo '<meta property="og:title" content="' . esc_attr( get_bloginfo( 'name' ) ) . '" />';
        echo '<meta property="og:image" content="' . $site_logo . '" />';
        echo '<meta property="og:description" content="' . esc_attr( get_bloginfo( 'description' ) ) . '" />';
    }
     
    // single post or page
    elseif ( is_singular() ) {
        echo '<meta property="og:type" content="article" />';
        echo '<meta property="og:url" content="' . get_permalink() . '" />';
        echo '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />';
        if ( has_post_thumbnail( $post->ID ) ) {
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
            echo '<meta property="og:image" content="' . esc_attr( $image[0] ) . '" />';
        } else
            echo '<meta property="og:image" content="' . $site_logo . '" />';
        echo '<meta property="og:description" content="' . esc_attr( get_the_excerpt() ) . '" />';
    }
}

//
 
add_action( 'init', 'init_example' );
 
function init_example() {
    global $wp_rewrite;
    $wp_rewrite->author_base = 'profile';
    $wp_rewrite->search_base = 'find';
    $wp_rewrite->pagination_base = 'p';
}

//

add_filter( 'manage_posts_columns', 'manage_posts_columns_example', 5 );

function manage_posts_columns_example( $columns ) {
    $columns['post_attachments'] = __( 'Attached', 'theme-name' );
    return $columns;
}

/************************************************************************************************/
/* SCRIPTS & STYLES */
/************************************************************************************************/

add_action( 'wp_enqueue_scripts', 'wp_enqueue_scripts_example' );
 
function wp_enqueue_scripts_example() {
    // you can enqueue scripts...
    wp_enqueue_script( 'my-script', get_stylesheet_directory_uri() . '/scripts/my-script.js' );
    // ...and styles, too!
    wp_enqueue_style( 'my-style', get_stylesheet_directory_uri() . '/styles/my-style.css' );
}

//

add_action( 'login_enqueue_scripts', 'login_enqueue_scripts_example' );
function login_enqueue_scripts_example() {
 
    echo '<style type="text/css">'
	        . '#login h1 a {'
	            . 'background-image: url(' . get_bloginfo( 'template_directory' ) . '/images/login-logo.png);'
	            . 'padding-bottom: 30px;'
	        . '}'
	    . '</style>';
	     
}

/************************************************************************************************/
/* FOOTER */
/************************************************************************************************/

add_action( 'wp_footer', 'wp_footer_example' );
 
function wp_footer_example() {
    $stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
        get_num_queries(),
        timer_stop( 0, 3 ),
        memory_get_peak_usage() / 1024 / 1024
    );
    if( current_user_can( 'manage_options' ) ) {
        echo "<!-- {$stat} -->";
    }
}

//

add_action( 'get_footer', 'get_footer_example' );
 
function get_footer_example( $name ) {
    if ( 'new' == $name ) { ?>
        <script>
            (function( $ ) {
                //put all your jQuery goodness in here.
            })( jQuery );
        </script>
    <?php
    }
}


/************************************************************************************************/
/* WIDGETS */
/************************************************************************************************/

add_action( 'widgets_init', 'widgets_init_example' );
 
function widgets_init_example() {
    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' );
}

/************************************************************************************************/
/* ADMIN */
/************************************************************************************************/

add_action( 'admin_head', 'admin_head_example' );
 
function admin_head_example() {
    echo '<link rel="shortcut icon" type="image/x-icon" href="' . get_bloginfo('template_directory') . '/images/admin-favicon.ico" />';
}

//

add_action( 'admin_notices', 'admin_notices_example' );
 
function admin_notices_example() {  
    echo '<div class="error">
            <p>We are performing website maintenance. Please don\'t make any changes in your posts until further notice!</p>
          </div>';
}

//

add_action( 'admin_init', 'admin_init_example', 1 );
 
function admin_init_example() {
    if ( ! current_user_can( 'manage_options' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
        wp_redirect( site_url() ); 
        exit;
    }
}

// Retirer des pages de l'admin en fonction des capabilities

if ( ! current_user_can( 'manage_options' ) ) {
    add_action( 'admin_menu', 'admin_menu_example' );
}
 
function admin_menu_example() {
  remove_menu_page( 'index.php' );                  //Dashboard
  remove_menu_page( 'edit.php' );                   //Posts
  remove_menu_page( 'upload.php' );                 //Media
  remove_menu_page( 'edit.php?post_type=page' );    //Pages
  remove_menu_page( 'edit-comments.php' );          //Comments
  remove_menu_page( 'themes.php' );                 //Appearance
  remove_menu_page( 'plugins.php' );                //Plugins
  remove_menu_page( 'users.php' );                  //Users
  remove_menu_page( 'tools.php' );                  //Tools
  remove_menu_page( 'options-general.php' );        //Settings
   
}

// Ajouter info dans admin bar

add_action( 'wp_before_admin_bar_render', 'wp_before_admin_bar_render_example' ); 
 
function wp_before_admin_bar_render_example() {
    global $wp_admin_bar;
    $wp_admin_bar->add_node( array(
        'id'    => 'contact-designer',
        'title' => 'Contact Designer',
        'href'  => 'http://barisunver.com.tr/contact/',
        'meta'  => array( 'target' => '_blank' )
    ) );
}

//

add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts_example' );
function admin_enqueue_scripts_example( $hook ) {
 
    if( 'edit.php' != $hook ) {
        return;
    }
     
    wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . 'myscript.js' );
}

// Image à la une requise

add_action( 'save_post', 'save_post_example' );
add_action( 'admin_notices', 'admin_notices_example' );
function save_post_example( $post_id ) {
 
    // change to any custom post type 
    if( get_post_type( $post_id ) != 'post' ) {
        return;
    }
      
    if ( ! has_post_thumbnail( $post_id ) ) {
     
        // set a transient to show the users an admin message
        set_transient( 'has_post_thumbnail', 'no' );
         
        // unhook this function so it doesn't loop infinitely
        remove_action( 'save_post', 'wpds_check_thumbnail' );
         
        // update the post set it to draft
        wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft' ) );
        add_action( 'save_post', 'wpds_check_thumbnail' );
         
    } else {
        delete_transient( 'has_post_thumbnail' );
    }
     
}
  
function admin_notices_example() {
 
    // check if the transient is set, and display the error message
    if ( get_transient( 'has_post_thumbnail' ) == 'no' ) {
        echo '<div id="message" class="error"><p>You must select a featured image for your post.</p></div>';
        delete_transient( 'has_post_thumbnail' );
    }
  
}

// Admin Notice

add_action( 'admin_notices', 'admin_notices_example' );
function admin_notices_example() {
 
    // check if the transient is set, and display the error message
    if ( get_transient( 'has_post_thumbnail' ) == 'no' ) {
        echo '<div id="message" class="error"><p>You must select a featured image for your post.</p></div>';
    }
  
}

// Masquer la barre admin en fonction des capabilities 

add_action( 'set_current_user', 'set_current_user_example' );
function set_current_user_example() {
 
    if ( ! current_user_can( 'edit_posts' ) ) {
        show_admin_bar( false );
    }
     
}

// AJOUTER UNE COLONNE DANS LES MEDIAS

add_filter( 'manage_media_columns', 'manage_media_columns_example' );
add_action( 'manage_media_custom_column', 'manage_media_custom_column_example', 10, 2 );
 
function manage_media_columns_example( $columns ) {
    $columns[ 'wps_post_attachments_id' ] = __( 'ID', 'theme-name' );
    return $columns;
}
 
function manage_media_custom_column_example( $column_name, $attachment_id ){
 
    if ( 'wps_post_attachments_id' === $column_name ) {
        echo $attachment_id;
    }
}


/************************************************************************************************/
/* LOGIN */
/************************************************************************************************/

add_action( 'wp_authenticate', 'wp_authenticate_example' );

// Permet de se logger avec son email
function wp_authenticate_example( $username ) {
    $user = get_user_by( 'email', $username );
    if ( ! empty( $user->user_login ) ) {
        $username = $user->user_login;
    }
    return $username;
}

//

add_action( 'login_form', 'login_form_example' );
 
function login_form_example() {
    echo '<p><strong>Remember:</strong> You must enter your username, not your email address!</p>';
}

/************************************************************************************************/
/* LOGOUT */
/************************************************************************************************/

add_action( 'wp_logout', 'wp_logout_example' );
 
function wp_logout_example() {
    wp_redirect( home_url() );
    exit();
}

/************************************************************************************************/
/* USERS */
/************************************************************************************************/

// DELETE USER

add_action( 'delete_user', 'delete_user_example' );
 
function delete_user_example( $user_id ) {
    global $wpdb;
    $user_obj = get_userdata( $user_id );
    $email = $user_obj->user_email;
    $headers = 'From: ' . get_bloginfo( 'name' ) . ' <' . get_bloginfo( 'admin_email' ) . '>' . "\r\n";
    $subject = 'You are being deleted, brah';
    $message = 'Your account at ' . get_bloginfo( 'name' ) . ' has been deleted because of your totally uncool behaviors.';
    wp_mail( $email, $subject, $message, $headers );
}

// 

add_action( 'profile_update', 'profile_update_example' );
 
function profile_update_example( $user_id ) {
    $site_url = get_bloginfo( 'name' );
    $user_info = get_userdata( $user_id );
    $user_name = $user_info->display_name;
    $user_email = $user_info->user_email;
    $subject = "Profile updated";
    $message = "Hello $user_name,\n\nYour profile has been updated! Please contact us if you're not the one who changed your profile.\n\nThank you for visiting $site_name.";
    wp_mail( $user_email, $subject, $message );
}

// Ajouter un colonne aux Users dans l'admin

add_action( 'manage_users_custom_column', 'manage_users_custom_column_example', 10, 3 );
add_filter( 'manage_users_columns', 'manage_users_columns_example' );
 
// create a new column named "Registration Date"
function manage_users_columns_example( $columns ) {
    $columns['user_registered'] = __( 'Registration Date', 'theme-name' );
    return $columns;
}
 
// fill the column cells with the registration dates
function manage_users_custom_column_example( $value, $column_name, $user_id ) {
    if ( 'user_registered' == $column_name ) {
        $userdata = get_userdata( $user_id );
        return $userdata->user_registered;
    }
}

/************************************************************************************************/
/* SEARCH */
/************************************************************************************************/

// Include custom post type In search results

add_action( 'pre_get_posts', 'pre_get_posts_example' );
function pre_get_posts_example( $query ) {
 
    if ( ! is_admin() && $query->is_main_query() ) {
     
        if ( $query->is_search ) {
            $query->set( 'post_type', array( 'post', 'movie' ) );
        }
 
    }
}