• Resolved moxojo

    (@moxojo)


    Maybe a noob question but was causing some frustration.

    Is there a reason to return an empty string instead of NULL when getting a fields when it doesn’t have a value?

    I want to check if a field is null. i.e. if someone has forgotten to fill it out, if they have I want to run another some other code. Here is an example.

    //uses Types Field API https://wp-types.com/documentation/functions/
    //get our custom title....
        $post_h1_title = types_render_field('h1-title', array('raw' => TRUE));
        	if (!is_null($post_h1_title)) {
        	     echo $post_h1_title;
        	}
        	//but if that doesn't exist then get the post title
        	else {
        	    the_title();
        	}

    The only problem is that this doesn’t work because types_render_field returns an empty string if the field isn’t filled out. I realize I could test for this, but that doesn’t seem to be intuitive.

    So is there a reason for returning an empty string instead of something that can be tested with isset or is_null? If there is that is cool, if not then could this get changed?

    Thanks for your work on the plugin and for the great documentation. Cheers!

    https://www.ads-software.com/extend/plugins/types/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Dear moxojo,

    Please try the following and let me know the following.


    $post_h1_title = types_render_field('h1-title', array('raw' => true));
    if (!empty($post_h1_title)) {
    echo $post_h1_title;
    }
    //but if that doesn't exist then get the post title
    else {
    the_title();
    }

    Thread Starter moxojo

    (@moxojo)

    …oops, thanks for kindly overlooking my stupidity. I can’t believe I didn’t think of that.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Why return an empty string instead of null when there is nothing in the field’ is closed to new replies.