WordPress.org

Ready to get started?Download WordPress

Forums

Types - Custom Fields and Custom Post Types Management
[resolved] Why return an empty string instead of null when there is nothing in the field (3 posts)

  1. moxojo
    Member
    Posted 1 year ago #

    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 http://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!

    http://wordpress.org/extend/plugins/types/

  2. mbigul
    Member
    Plugin Author

    Posted 1 year ago #

    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();
    }

  3. moxojo
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic