0

I'm still getting to grips with functions and experimenting with different php functions.

I've created a function what I want to use in sync with my custom metabox excerpt. It will basically sanitize the data and make sure its 150 characters long and the first word starts with a capital letter.

My code is very loose at the minute as I'm testing the functionality out, But am kind of lost as to how I should go about this, Sorry for any simple mistakes.

the $test_excerpt was just to test it, but at the moment it outputs nothing.

Code is below.

<?php

//Function that trims any access spaces either side of the excerpt incase it fails the validation run

$text_excerpt = "test";

function vs_trim_excerpt($text_excerpt) {
    if(isset($text_excerpt)) {
    trim($text_excerpt);
    ucfirst($text_excerpt);
    wordwrap($text_excerpt, 150);
    return $test_excerpt();
    } 
}

vs_trim_excerpt();
?>

add_action('init', 'vs_trim_excerpt');

2 Answers 2

0

I am not sure this a WordPress question. It is really more just bad PHP but since that is a filter, in the spirit of fostering good will...

Your function is returning a variable-name function that doesn't exist, and none of your operations are saved at all. You are processing the string but you aren't keeping the changes. You need to assign the results to a variable. And, you aren't feeding the function any information to start with.

$text_excerpt = "test";
function vs_trim_excerpt($text_excerpt) {
    $text_excerpt = trim($text_excerpt);
    $text_excerpt = ucfirst($text_excerpt);
    $text_excerpt = wordwrap($text_excerpt, 150);
    return $text_excerpt;
} 
echo vs_trim_excerpt($text_excerpt);

That should work better.

7
  • Aww thanks s_ha_dum, learn't 3 things already I can't code when im this tired and about saving the functions. does return save the value of the variable after going through all those functions?
    – Dannyw24
    Commented Jul 5, 2013 at 23:17
  • Added another thing that I missed the first time though :)
    – s_ha_dum
    Commented Jul 5, 2013 at 23:19
  • No, it doesn't save anything. You need to echo the result or assign it to a variable.
    – s_ha_dum
    Commented Jul 5, 2013 at 23:21
  • Put a echo in front of the function call
    – s_ha_dum
    Commented Jul 5, 2013 at 23:21
  • I'll accept your awnser s_ha_dum, just need to manipulate this to work with my custom metabox field now :)
    – Dannyw24
    Commented Jul 5, 2013 at 23:22
0

You are missing some basic concepts of programming.

Just because you define a variable in your first line it is not available in the scope of your function (it only exists in the global namespace). Also the functions you call actually return values. You need to assign the return values to a variable to use it. For example have a look at the PHP manual for trim():

Return Values: The trimmed string.

You always should read the manual pages for the functions you use, so you'll see if they return anything and in what form it is returned.

function vs_trim_excerpt($text_excerpt) {
    $text_excerpt = trim($text_excerpt);
    $text_excerpt = ucfirst($text_excerpt);
    $text_excerpt = wordwrap($text_excerpt, 150);
    return $text_excerpt;
} 

$text_excerpt = "test";
$new_excerpt = vs_trim_excerpt($text_excerpt);
var_dump($new_excerpt);
5
  • This should do the trick: $text_excerpt = get_post_meta( get_the_ID(), 'name-of-your-metabox-field', true ); This will only work inside the loop. Manual page is here: codex.wordpress.org/Function_Reference/get_post_meta
    – s1lv3r
    Commented Jul 5, 2013 at 23:42
  • It outputs it but doesn't seem to make the beginning word start in capitals.
    – Dannyw24
    Commented Jul 6, 2013 at 0:25
  • You will need to aditionally apply your function: $text_excerpt = vs_trim_excerpt($text_excerpt);
    – s1lv3r
    Commented Jul 6, 2013 at 8:32
  • do you mean use $text_excerpt = get_post_meta( get_the_ID(), 'name-of-your-metabox-field', true ); first then add $text_excerpt = vs_trim_excerpt($text_excerpt);
    – Dannyw24
    Commented Jul 6, 2013 at 11:51
  • Yes, and after that you can output it. echo $text_excerpt;
    – s1lv3r
    Commented Jul 6, 2013 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.