WordPress.org

Ready to get started?Download WordPress

Forums

OptionTree
Making Changes in Option Tree via database (2 posts)

  1. technologyspa
    Member
    Posted 1 year ago #

    Hello,
    I am having an issue with Option Tree whenever I attempt to make a change to the database manually. The I am editing under wp_options with the option_name... option_tree. My purpose for doing is to edit the url when moving the site from staging to production. If I make a change in anything under the option_value, the site is broken and can only be fixed by returning the value back to the original. Let's say I have s:4:"logo",s:88:"http://stage.google.com"... etc.... and I change it to s:4:"logo",s:88:"http://www.google.com" the site is broken... simply meaning that the option tree commands for getting data do not return any values.
    I'm assuming there is some other reference for option tree that sets how things "should be" and when the two aren't the same... then that breaks things. I'm also assuming it may have something to do with the export page in option tree that gives you large values for exporting settings etc from one site to another. Is that stored somewhere? Is there a way to reset it so that maybe option tree re-initializes things?
    I simply need a way to make changes to option tree without using the gui because there are a large number of options. Any help is very appreciated....

    http://wordpress.org/extend/plugins/option-tree/

  2. technologyspa
    Member
    Posted 1 year ago #

    So I thought I would save other people from the same frustration. The reason you can't simply do a find and replace on the database when moving from one url to another is because the data stored in option_value for option_tree is serialized. So for every value in the array, it has the number of characters associated with it. So the values that I posted before:
    s:4:"logo",s:88:"http://stage.google.com"
    ... are messed up because it thinks that there are 88 characters in the url and when there are not, it breaks the site.
    So Here's what I did. I initially attempted to go directly to the database to put the serialized string. For some reason it failed. So I tracked down how it was done in the plugin (used the get_option('option_tree')) and started the process of storing it to an array and then finding the values I needed to replace and replacing them and storing them back into the array.
    You can simply change the str_replace values with your own to make it work. After that, I attempted to serialize the array and then dump it back into the option but that didn't work. So I am simply echoing out the serialized array and then base64 encoding it to use it in the import portion of the plugin. If you import it in using the developer's tool... then it works like a champ. The last thing I had to do was to run a couple of update commands on the database:

    UPDATE wp_options SET option_value=replace(option_value, "stage-v2.google.com", "www.google.com");
    UPDATE wp_posts SET guid=replace(guid, "stage-v2.google.com", "www.google.com");
    UPDATE wp_options SET option_value=replace(option_value, "www.someotherdevelopmentsite.com/google", "www.google.com");
    UPDATE wp_posts SET guid=replace(guid, "www.someotherdevelopmentsite.com/google", "www.google.com");

    Here's the php code:

    <?php
    define('WP_USE_THEMES', false);
    global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
    require('wp-load.php');
    
    $array = replaceVal(get_option('option_tree'));
    
    function replaceVal(&$array) {
    foreach ($array as $key=>$val){
    	if (is_array($val)) {
    		$val = replaceVal($val);
    	}
    	else {
    $val = str_replace('stage-v2.google.com','www.google.com',$val);
    		$val = str_replace('www.anotherdevelopmentsite.com/google','www.google.com',$val);
    	}
    			$array[$key] = $val;
    }
    return $array;
    }
    $serializedandencoded = base64_encode(serialize($array));
    echo '<pre>';print_r($serializedandencoded);echo '</pre>';
    ?>

    I would really like a way to change the database directly without having to run the import, but it was as good as I could get with needing it really quickly.
    I could see the developer possibly adding a "change url" field so that all the pictures etc could be changed dynamically. Or maybe switching to json would alleviate painpoint as well. All just suggestions. If anyone figures out how to write it directly to the database instead of doing an import I would LOVE to see it. :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.