WordPress.org

Forums

[resolved] having an issue with get_option and checking value (2 posts)

  1. Doodlebee
    Member
    Posted 4 years ago #

    Just curious: I'm writing a plugin (well, I've already written it and it's in the repository, I'm trying to update it for the upcoming release of WordPress) and I wanted to "fix" something that's been bugging me. The plugin is set to create a post when it's initially activated. Originally, it would just check for the presence of the post, and if it existed, it wouldn't create it again - but if the end user permanently deleted it, it would come back. This annoys me. (I'm sure it annoys end users too.)

    So after a discussion with someone, it was determined that if I set an option value in the database, and have the plugin check for the existence of *that*, then people could delete the initial post and it wouldn't come back. Cool. I was avoiding putting anything in the database, but I think it's worth it here - it's just a single, small option.

    So in my code, I have this:

    function check_bei_posts() { // checks to see if the plugin exists
    	$optioncheck = get_option('_back_end_instructions'); // look for it in the options table
    	if(!$optioncheck) { // if it doesn't exist...
    		add_action('admin_init', 'bei_create_first_post'); //insert that first post
    		add_option('_back_end_instructions', 'true', '', 'no'); // and add the option so if the admin deletes it, it won't come back.
    	}
    }

    Now, if I remove the "add_action" line, it'll pop the new option value in the database. Yay. However, it won't create the post. If I remove the "add_option" line, then it'll create the post on activation like it's supposed to. So I know the code in both functions are correct, because it's doing what it should.

    The problem is, it won't do *both*. I'm basically checking to see if $optioncheck is true, and if it is NOT, I want it to add the option AND create the post. All it'll do is add the option - unless I remove the add_option line, and then it'll create the post (and I'm back where I was at the beginning.)

    Am I missing something? Because I cannot, for the life of me, figure out why it won;t 1) check for the "true" value of $optioncheck; 2) if $optioncheck is false, it'll add it and 2a) it'll add the new post.

    I'd really appreciate another set of eyes on this, or perhaps alternate idea on how to pull this off? - thanks! :)

    EDIT: apparently the "Version" thing in this form doesn't allow letters. I'm using 3.2 RC 1.

  2. Doodlebee
    Member
    Posted 4 years ago #

    Okay! I got it. Instead of adding the option right there at the check, I put the add_option call within the create_first_post function. Works a treat!

    Thanks anyway :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.