Grow your CSS skills. Land your dream job.

[WORDPRESS] Remove div IF textarea is empty for Events Post Type Function

  • __
    # June 14, 2013 at 9:53 pm

    I assume this is lines 134-137…?

    On the surface, I don’t see any reason why this wouldn’t work as desired. No offense to you (or anyone), but I have a strong dislike for WP behind-the-scenes, and the “shortcode” and “functions.php” models are two of the main reasons why. : )

    `get_post_meta` (with `$single=true`) is supposed to return an empty string if it finds nothing.

    /* snip */
    // on|around line 134:
    $event_info = get_post_meta($post->ID, ‘event_info’, true);

    // add this right here
    var_dump( $event_info ); exit;

    // tell me what it shows you

    # June 14, 2013 at 10:06 pm

    @traq Yes I apologize for not pointing out the location of the issue.

    I’m not a PHP guru lol so I’m lost.
    BUT I did add


    var_dump( $event_info ); exit;

    above these lines //

    if(!empty($event_info)) {
    $ret .= ‘INFO:   ‘ . $event_info . ‘‘ ;
    }

    and I didn’t necessarily get an error page, but I got some sort of message in the content area of my template//

    string(33) “Extra Details “

    __
    # June 14, 2013 at 10:29 pm

    >`string(33) “Extra Details “`

    It says “33”? Is “Extra Details ” the entire text it returns?

    Does the pastebin you posted contain **all** the code you’re using?

    In any case, that means `$event_info` is not empty – so the condition is true, and “INFO” will be added. Is this an event where you expect the event info to be empty?

    If not, if you can, try running this with an event where you expect `$event_info` to be empty.

    # June 14, 2013 at 10:38 pm

    @traq would you like me to post the Whole entire CPT + Shortcode Function ?
    Because the pastebin only includes the Shortcode section…

    I basically want the “INFO” text etc to be removed IF the Event has no extra information inserted into the textarea I created…

    If it helps I’ve taken [a screenshot of the issue](http://i43.tinypic.com/2s7xbuw.png “http://i43.tinypic.com/2s7xbuw.png”).

    __
    # June 14, 2013 at 10:48 pm

    No, I understand the issue; I’m just trying to understand where the problem is coming from. The code you posted seems like it should be producing the desired result –

    if your `if( ! empty( $event_info ) )` condition is _always true_, I’d like to backtrack and see what might be causing `$event_info` to _never be empty_.

    Yes, it would be helpful to see _everything_ you wrote.

    In particular, how did you define the fields for your custom post type?

    # June 14, 2013 at 11:01 pm

    @traq Here is the ENTIRE Events CPT http://pastebin.com/4DA0AFQw

    :)

    # June 14, 2013 at 11:10 pm

    @traq Line 116 should be the location of “Info” textarea…

    # June 14, 2013 at 11:13 pm

    Adrian, what if it was outputting whitespace even if she didn’t add anything to the textarea? That would also make it run true.

    __
    # June 14, 2013 at 11:28 pm

    alright… what I’m looking for is something that might add the value “Extra Details” as a default value for your `event_info` column. I’m not seeing anything – that phrase doesn’t appear anywhere in the code you posted. Any ideas?

    >Adrian, what if it was outputting whitespace even if she didn’t add anything to the textarea? That would also make it run true.

    Yes, it would. But WP says it should return “an **empty** string” in that instance… unless whitespace (or something else, like a javascript “placeholder” value, for example) is actually being added to the form field somehow.

    @Keilowe, when you ran my code the first time, did you expect `$event_info` to be empty?

    Also,
    >It says “33”? Is “Extra Details ” the entire text it returns?

    # June 14, 2013 at 11:41 pm

    @traq “INFO” is just text I added as a label.

    $ret .= ‘

    ‘;
    $event_info = get_post_meta($post->ID, ‘event_info’, true);
    if ( ! empty($event_info)) {
    $ret .= ‘INFO: ‘ . $event_info . ‘‘ ;
    }
    $ret .= ‘

    ‘;

    Even if $event_info is empty the text “INFO” still appears.

    # June 14, 2013 at 11:44 pm

    @traq when I use the line of code you posted

    $ret .= ‘

    ‘;
    $event_info = get_post_meta($post->ID, ‘event_info’, true);
    var_dump( $event_info ); exit;
    if ( ! empty($event_info)) {
    $ret .= ‘INFO: ‘ . $event_info . ‘‘ ;
    }
    $ret .= ‘

    ‘;

    [This happens](http://i39.tinypic.com/2yvrzok.png “http://i39.tinypic.com/2yvrzok.png”). It removes my container and all the events and just displays the

    > “string(33) “Extra Details “”

    # June 14, 2013 at 11:54 pm

    @traq @chrisburton I FIXED IT!

    __
    # June 14, 2013 at 11:57 pm

    >Even if $event_info is empty the text “INFO” still appears.

    What I’m getting at is that, if you see “INFO”, that means that `$event_info` was _not_ empty, even though you expected it to be. `var_dump` lets us see the value that `$event_info` contained: a string, 33 characters long. Since we only see 15 characters displayed, I’m assuming the other 18 are HTML markup or whitespace of some sort.

    Try viewing the HTML source of your webpage, and look for the full value after `string(33)`. Copy+paste it, please.

    >This happens. It removes my container and all the events …

    Right, because I had you do `exit;`. It’s not a solution, it’s a test. Because we exit the script, then the value we dumped should be the _last_ bit of text on your webpage (easier to find that way).

    __
    # June 14, 2013 at 11:58 pm

    >@traq @chrisburton I FIXED IT!

    …great! What was going on?

    # June 14, 2013 at 11:59 pm

    @traq @chrisburton I used a test line that the author of the tutorial I followed recommended to me.
    The test line is//

    $ret = $ret . ‘INFO: {‘ . $event_info . ‘}‘ ;

    By adding that code I could see if there was a space added into the textarea but no actual text. AND THERE WAS IN FACT A FEW SPACES!

    So I edited the event with the empty Info, deleted the spaces and now…what do you know!!! The label “INFO” disappeared!!!!

    I find that ridiculous though :/ A Few spaces could make such a difference.
    Is there any possible way for WP to only display the INFO label if actual text was inserted?

    Does that question even make sense?

    I really apologize for wasting your time :/
    Such a small mistake could cause so much confusion!

Viewing 15 posts - 16 through 30 (of 33 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".