Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created November 3, 2011 02:17
Show Gist options
  • Select an option

  • Save billerickson/1335597 to your computer and use it in GitHub Desktop.

Select an option

Save billerickson/1335597 to your computer and use it in GitHub Desktop.
Shorten user description
<?php
/**
* Limit User Description to 50 characters
* @link https://gist.github.com/1335597
* @param string $description
* @return string modified description
*/
function be_limit_description( $description ) {
$max_characters = 50;
if ( strlen( $description ) > $max_characters ) {
/** Truncate $text to $max_characters + 1 */
$description = substr( $description, 0, $max_characters + 1 );
/** Truncate to the last space in the truncated string */
$description = trim( substr( $description, 0, strrpos( $description, ' ' ) ) );
}
return $description; // Return the trimmed description to wp_insert_user();
}
add_filter( 'pre_user_description', 'be_limit_description' );
@billerickson
Copy link
Copy Markdown
Author

I don't know why this doesn't work. If I change update_user_meta at the end to use a different field (ex: 'brand_new_field' ), it works for creation but it doesn't ever update.

I pasted two paragraphs of text in my description, and if you do print_r( $description ) right before update_user_meta you can see it successfully shortened it.

@ramseyp
Copy link
Copy Markdown

ramseyp commented Nov 3, 2011

Same here. All the way down, I could var_export($description) the value was correct. If I var_export($update), I get "true", but that update seems to just not do anything. I'm using WP3.3beta2.

@ramseyp
Copy link
Copy Markdown

ramseyp commented Nov 3, 2011

if you add

$newdesc = get_user_meta( $user_id, 'description', true );
var_export ($newdesc);

at line 23, $newdesc returns the shortened description.

@norcross
Copy link
Copy Markdown

norcross commented Nov 3, 2011

May be a bug. I tried using the basic example from the Codex (http://codex.wordpress.org/Function_Reference/update_user_meta) and got nothing.

@billerickson
Copy link
Copy Markdown
Author

Paul Menard on the wp-austin mailing list pointed out the issue. The hook I was using was being called too early and was being overwritten later on based on the data in $_POST. I updated the above code to use the 'pre_user_description' filter which runs much later.

@norcross
Copy link
Copy Markdown

norcross commented Nov 3, 2011

Glad it's working.

@ramseyp
Copy link
Copy Markdown

ramseyp commented Nov 3, 2011

Paul rocks. Excellent filter mod, Bill. @norcross, it's weird that the update_user_meta method returns the value in the array, but it doesn't record it. Seems a little voodoo-ish.

@billerickson
Copy link
Copy Markdown
Author

@ramseyp, I think the issue is that wp_insert_user() didn't care what was currently in the database. When the page loads, it puts the database value into the textarea. When the page is saved, it updates the user_meta to whatever is in that textarea (what's in $_POST['description']). My code was hopping in the middle of that, updating the database (but then it gets overwritten later on).

I was able to get my original code to work by changing the last line from this: $update = update_user_meta( $user_id, 'description', $description ); to this: $_POST['description'] = $description;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment