Support » Plugins » Hacks » How to Check If Option Already Exists in the Options Table?

  • I’m talking about
    add_option, delete_option and update_option

    I know how to check if the values for the options are empty, but I don’t know how to check if the option ENTRY exists in the database in the first place?

    Normally I would do that with num_rows, but how do you do it in WordPress, is there an easy way?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Try this.

    if ( 'impossible_default_value_1234' === get_option( 'myoption', 'impossible_default_value_1234' ) ) {
        echo "<p>option was <strong>not<strong> set</p>";

    And what would I do if the value field of the option is empty in the database?

    Maybe I should explain what I’m doing to make this clearer.

    I’m developing a theme and I want the user to tick off/on pages from the navi bar.

    Now if the option does NOT exist, I’d have to add it with add_option, but if it DOES exist and the value field is empty (maybe because the user hid pages before and then made them visible again), then I’d have to update the options row with update_option.

    And now I need a way to check if the options row exists or not, even if the value field is empty. As far as I could understand, your example would work if I have a value.

    Or am I mistaking?

    You can always use update_option() which will create the option if it doesn’t exist.
    For values, you should use 1 and 0, instead of option exists or doesn’t exist. Also look into PHP’s ‘===’ and ‘!==’ (compared to ‘==’ and ‘!=’).

    However, you should store many settings in one option, using an array, rather than lots and lots of options.

    Some things to consider:
    1. In your example, perhaps have an array $my_theme_nav_pages (stored as option ‘my_theme_nav_pages’) with keys for pages, and values 1 or 0.

    2. Maybe your theme should support WP’s standard navigation menus, where a user already has an interface to build a navigation menu of pages or anything else.

    Ohh the solution was that simple, I didn’t realize that I can always use update_option, even if the option doesn’t exists. I just rewrote my script and it works GREAT!.

    Though I realize I may not have chosen the most efficient solution, since you mentioned arrays, I could have used arrays as well. What I rather did was I simply appended all the pages with a foreach loop and space between the page ID’s to ONE option and then it will be inserted into the database like this.

    64 57 16

    And then I take those values with get_option in the header file and split them with the php explode function and then I implode them while separating them with a comma. The imploded string gets inserted into a variable, and that variable gets inserted into the “exclude” attribute of wp_list_pages.

    To me, this is a very straight forward solution, it’s not quite bad for the fact that I’m only programming since 4 month.

    This way the user can:
    – hide selected pages
    – make selected pages visible again
    – or reset everything (make every page visible)

    The reason why I need this functionality is because I have blurbs on the front page, where the user can enter text like “about blog” or “about author”, and those blurbs are entered by creating a page. The blurbs simply take the title and the plain text with get_the_content.

    Since the pages will show in the navi bar, it’s good to simply let the user tick them off.

    I will look into those WP standard navigation menus, I never got seriously into them. After I explained everything, do you think I could have accomplished the same thing with that too?

    Though I will leave everything as it is, because I need a unique functionality for my theme to have it accepted by market places. Besides this functionality I really don’t have much more to offer, it’s a quite simple though nice looking theme.

    Thanks for your help.

    From your explanation, standard navigation probably wouldn’t fit with what you are doing.

    One thought though; if your theme options know which pages are being used as the blurbs, perhaps you could automatically exclude them from the navigation?

    Anyway, glad you got sorted out.

    Well thanks for the heads up about the standard navigation.

    It is a great suggestion to have them automatically hidden, something I already thought about.

    But here’s the reason why I didn’t do it that way. For example, maybe the user wants to write a lengthy About Us page beyond the short blurb on the front page, he’d simply break the text with a read more button. So when visitors read the intro text in the blurb they’d click on read more and then they’d get let to the About Us page.

    So from a functionality perspective this is a big PLUS and makes the theme more valuable 🙂

    Yes, I see what you mean, it makes sense to allow what you describe.

    One more suggestion which might make things more natural/easier: and this depends on your UI, but in the place where the user enters the page (id?) to place in slot 1 (or however you designate the blurb areas), also give them a checkbox to say “and exclude from navigation”, that way the user gives you both pieces of information in a more natural way.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to Check If Option Already Exists in the Options Table?’ is closed to new replies.