Showing posts with label add_action. Show all posts
Showing posts with label add_action. Show all posts

Saturday, 17 May 2014

customizing user action drop down in admin menu

We may need to customize the gravatar or provide the custom link to the profile page in the the user action drop down under the user name (right top corner) in admin bar, In order to do the customization we can use the wp_before_admin_bar_render action hook as shown below.

add_action( 'wp_before_admin_bar_render', 'custom_remove_my_account' );
if (!function_exists('custom_remove_my_account')) {
    function custom_remove_my_account() {
        global $wp_admin_bar;
        $wp_admin_bar->remove_node('user-actions');  /*Removed the default user action dropdown*/
        custom_admin_bar_my_account_menu($wp_admin_bar);
    }
}


/**

custom_admin_bar_my_account_menu method alters the user action dropdown
*/ 
if (!function_exists('custom_admin_bar_my_account_menu')) {
    function custom_admin_bar_my_account_menu( $wp_admin_bar ) {
        $user_id      = get_current_user_id();
        $current_user = wp_get_current_user();
        $profile_url  = get_permalink( 6 ); /*Here i just provided the custom page link as profile page*/

        if ( ! $user_id )
        return;

        $wp_admin_bar->add_group( array(
            'parent' => 'my-account',
            'id'     => 'user-actions',
            )
        );

        $user_info  = get_avatar( $user_id, 64 );
        $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";

        if ( $current_user->display_name !== $current_user->user_login )
        $user_info .= "<span class='username'>{$current_user->user_login}</span>";

        $wp_admin_bar->add_menu( array(
            'parent' => 'user-actions',
            'id'     => 'user-info',
            'title'  => $user_info,
            'href'   => $profile_url,
            'meta'   => array(
                'tabindex' => -1,
                ),
            )
        );
        $wp_admin_bar->add_menu( array(
            'parent' => 'user-actions',
            'id'     => 'edit-profile',
            'title'  => __( 'Edit My Profile' ),
            'href' => $profile_url,
            )
        );
        $wp_admin_bar->add_menu( array(
            'parent' => 'user-actions',
            'id'     => 'logout',
            'title'  => __( 'Log Out' ),
            'href'   => wp_logout_url(home_url()),
            )
        );
    }
}

Just paste the abovecode in either plugin files or in the functions.php ( theme file ).
 

Thursday, 4 July 2013

Integrate wordpress media uploader

We can use native wordpress Media uploader in our themes and plugins to upload the image from the media library.

Following code will include the required scripts and stylesheets for the Media uploader thickbox.

if(!function_exists('custom_uploader_scripts')) {
    function custom_uploader_scripts() {

        wp_enqueue_script('jquery');
        wp_enqueue_script('media-upload');
        wp_enqueue_script('thickbox');
    }
}
add_action('admin_print_scripts', 'custom_uploader_scripts');

if(!function_exists('custom_uploader_styles')) {
    function custom_uploader_styles() {
        wp_enqueue_style('thickbox');
    }
}

add_action('admin_print_styles', 'custom_uploader_styles');


Following lines will be used to display the  input text and the Browse button.


<input type="text" name="input_image_path" id="input_image_path" value=""/>
<input type="button" name="trigger_image_button" id="trigger_image_button" value="Browse" />
Following code can be used in the separate JS file or can be used in the same php file. This will trigger the media uploader and will insert the image url into the text box on image selection.



<script type="text/javascript">

 (function($){

    $('#trigger_image_button').click(function() {
        tb_show('', 'media-upload.php?referer=custom-upload&amp;type=image&TB_iframe=true');
       
window.send_to_editor = function(html) {
        imgurl = $('img',html).attr('src');
        $('#input_image_path').val(imgurl);
        tb_remove();
        return false;
    });

})(jQuery);

</script>




In media uploader, by default the save button text will be "Insert into Post", we can customize the text of the button through the following code


if(!function_exists('custom_options_setup')) {
    function custom_options_setup() {
        global $pagenow;
        if ('media-upload.php' == $pagenow || 'async-upload.php' == $pagenow) {
            // Now we'll replace the 'Insert into Post Button inside Thickbox'
            add_filter( 'gettext', 'replace_thickbox_text' , 1, 2 );
          
        }
    }
}


add_action( 'admin_init', 'custom_options_setup' );

if(!function_exists('replace_thickbox_text')) {
    function replace_thickbox_text($translated_text, $text ) {  
        if ( 'Insert into Post' == $text ) {
            $
referer = strpos( wp_get_referer(), 'custom-upload' );
           
/* Here the custom-upload text is appended to the url through above JS code. Its to make filter to apply only for our page without disturbing the other media uploaders. */
            if ( $referer != '' ) {
                return __('Use this Image', 'custom');
            }
        }

        return $translated_text;
    }
}



Friday, 17 May 2013

Custom validation in user profile page

We can perform custom validation in user profile page. We can perform the validation through user_profile_update_errors hook.
 
if (!function_exists('custom_user_contact_validate')) {

    function custom_user_contact_validate(&$errors, $update = null, &$user = null) {

        if (!empty($_POST["action"]) && ($_POST["action"] == "adduser" || $_POST["action"] == "createuser"))
            return $errors;
      
        if (empty($_REQUEST['first_name'])) {
            $errors->add('Invalid Data', '<strong>ERROR</strong>: Please Enter your First Name');
        }

/*Here the field first name has been validated*/
    }

}
add_action('user_profile_update_errors', 'custom_user_contact_validate', 5);



Note: The validation added through this Hook will works while editing the user's profile page. Fields wont be validated while creating a user.

Thursday, 16 May 2013

Custom menu in admin bar

Our plugins may need to add a menu item for pointing to settings page in admin bar.  wp_before_admin_bar_render filter helps us to do that.

if(!function_exists('custom_admin_bar_menu')){
    function custom_admin_bar_menu() {
        global $wp_admin_bar;
            $wp_admin_bar->add_menu( array(
            'id' => 'my_settingspage',
            'title' => __('My settings'),
            'href' => admin_url('/plugingsettingspage.php')
        ) );
    }
}
add_action( 'wp_before_admin_bar_render', 'custom_admin_bar_menu' );



We can also make it as child to existing menu by adding the menu-id as a parent to that menu

Wednesday, 8 May 2013

Admin bar for non logged users

Its possible to display the admin bar for the non-logged-in users it can be easily done by using the show_admin_bar filter and admin_bar_menu hook.

Just paste the following code in either plugin files or in the functions.php ( theme file ).

if(!function_exists('custom_login_adminbar')){
    function custom_login_adminbar( $wp_admin_bar) {


        if ( !is_user_logged_in() )
        $wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );

/*Here you can add your own menus*/
    }


}
add_action( 'admin_bar_menu', 'custom_login_adminbar' );
add_filter( 'show_admin_bar', '__return_true' , 1000 );


 

By default the admin bar will come with wordpress documentation link and search box. You can remove the admin bar items by using wp_before_admin_bar_render filter. Following code will remove the wordpress documentation link for non-logged-in user.

Just paste the following code in either plugin files or in the functions.php ( theme file ).

    if(!function_exists('custom_admin_bar_render')) {
        function custom_admin_bar_render() {


            if ( !is_user_logged_in() ) {
                global $wp_admin_bar;
                $wp_admin_bar->remove_menu('wp-logo');
            }


        }
    }
add_action( 'wp_before_admin_bar_render', 'custom_admin_bar_render' );