Useful Functions

WooCommerce E-Mail Activation

        /** * * * FUNCTION TO CHANGE LOGIN PAGE LOGO * * */            
        function my_login_logo_one() {  ?>      
        <style type="text/css">        
        body.login div#login h1 a {      
        background-image: url(/wp-content/themes/mytheme/images/logo.png);  
        background-size: 200px auto;
        width: 220px;
        height: 140px;
        //Add your own logo image in this url  
        padding-bottom: 10px;   }      
        </style>        
        <?php   }
        add_action( 'login_enqueue_scripts', 'my_login_logo_one' );
       
        /** * * * FUNCTION TO CHANGE LOGIN PAGE LOGO URL * * */        
        function my_login_logo_url() {    
        return get_bloginfo( 'url' );   }      
        add_filter( 'login_headerurl', 'my_login_logo_url' );  
        function my_login_logo_url_title() {          
        return 'Your Site Name and Info';       }      
        add_filter( 'login_headertitle', 'my_login_logo_url_title' );
       
        /** * * * FUNCTIONS TO ACHIEVE VERIFY MAIL & ACCOUNT AFTER REGISTER * * */
       
        // prevent user automatic log after register
function wc_registration_redirect( $redirect_to ) {
        wp_logout();
        wp_redirect( '/my-account/?q=');
        exit;
        }
        // after login check if this email is verified
        function wp_authenticate_user( $userdata ) {
                        $isActivated = get_user_meta($userdata->ID, 'is_activated', true);
                        if ( !$isActivated ) {
                                        $userdata = new WP_Error(
                                                                        'inkfool_confirmation_error',
                                                                        __( '<strong>ERROR:</strong> Your account has to be activated before you can login.
                                       You can resend by clicking <a href="/my-account/?u='.$userdata->ID.'">here</a> /
                                       <b>ERROR:<b> Your must validate your account in order to continue.
                                       To resend the validation from <a href="/my-account/?u='.$userdata->ID.'">here</a>', 'inkfool' )
                                                                        );
                        }
                        return $userdata;
        }
        // after register sendemail to verify account
        function my_user_register($user_id) {
                        // get user data
                        $user_info = get_userdata($user_id);
                        // create md5 code to verify later
                        $code = md5(time());
                        // make it into a code to send it to user via email
                        $string = array('id'=>$user_id, 'code'=>$code);
                        // create the activation code and activation status
                        update_user_meta($user_id, 'is_activated', 0);
                        update_user_meta($user_id, 'activationcode', $code);
                        // create the url
                        $url = get_site_url(). '/my-account/?p=' .base64_encode( serialize($string));
                        // basically we will edit here to make this nicer
                        $html = 'Please click the following links to activate / <b>Click the following link to validate your account: </b> <br/><br/> <a href="'.$url.'">'.$url.'</a>';
                        // send an email out to user
                        wc_mail($user_info->user_email, __('Your-Site.Gr - Activate your account'), $html);
        }
        function my_init(){
                        if(isset($_GET['p'])){
                                        $data = unserialize(base64_decode($_GET['p']));
                                        $code = get_user_meta($data['id'], 'activationcode', true);
                                        // check whether the code given is the same as ours
                                        if($code == $data['code']){
                                                        // update the db on the activation process
                                                        update_user_meta($data['id'], 'is_activated', 1);
                                                        wc_add_notice( __( '<strong>Success:</strong> Your account has been activated! ', 'inkfool' )  );
                                        }else{
                                                        wc_add_notice( __( '<strong>Error:</strong> Activation fails, please <a href="/contact">contact our administrator.</a>', 'inkfool' )  );
                                        }
                        }
                        if(isset($_GET['q'])){
                                        wc_add_notice( __( '<strong>Your account has to be activated before you can login. Please check your email.', 'inkfool' ) );
                        }
                        if(isset($_GET['u'])){
                                        my_user_register($_GET['u']);
                                        wc_add_notice( __( '<strong>Success:</strong> Your activation email has been resend. Please check your email.</b>', 'inkfool' ) );
                        }
        }

 

WordPress Functions for Custom Login

/** * Function for redirecting users on login based on user role */
function my_login_redirect( $url, $request, $user ){
        if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
                if( $user->has_cap( 'administrator' ) ) {
                        $url = home_url('/index.php');
                        } else {
                                $url = home_url('/index.php');
                                }
                                }
                                return $url;}add_filter('login_redirect', 'my_login_redirect', 10, 3 );
 
/** * Function for redirecting users on logout */
function logout_page() {
    $login_page  = home_url( '/login.php' );
    wp_redirect( $login_page . "" );
    exit;
}
add_action('wp_logout','logout_page');
 
/** * Function for redirecting users on failed login */
function login_failed() {
    $login_page  = home_url( '/login.php' );
    wp_redirect( $login_page . "?login=failed" );
    exit;
}
add_action( 'wp_login_failed', 'login_failed' );
 
/** * Function for redirecting users on empty user & pass fields */
function verify_username_password( $user, $username, $password ) {
    $login_page  = home_url( '/login.php' );
    if( $username == "" || $password == "" ) {
        wp_redirect( $login_page . "?login=empty" );
        exit;
    }
}
add_filter( 'authenticate', 'verify_username_password', 1, 3);

 

Allow Shortcodes in Text Widgets

add_filter('widget_text', 'do_shortcode');

 

Add Search Bar in Header

add_filter('wp_nav_menu_items','add_search_box_to_menu', 10, 2);
function add_search_box_to_menu( $items, $args ) {
    if( $args->theme_location == 'primary' )
        return $items.get_search_form();
 
    return $items;
}

 

Restrict Custom Post Type to Logged In Users

/** * Assume that we have a custom post type "advert" */
add_filter( 'wp', 'custom_post_type_redirect', 0 );
function custom_post_type_redirect( $content ) {
    global $post;
    if(
        (
            $post->post_type == 'advert'
            ||
            is_post_type_archive( 'advert' )
        )
        &&
        !is_user_logged_in()
    ) {
        wp_redirect( get_home_url() );
        exit;
    }
    return $content;
}
/** * If user is not Logged it, redirect user to home */

 

Get WP Post Content Without the First Image

/** *  A BEAUTIFUL SOLUTION I FOUND IN http://www.setupmyvps.com/how-to-get-post-content-without-the-first-image-in-wordpress */
// This one in the theme's functions
 function remove_first_image($the_string) {
    $count = substr_count($the_string, '<img');
    if ($count > 0) {
        $length = strlen($the_string);
        $imgBeg = strpos($the_string, '<img');
        $imgEnd = strpos($the_string, '>', $imgBeg);
        $chop = $length-$imgEnd;
        $new_content = substr($the_string, 0, $imgBeg) . substr($the_string, $imgEnd+1, $chop);
        $capcount = substr_count($new_content, '&#91;caption');
        if ($capcount > 0) {
            $length = strlen($new_content);
            $capBeg = strpos($new_content, '&#91;caption');
            $capClose = strpos($new_content, '/caption&#93;');
            $capEnd = strpos($new_content, '&#93;', $capClose);
            $chop = $length-$capEnd;
            $final_content = substr($new_content, 0, $capBeg) . substr($new_content, $capEnd+1, $chop);
            return $final_content;
        }
        else {
            return $new_content;
        }
    }
    else {
        return $the_string;
    }
}

// Calling the function from the loop
while ( have_posts() ) : the_post(); ?>
    <?php $old_content = get_the_content(); ?>
    <?php $new_content = remove_first_image($old_content); ?>
    <?php $display_content = apply_filters('the_content',$new_content); ?>
    <div class="your-content-class">
        <?php echo $display_content; ?>
    </div>

 

My Public Pastebin http://pastebin.com/u/meetsos
My Facebook Profile http://facebook.com/meet7sos