Manage Dashboard Columns

December 18, 2014 (6 years ago)
WordPress, Functions

In the latest versions of WordPress, the number of columns showing in the dashboard are automatically generated based on your screen size. That works for many situations, but you may want to control the number of columns.

For example, if  you only have 1 or 2 items showing on your dashboard, it looks awkward on a large screen. So, here is a function that allows you to control the default and max number of columns:

function lt_dashboard_columns() {
    add_screen_option(
        'layout_columns',
        array(
            'max'     => 2,
            'default' => 1
        )
    );
}
add_action( 'admin_head-index.php', 'lt_dashboard_columns' );

 

Allow HTML in Excerpts

December 9, 2014 (6 years ago)
WordPress, PHP, Functions

By default, WordPress strips all HTML tags from excerpts. To allow them, simply replace the filter that creates the excerpt from your content.

First, copy the default excerpt filter from wp-includes/formatting.php:

function wp_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace('\]\]\>', ']]>', $text);
        $text = strip_tags($text);
        $excerpt_length = 55;
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words)> $excerpt_length) {
            array_pop($words);
            array_push($words, '[...]');
            $text = implode(' ', $words);
        }
    }
    return $text;
}

 By changing the strip_tags parameters, you can allow whatever tags you want:

Also be sure to change the name of the function. Copy this to your functions.php file.

function lt_html_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace('\]\]\>', ']]>', $text);
        /*just add all the tags you want to appear in the excerpt --
        be sure there are no white spaces in the string of allowed tags */
        $text = strip_tags($text,'<p><br><b><a><em><strong>');
        /* you can also change the length of the excerpt here, if you want */
        $excerpt_length = 55; 
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words)> $excerpt_length) {
            array_pop($words);
            array_push($words, '[...]');
            $text = implode(' ', $words);
        }
    }
    return $text;
}

 Then, replace the default filter with your new one in functions.php:

/* remove the default filter */
remove_filter('get_the_excerpt', 'wp_trim_excerpt');

/* now, add your own filter */
add_filter('get_the_excerpt', 'lt_html_excerpt');

 

Replace “Howdy” in Admin Bar

October 20, 2014 (6 years ago)
WordPress, Functions

Use this function to replace “Howdy, $username” with “Welcome, $username”:

    function howdy_message($translated_text, $text, $domain) {  
        $new_message = str_replace('Howdy', 'Welcome', $text);  
        return $new_message;  
    }  
    add_filter('gettext', 'howdy_message', 10, 3);  

 Thanks to Thoriq Firdaus! Replayed from: http://www.hongkiat.com/blog/wordpress-howdy-customized/, where you’ll also find a clever function to customize the message for special occasions.

Custom Admin Page

July 1, 2014 (6 years ago)
WordPress, PHP, Functions

I use these for those occasions when I need to store specialized information in non-WordPress tables. (Such as orders in a custom shopping cart I built.)

It’s also handy for adding informational or instructional pages to the Dashboard.

This adds the page to the WP admin menu (put in your functions.php file):

// admin page for orders
add_action( 'admin_menu', 'lt_orders' );

function lt_orders() {
    add_menu_page( 'Orders', 'Orders',  'manage_options',  'lt-orders',   'lt_render', '', '3'   );
}
function lt_render() {
    global $title;
    print '<div class="wrap">';
    print "<h2>$title</h2>";
    $file = plugin_dir_path( __FILE__ ) . "lt_orders.php";
    if ( file_exists( $file ) )
        require $file;
    print '</div>';
}

 The page itself ( lt_orders.php ) resides in the theme folder, and is a simple PHP page, with any functionality you like. It doesn’t need any header or footer calls because it is rendered inside the admin.

Custom Logo on Login Page

July 1, 2014 (6 years ago)
WordPress, Functions

It’s a nice touch to customize for the client. We can also change the link to the site home page, and the title to the site name.

Put this in your functions.php file.

function lt_loginlogo() {
	echo '<style type="text/css">
	.login h1 a { background-image: url('.get_bloginfo('template_directory').'/images/custom-login-logo.png) !important; }
	</style>';
}
add_action( 'login_head', 'lt_loginlogo' );

function lt_loginurl( $url ) {
	$url = home_url( '/' );
	return $url;
}
add_filter( 'login_headerurl', 'lt_loginurl' );

function lt_logintitle( $title ) {
	$title = get_bloginfo( 'name' );
	return $title;
}
add_filter( 'login_headertitle', 'lt_logintitle' );

Check the path and name for your image, and make sure it’s no larger than 80px x 80px.

Front End Login Form

June 20, 2014 (6 years ago)
WordPress, PHP, Functions

So many times clients (and their members) want to login on the front end. Simple to add this in the header, or sidebar. Style as you wish.

Put this in your header template (or anywhere else you want the form to appear.)

<?php if (!is_user_logged_in()) { ?>
	<form action="<?php echo get_option('home'); ?>/wp-login.php" method="post">
	<input type="text" name="log" id="log" value="<?php if (isset($user_login)) echo esc_html(stripslashes($user_login), 1) ?>" size="20" />
	<input type="password" name="pwd" id="pwd" size="20" />
	<input type="submit" name="submit" value="Login" />
	<input name="rememberme" id="rememberme" type="hidden" checked="checked" value="forever" />
	<input type="hidden" name="redirect_to" value="<?php echo home_url(); ?>" />
	<a href="<?php echo wp_lostpassword_url( home_url() ); ?>">Forgot password or username?</a>
	</form>
<?php } else { 
	global $current_user;
	get_currentuserinfo(); ?>				
	<p>Welcome, <?php if($current_user->user_firstname) echo $current_user->user_firstname; else echo $current_user->display_name; ?>!</p>
	<a class="button" href="<?php echo wp_logout_url( home_url() ); ?>">Logout</a>
<?php } ?>

 

Add a Custom Avatar

June 20, 2014 (6 years ago)
WordPress, Functions

So the client has a membership site, and wants to have a custom default avatar branded to their site, to replace the boring Mystery Man, or other strange concoctions:

Put this code in your functions.php file.

//custom avatar
function dw_addgravatar( $avatar_defaults ) {
	$myavatar = get_bloginfo('template_directory') . '/images/avatar.jpg';
	$avatar_defaults[$myavatar] = 'New Avatar';
	return $avatar_defaults;
	}
add_filter( 'avatar_defaults', 'dw_addgravatar' );

 Then create your image and upload the file to wp-content/themes/yourtheme/images/avatar.jpg (you can name it anything you want).

Remove Admin Items for Users

June 20, 2014 (6 years ago)
WordPress, Functions

This function allows you to hide critical admin pages from non-admin users. They will still be able to access the pages if they know the URL, but they will not appear in the admin menu.

add_action( 'admin_menu', 'dianaworks_remove_menu_pages' );

function dianaworks_remove_menu_pages() {
/* only let administrators see these pages */
	if (!current_user_can('activate_plugins') {
		remove_menu_page('tools.php');	
		remove_menu_page('themes.php');	
		remove_menu_page('options-general.php');	
		remove_menu_page('plugins.php');	
		remove_menu_page('admin.php?page=wpcf7');
		remove_submenu_page( 'users.php' , 'user-new.php' );
		remove_submenu_page( 'edit.php' , 'edit-tags.php?taxonomy=category' );
		remove_submenu_page( 'edit.php' , 'edit-tags.php?taxonomy=post_tag' );
		remove_submenu_page( 'link-manager.php' , 'edit-tags.php?taxonomy=link_category' );
		}
	}

 

Add link to More Tag

June 20, 2014 (6 years ago)
WordPress, Functions

Make that pesky […] at the end of excerpts an active link.

Put in functions.php

function new_excerpt_more($more) {
       global $post;
        return '<br><a class="moretag" href="'. get_permalink($post->ID) . '"> [Read More...]</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

 

Upload and Rename a File

June 20, 2014 (6 years ago)
PHP, Functions

I have a couple of applications where users need to upload files regularly, and have them renamed and placed into a specific directory. (These applications run independently from WP, so this is just straight PHP.

The form:

<form action="testupload.php" method="post" enctype="multipart/form-data">
<input type=hidden name="filename" value="renamed.pdf">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

 The processing page:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {

if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }

      if (file_exists("scholarships/" . $_FILES["file"]["name"]))
      {
          unlink ("scholarships/" . $_FILES["file"]["name"]);
      }
      move_uploaded_file($_FILES["file"]["tmp_name"],"scholarships/" . $_POST["filename"]);
      echo "File uploaded and renamed" ;

    
     } else {
     echo "File not uploaded";
}
?>

 

« Previous - Next »

Skip to toolbar