How To Add New Option In WP Options WordPress By Ajax – Plugins Development

In this tutorial we will talk about how to add new option in wordpress table named wp options by ajax. I have just used this approach in one of my plugins and I decided to show you an example if you ever need it. It's very simple to use this options and is very useful because, when you develop a plugin from scratch and let's you need few settings to be stored in database, you can save them in wp options because all this kind of options go there. There are 2 functions which will save the option for you in the database but we will use only one, because is much easier and is just as good.

 

First function: add_option('field_name', 'field_value'). 

As you can see from the function, it will add a new row with the name from first parameter, and value from the second parameter. This function only will insert the new row. But instead using this funtion we will use update_option(); because it will add a new row as add_option() do, and if the row already exist in the database, this function will update the row with new values.

update_option('column_name', 'new_value');

To get this row you can use the get_option(); function. just type the function and the column name you want to select from database.

 

In my case I need to run this function when a radio button is changed so my ajax will look like the following:

jQuery('.status_check input').on('change', function(){
    var id = jQuery(this).val();
    jQuery.ajax({ 
      method: "POST",
      data:{
            	action:'update_status',
        		id: id
    		},
        	url: $_base_website_url+"/wp-admin/admin-ajax.php",
        	success:function(result){
        		console.log(result);
        	},
        	error:function(error){
        		console.log(error);
        	}
    });
  });

 

What the above script do? When "change" event is triggered the ajax will run and it will search for "update_status" function which is a little below. Anyway you can change this function according to your needs, this ajax can be used at click event or any other. Once the function is called we will return a response which should be appear in the console because on success function we have console.log(result);

And below you can see how I used the update_option() function:

public function update_status(){
    $id = $_POST['id'];
    update_option( 'status_active', $id);
    exit();
  }

 

We look for $_POST id the id which is sent from ajax, once we have the id, we update the status_active column from wp-options with the new status and that's it. Then if we want to get the status we can just use the following function:

public function check_status(){
    return get_option('status_active');
  }

 

This function are in a class so if you want to put them in functions.php you have to change them a little. We have all we need but one more thing needs to be done because if you will run now the ajax you will see in console a 400 bad request from admin-ajax.php that's because the ajax does not find our functions, to solve this problem we need to register the functions with the following lines of code:

add_action('wp_ajax_update_status', array($this, 'update_status'));
add_action('wp_ajax_nopriv_update_status', array($this, 'update_status'));

Remember I am in class that's why I am using array($this, 'update_status'), if you are in the functions.php file you just put the name of the function there with no array.

That's it, with these few lines of code you can add new option in wp options wordpress table. I use this approach in plugins development but it can be used everywhere in wordpress there is no rule to use this only in plugins. You can insert this function in functions.php, but without the function should not be public. There you can just declare it as function and everything will work.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x