Support » Plugins » Building a plugin – saving to options to the database?

  • Resolved sepp88

    (@sepp88)


    Hi guys,

    i followed the instructions in the wordpress codex to built a simple plugin. all i wanna do is saving two different strings to the database.

    // variables to get options
    $opt_text = 've_words'; //text
    $opt_link = 've_spanclass'; //link
    
    function ve_options_page() {
    	// variables for the field and option names
        $hidden_field_name = 've_submit_hidden';
        $data_field_text = 've_words';
    	$data_field_link = 've_spanclass';
    
        // Read in existing option value from database
        $opt_val_text = get_option( $opt_text );
    	$opt_val_link = get_option( $opt_link );
    
        // See if the user has posted some information
        // If so, this hidden field will be set to 'Y'
        if( $_POST[ $hidden_field_name ] == 'Y' ) {
    
            // Read posted value
            $opt_val_text = $_POST[ $data_field_text ];
    		$opt_val_link = $_POST[ $data_field_link ];
    
            // Save the posted value in the database
            update_option( $opt_text, $opt_val_text );
    		update_option( $opt_link, $opt_val_link );
    
            // Put an options updated message on the screen
    ?>
    ...

    What am i doing wrong. I’m setting up to different option-names (opt_text and opt_link) and i want to save them. I have to inputfields to enter the two options values.

    however, they kind of overwrite eavh other? can you guys help me please.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Does it make a difference if you put the variables inside the function?

    function ve_options_page() {
    
    // variables to get options
    $opt_text = 've_words'; //text
    $opt_link = 've_spanclass'; //link

    thank you. YES IT DOES MAKE A DIFFERENC! now it’s working. however i’ve no idea how to read the saved data if i don’t set the variables “globally” (outside the function).

    I want to read the saved values inside this function.

    add_filter('the_excerpt', 'verbalessences');
    
    function verbalessences($content) {
    
      	$pattern = get_option( $opt_text );
    	echo $pattern;
        return $content;
    
    }

    I don’t know why it’s not working. You may have any ideas!

    You can set them globally, but you must declare them global.

    // variables to get options
    $opt_text = 've_words'; //text
    $opt_link = 've_spanclass'; //link
    
    function ve_options_page() {
     global $opt_text, $opt_link;
    ...

    Thank you.

    You are welcome. Now, please use the dropdown at top right to mark this topic ‘Resolved’.

    oh sorry, i have one more question.
    If i use the add_filter(the_content, add_filter(the_excerpt function my plugin retrieves the same value of the database twice.

    add_filter('the_content', 'runthrough');
    //add_filter('the_excerpt', 'runthrough');
    
    function runthrough($content) {
    
    	//Getting Data
    	$opt_words = 've_words'; //words
    	$opt_val_words = get_option( $opt_words );
    	//
    	echo $opt_words;
    
    	...

    So for instance if $opt_val_words is set to “Lorem, amet” in the database, i get “Lorem, ametLorem, amet” if i add the filter to the_content and the_excerpt. If I set it to just one of both (either the_content or the_excerpt) i get the value only once as it should be. I have no idea how to fix this. I want to run through the_content and the_excerpt but of course want to retrieve the saved data only once.

    ???

    This is a wild shot, but it may work:

    function runthrough($content) {
    
       //Getting Data
       $opt_words = 've_words'; //words
       $opt_val_words = get_option( $opt_words );
       //
       if (false === strpos($content,$opt_words) echo $opt_words;
    
       ...

    Unfortanately it’s not working. Somehow my plugin isn’t working if i add the filter to the_content and the_excerpt at the same time.

    I thought it was working twice before – you said you were getting two copies of the words.

    I copied the code snippet that I gave above from your earlier example. Should it really be this:

    function runthrough($content) {
    
       //Getting Data
       $opt_words = 've_words'; //words
       $opt_val_words = get_option( $opt_words );
       //
       if (false === strpos($content,$opt_val_words) echo $opt_val_words;
    
       ...

    Glad you worked it out!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Building a plugin – saving to options to the database?’ is closed to new replies.